A Python library for sending and receiving CAN messages via the PEAK PCan adapter.

Initial commit.
This commit is contained in:
Christian Sltrop 2012-07-06 14:52:16 +02:00
commit b4631bbd9a
18 changed files with 4997 additions and 0 deletions

99
CANLibrary/CanMessage.py Normal file
View file

@ -0,0 +1,99 @@
# -*- coding: UTF-8 -*-
'''
Created on 06.07.2012
@author: Christian Sültrop
'''
#import CanSignal
import array
import sys
class CanMessage(object):
'''
A CAN message.
A CAN message has an ID, a Length in bytes, an array of signals, and Data.
'''
# Class variables
def __init__(self, MessageId, MessageLength, MessageCycleTime, Label):
'''
Create a new CanMessage object.
@param MessageId: The CAN ID. Valid range 0x00 to 0x7ff.
@param MessageLength: The message length in bytes.
@param messageCycleTime: Cycle time in ms.
'''
# Instance variables
self.Signals = {}
self.Data = array.array('B')
# check message length for validity
if MessageLength > 8:
sys.exit('Invalid message MessageLength')
# check message ID for validity
if (MessageId < 0x000) or (MessageId > 0x7ff):
sys.exit('Invalid MessageId')
# assign the parameters
self.Length = MessageLength
self.Id = MessageId
self.CycleTime = MessageCycleTime
self.Label = Label
# create an initial array of message data
for i in range(0, self.Length):
self.Data.append(0x00)
def addSignal(self, CanSignal):
'''
Add a Signal of type CanSignal to the list of signals.
@param CanSignal: The signal to add to the list of signals.
'''
self.Signals.update({CanSignal.Label: CanSignal})
def composeData(self):
'''
Takes the CanSignals from the list in self.Signals and copies the data that is stored in their Data fields into the Data field
of the CanMessage object. The position where to copy the data is defined in the Begin and Length fields of the CanSignal objects.
'''
for symKey in self.Signals:
sym = self.Signals[symKey]
srcBegin = 0 # reading the source data (from the Signal definition) always starts at the first bit
tgtBegin = sym.Begin # where the source data goes is defined in the Begin field of the CanSignal object
srcByteNo = (srcBegin/8) # will be 0 if srcBegin == 0
srcBitNo = (srcBegin%8) # will be 0 if srcBegin == 0
tgtByteNo = (tgtBegin/8) # get the byte and bit numbers
tgtBitNo = (tgtBegin%8) # for the target (the CanMessage object) array
for i in range(0, sym.Length):
# copy the source data bits to the target
# OR # get srcBitNo from srcByteNo and shift it to tgtBitNo
if tgtByteNo >= self.Length:
sys.exit('Signal does not fit into message!')
self.Data[tgtByteNo] |= ((sym.Data[srcByteNo] >> srcBitNo) & 0x01) << tgtBitNo
# increment the counters
srcBitNo += 1
if srcBitNo >= 8: # on bit counter overflow, increment the byte counter
srcBitNo = 0
srcByteNo += 1
tgtBitNo += 1
if tgtBitNo >= 8:
tgtBitNo = 0
tgtByteNo += 1

BIN
CANLibrary/CanMessage.pyc Normal file

Binary file not shown.

44
CANLibrary/CanSignal.py Normal file
View file

@ -0,0 +1,44 @@
'''
Created on 06.07.2012
@author: sueltrop
'''
import sys
import array
class CanSignal(object):
'''
A CAN Signal.
A Signal has a Begin, a Length and Data. The Label is a textual tag that identifies the Signal to
a human.
'''
Begin = 0;
Length = 0;
Data = [];
Label = ''
def __init__(self, SignalBegin, SignalLength, SignalData, SignalLabel):
'''
Constructor.
@param SignalBegin: At which bit does the Signal begin in a CAN message?
@param SignalLength: The length of the Signal data in bits.
@param Label: A text label that describes the Signal.
'''
self.Begin = SignalBegin
self.Length = SignalLength
self.Label = SignalLabel
self.Data = SignalData
def SetData(self, Data):
'''
Updated the data of the Signal.
The data is given to the Signal as full bytes, but only the bits up to Length may be used!
@param Data: An array of unsigned characters containing the data.
'''
#if not (len(Data) == self.Length):
# sys.exit('Data has invalid length')
self.Data = Data

BIN
CANLibrary/CanSignal.pyc Normal file

Binary file not shown.

65
CANLibrary/PCan.py Normal file
View file

@ -0,0 +1,65 @@
# -*- coding: UTF-8 -*-
'''
Created on 06.07.2012
@author: Christan Sültrop
'''
import PCANBasic
import sys
from PCANBasic import PCAN_BAUD_1M, PCAN_BAUD_125K
class PcanAdapter (object):
'''
A class for controlling a PEAK PCan adapter. Based on the PCANBasic library.
'''
Baudrate = { '125k' : PCANBasic.PCAN_BAUD_125K,
'500k' : PCANBasic.PCAN_BAUD_500K,
'1000k' : PCANBasic.PCAN_BAUD_1M }
def __init__(self, Baudrate):
self.Channel = PCANBasic.PCAN_USBBUS1
self.Pcan = PCANBasic.PCANBasic()
self.Baudrate = Baudrate # Baudrate from PCANBasic
self.isInitialised = False
def __del__(self):
print '\nDestructor:'
self.uninitialize()
def initialize(self):
self.Pcan.Uninitialize(PCANBasic.PCAN_NONEBUS)
status = self.Pcan.Initialize(self.Channel, self.Baudrate)
if status != PCANBasic.PCAN_ERROR_OK:
print 'Error: ', self.Pcan.GetErrorText(status, 0)[1]
sys.exit("PCAN initialization error")
print("PCAN initialized")
self.isInitialised = True
channel, hwId = self.Pcan.GetValue( self.Channel, PCANBasic.PCAN_DEVICE_NUMBER )
print 'DeviceNumber: ', hwId
def uninitialize(self):
if self.isInitialised:
status = self.Pcan.Uninitialize(self.Channel)
if status != PCANBasic.PCAN_ERROR_OK:
print 'Error: ', self.Pcan.GetErrorText(status, 0)[1]
sys.exit("PCAN deinitialization error")
print("PCAN deinitialized")
def sendMessage(self, Message):
Message.composeData()
msg = PCANBasic.TPCANMsg()
msg.ID = Message.Id
msg.MSGTYPE = PCANBasic.PCAN_MESSAGE_STANDARD
msg.LEN = Message.Length
msg.DATA[0:len(Message.Data)] = Message.Data
self.Pcan.Write(self.Channel, msg)
#print ('Message ' + Message.Label + ' written.\n')

BIN
CANLibrary/PCan.pyc Normal file

Binary file not shown.

0
CANLibrary/__init__.py Normal file
View file

BIN
CANLibrary/__init__.pyc Normal file

Binary file not shown.

65
CANLibrary/example.py Normal file
View file

@ -0,0 +1,65 @@
# -*- coding: UTF-8 -*-
'''
Example for creating and sending CAN messages via PCAN using the CanMessage, CanSymbol and PCan classes.
@author: Christian Sültrop
'''
from CanMessage import CanMessage
from CanSymbol import CanSymbol
from PCan import PcanAdapter
print '\ncreate some messages'
mMotor_1 = CanMessage(0x280, 8, 10)
print 'mMotor_1.Data', mMotor_1.Data
mMotor_2 = CanMessage(0x288, 8, 20)
print 'mMotor_2.Data', mMotor_2.Data
print '\ncreate some symbols'
# for mMotor_1
MO1_Leergas = CanSymbol(0, 1, [0x01], 'MO1_Leergas')
MO1_Sta_Pedal = CanSymbol(1, 1, [0x01], 'MO1_Sta_Pedal')
MO1_Mo_m_ex = CanSymbol(8, 8, [0x00], 'MO1_Mo_m_ex')
value = int(3000*0.25)
MO1_Drehzahl = CanSymbol(16, 16, [value>>8, value&0x00ff], 'MO1_Drehzahl') # split into two bytes
mMotor_1.addSymbol(MO1_Leergas)
mMotor_1.addSymbol(MO1_Sta_Pedal)
mMotor_1.addSymbol(MO1_Mo_m_ex)
mMotor_1.addSymbol(MO1_Drehzahl)
print MO1_Drehzahl.Data
# for mMotor_2
MO2_Kuehlm_T = CanSymbol(8, 8, [int(60*0.75)-48], 'MO2_Kuehlm_T')
mMotor_2.addSymbol( MO2_Kuehlm_T )
mMotor_2.addSymbol( CanSymbol(24, 8, [0], 'MO2_GRA_Soll') )
mMotor_2.addSymbol( CanSymbol(0, 6, [44], 'MO2_CAN_Vers') )
print '\nManipulate Symbols'
# does not work yet! need a way to replace a symbol -> dictionary
MO1_Leergas.SetData( [0x00] )
mMotor_1.addSymbol( MO1_Leergas )
print '\nCreate a PCAN adapter'
pcan = PcanAdapter(PcanAdapter.Baudrate['500k'])
pcan.initialize()
print '\nSend the messages'
mMotor_1.composeData()
pcan.sendMessage(mMotor_1)
mMotor_2.composeData()
pcan.sendMessage(mMotor_2)
print 'end of program'

335
PCANBasic/PCANBasic.bas Normal file
View file

@ -0,0 +1,335 @@
Attribute VB_Name = "PCANBasic"
' PCANBasic.bas
'
' ~~~~~~~~~~~~
'
' PCAN-Basic API
'
' ~~~~~~~~~~~~
'
' ------------------------------------------------------------------
' Author : Keneth Wagner
' Last change: 08.11.2011 Wagner
'
' Language: Visual Basic 6.0
' ------------------------------------------------------------------
'
' Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
' more Info at http://www.peak-system.com
'
'///////////////////////////////////////////////////////////
'/ Value definitions
'///////////////////////////////////////////////////////////
' Currently defined and supported PCAN channels
'
Public Const PCAN_NONEBUS As Byte = &H0 ' Undefined/default value for a PCAN bus
Public Const PCAN_ISABUS1 As Byte = &H21 ' PCAN-ISA interface, channel 1
Public Const PCAN_ISABUS2 As Byte = &H22 ' PCAN-ISA interface, channel 2
Public Const PCAN_ISABUS3 As Byte = &H23 ' PCAN-ISA interface, channel 3
Public Const PCAN_ISABUS4 As Byte = &H24 ' PCAN-ISA interface, channel 4
Public Const PCAN_ISABUS5 As Byte = &H25 ' PCAN-ISA interface, channel 5
Public Const PCAN_ISABUS6 As Byte = &H26 ' PCAN-ISA interface, channel 6
Public Const PCAN_ISABUS7 As Byte = &H27 ' PCAN-ISA interface, channel 7
Public Const PCAN_ISABUS8 As Byte = &H28 ' PCAN-ISA interface, channel 8
Public Const PCAN_DNGBUS1 As Byte = &H31 ' PCAN-Dongle/LPT interface, channel 1
Public Const PCAN_PCIBUS1 As Byte = &H41 ' PCAN-PCI interface, channel 1
Public Const PCAN_PCIBUS2 As Byte = &H42 ' PCAN-PCI interface, channel 2
Public Const PCAN_PCIBUS3 As Byte = &H43 ' PCAN-PCI interface, channel 3
Public Const PCAN_PCIBUS4 As Byte = &H44 ' PCAN-PCI interface, channel 4
Public Const PCAN_PCIBUS5 As Byte = &H45 ' PCAN-PCI interface, channel 5
Public Const PCAN_PCIBUS6 As Byte = &H46 ' PCAN-PCI interface, channel 6
Public Const PCAN_PCIBUS7 As Byte = &H47 ' PCAN-PCI interface, channel 7
Public Const PCAN_PCIBUS8 As Byte = &H48 ' PCAN-PCI interface, channel 8
Public Const PCAN_USBBUS1 As Byte = &H51 ' PCAN-USB interface, channel 1
Public Const PCAN_USBBUS2 As Byte = &H52 ' PCAN-USB interface, channel 2
Public Const PCAN_USBBUS3 As Byte = &H53 ' PCAN-USB interface, channel 3
Public Const PCAN_USBBUS4 As Byte = &H54 ' PCAN-USB interface, channel 4
Public Const PCAN_USBBUS5 As Byte = &H55 ' PCAN-USB interface, channel 5
Public Const PCAN_USBBUS6 As Byte = &H56 ' PCAN-USB interface, channel 6
Public Const PCAN_USBBUS7 As Byte = &H57 ' PCAN-USB interface, channel 7
Public Const PCAN_USBBUS8 As Byte = &H58 ' PCAN-USB interface, channel 8
Public Const PCAN_PCCBUS1 As Byte = &H61 ' PCAN-PC Card interface, channel 1
Public Const PCAN_PCCBUS2 As Byte = &H62 ' PCAN-PC Card interface, channel 2
' Represent the PCAN error and status codes
'
Public Const PCAN_ERROR_OK As Long = &H0 ' No error
Public Const PCAN_ERROR_XMTFULL As Long = &H1 ' Transmit buffer in CAN controller is full
Public Const PCAN_ERROR_OVERRUN As Long = &H2 ' CAN controller was read too late
Public Const PCAN_ERROR_BUSLIGHT As Long = &H4 ' Bus error: an error counter reached the 'light' limit
Public Const PCAN_ERROR_BUSHEAVY As Long = &H8 ' Bus error: an error counter reached the 'heavy' limit
Public Const PCAN_ERROR_BUSOFF As Long = &H10 ' Bus error: the CAN controller is in bus-off state
Public Const PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT Or PCAN_ERROR_BUSHEAVY Or PCAN_ERROR_BUSOFF) ' Mask for all bus errors
Public Const PCAN_ERROR_QRCVEMPTY As Long = &H20 ' Receive queue is empty
Public Const PCAN_ERROR_QOVERRUN As Long = &H40 ' Receive queue was read too late
Public Const PCAN_ERROR_QXMTFULL As Long = &H80 ' Transmit queue is full
Public Const PCAN_ERROR_REGTEST As Long = &H100 ' Test of the CAN controller hardware registers failed (no hardware found)
Public Const PCAN_ERROR_NODRIVER As Long = &H200 ' Driver not loaded
Public Const PCAN_ERROR_HWINUSE As Long = &H400 ' Hardware already in use by a Net
Public Const PCAN_ERROR_NETINUSE As Long = &H800 ' A Client is already connected to the Net
Public Const PCAN_ERROR_ILLHW As Long = &H1400 ' Hardware handle is invalid
Public Const PCAN_ERROR_ILLNET As Long = &H1800 ' Net handle is invalid
Public Const PCAN_ERROR_ILLCLIENT As Long = &H1C00 ' Client handle is invalid
Public Const PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW Or PCAN_ERROR_ILLNET Or PCAN_ERROR_ILLCLIENT) ' Mask for all handle errors
Public Const PCAN_ERROR_RESOURCE As Long = &H2000 ' Resource (FIFO, Client, timeout) cannot be created
Public Const PCAN_ERROR_ILLPARAMTYPE As Long = &H4000 ' Invalid parameter
Public Const PCAN_ERROR_ILLPARAMVAL As Long = &H8000 ' Invalid parameter value
Public Const PCAN_ERROR_UNKNOWN As Long = &H10000 ' Unknow error
Public Const PCAN_ERROR_ILLDATA As Long = &H20000 ' Invalid data, function, or action
Public Const PCAN_ERROR_INITIALIZE As Long = &H40000 ' Channel is not initialized
' PCAN devices
'
Public Const PCAN_NONE = &H0 ' Undefined, unknown or not selected PCAN device value
Public Const PCAN_PEAKCAN = &H1 ' PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
Public Const PCAN_ISA = &H2 ' PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
Public Const PCAN_DNG = &H3 ' PCAN-Dongle
Public Const PCAN_PCI = &H4 ' PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
Public Const PCAN_USB = &H5 ' PCAN-USB and PCAN-USB Pro
Public Const PCAN_PCC = &H6 ' PCAN-PC Card
' PCAN parameters
'
Public Const PCAN_DEVICE_NUMBER = &H1 ' PCAN-USB device number parameter
Public Const PCAN_5VOLTS_POWER = &H2 ' PCAN-PC Card 5-Volt power parameter
Public Const PCAN_RECEIVE_EVENT = &H3 ' PCAN receive event handler parameter
Public Const PCAN_MESSAGE_FILTER = &H4 ' PCAN message filter parameter
Public Const PCAN_API_VERSION = &H5 ' PCAN-Basic API version parameter
Public Const PCAN_CHANNEL_VERSION = &H6 ' PCAN device channel version parameter
Public Const PCAN_BUSOFF_AUTORESET = &H7 ' PCAN Reset-On-Busoff parameter
Public Const PCAN_LISTEN_ONLY = &H8 ' PCAN Listen-Only parameter
Public Const PCAN_LOG_LOCATION = &H9 ' Directory path for trace files
Public Const PCAN_LOG_STATUS = &HA ' Debug-Trace activation status
Public Const PCAN_LOG_CONFIGURE = &HB ' Configuration of the debugged information (LOG_FUNCTION_***)
Public Const PCAN_LOG_TEXT = &HC ' Custom insertion of text into the log file
Public Const PCAN_CHANNEL_CONDITION = &HD ' Availability status of a PCAN-Channel
Public Const PCAN_HARDWARE_NAME = &HE ' PCAN hardware name parameter
Public Const PCAN_RECEIVE_STATUS = &HF ' Message reception status of a PCAN-Channel
Public Const PCAN_CONTROLLER_NUMBER = &H10 ' CAN-Controller number of a PCAN-Channel
' PCAN parameter values
'
Public Const PCAN_PARAMETER_OFF = &H0 ' The PCAN parameter is not set (inactive)
Public Const PCAN_PARAMETER_ON = &H1 ' The PCAN parameter is set (active)
Public Const PCAN_FILTER_CLOSE = &H0 ' The PCAN filter is closed. No messages will be received
Public Const PCAN_FILTER_OPEN = &H1 ' The PCAN filter is fully opened. All messages will be received
Public Const PCAN_FILTER_CUSTOM = &H2 ' The PCAN filter is custom configured. Only registered messages will be received
Public Const PCAN_CHANNEL_UNAVAILABLE = &H0 ' The PCAN-Channel handle is illegal, or its associated hadware is not available
Public Const PCAN_CHANNEL_AVAILABLE = &H1 ' The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
Public Const PCAN_CHANNEL_OCCUPIED = &H2 ' The PCAN-Channel handle is valid, and is already being used
Public Const LOG_FUNCTION_DEFAULT = &H0 ' Logs system exceptions / errors
Public Const LOG_FUNCTION_ENTRY = &H1 ' Logs the entries to the PCAN-Basic API functions
Public Const LOG_FUNCTION_PARAMETERS = &H2 ' Logs the parameters passed to the PCAN-Basic API functions
Public Const LOG_FUNCTION_LEAVE = &H4 ' Logs the exits from the PCAN-Basic API functions
Public Const LOG_FUNCTION_WRITE = &H8 ' Logs the CAN messages passed to the CAN_Write function
Public Const LOG_FUNCTION_READ = &H10 ' Logs the CAN messages received within the CAN_Read function
Public Const LOG_FUNCTION_ALL = &HFFFF ' Logs all possible information within the PCAN-Basic API functions
' PCAN message types
'
Public Const PCAN_MESSAGE_STANDARD = &H0 ' The PCAN message is a CAN Standard Frame (11-bit identifier)
Public Const PCAN_MESSAGE_RTR = &H1 ' The PCAN message is a CAN Remote-Transfer-Request Frame
Public Const PCAN_MESSAGE_EXTENDED = &H2 ' The PCAN message is a CAN Extended Frame (29-bit identifier)
Public Const PCAN_MESSAGE_STATUS = &H80 ' The PCAN message represents a PCAN status message
' Frame Type / Initialization Mode
'
Public Const PCAN_MODE_STANDARD = PCAN_MESSAGE_STANDARD
Public Const PCAN_MODE_EXTENDED = PCAN_MESSAGE_EXTENDED
' Baud rate codes = BTR0/BTR1 register values for the CAN controller.
' You can define your own Baud rate with the BTROBTR1 register.
' Take a look at www.peak-system.com for our free software "BAUDTOOL"
' to calculate the BTROBTR1 register for every baudrate and sample point.
'
Public Const PCAN_BAUD_1M = &H14 ' 1 MBit/s
Public Const PCAN_BAUD_800K = &H16 ' 800 kBit/s
Public Const PCAN_BAUD_500K = &H1C ' 500 kBit/s
Public Const PCAN_BAUD_250K = &H11C ' 250 kBit/s
Public Const PCAN_BAUD_125K = &H31C ' 125 kBit/s
Public Const PCAN_BAUD_100K = &H432F ' 100 kBit/s
Public Const PCAN_BAUD_95K = &HC34E ' 95,238 kBit/s
Public Const PCAN_BAUD_83K = &H4B14 ' 83,333 kBit/s
Public Const PCAN_BAUD_50K = &H472F ' 50 kBit/s
Public Const PCAN_BAUD_47K = &H1414 ' 47,619 kBit/s
Public Const PCAN_BAUD_33K = &H1D14 ' 33,333 kBit/s
Public Const PCAN_BAUD_20K = &H532F ' 20 kBit/s
Public Const PCAN_BAUD_10K = &H672F ' 10 kBit/s
Public Const PCAN_BAUD_5K = &H7F7F ' 5 kBit/s
Public Const PCAN_TYPE_ISA = &H1 ' PCAN-ISA 82C200
Public Const PCAN_TYPE_ISA_SJA = &H9 ' PCAN-ISA SJA1000
Public Const PCAN_TYPE_ISA_PHYTEC = &H4 ' PHYTEC ISA
Public Const PCAN_TYPE_DNG = &H2 ' PCAN-Dongle 82C200
Public Const PCAN_TYPE_DNG_EPP = &H3 ' PCAN-Dongle EPP 82C200
Public Const PCAN_TYPE_DNG_SJA = &H5 ' PCAN-Dongle SJA1000
Public Const PCAN_TYPE_DNG_SJA_EPP = &H6 ' PCAN-Dongle EPP SJA1000
' CAN message
Public Type TPCANMsg
ID As Long ' 11/29-bit message identifier
MsgType As Byte ' Type of the message
LEN As Byte ' Data Length Code of the message (0..8)
DATA(7) As Byte ' Data of the message (DATA[0]..DATA[7])
End Type
Public Type TPCANTimestamp
millis As Long ' base-value: milliseconds: 0.. 2^32-1
millis_overflow As Integer ' roll-arounds of millis
micros As Integer ' microseconds: 0..999
End Type
''' <summary>
''' Initializes a PCAN Channel
''' </summary>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <param name="Btr0Btr1">"The speed for the communication (BTR0BTR1 code)"</param>
''' <param name="HwType">"NON PLUG&PLAY: The type of hardware and operation mode"</param>
''' <param name="IOPort">"NON PLUG&PLAY: The I/O address for the parallel port"</param>
''' <param name="Interupt">"NON PLUG&PLAY: Interrupt number of the parallel port"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_Initialize Lib "PCANBasic.DLL" _
(ByVal channel As Byte, _
ByVal Btr0Btr1 As Integer, _
Optional ByVal HwType As Byte = 0, _
Optional ByVal IOPort As Long = 0, _
Optional ByVal Interrupt As Integer = 0) As Long
''' <summary>
''' Uninitializes one or all PCAN Channels initialized by CAN_Initialize
''' </summary>
''' <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
''' uninitialize all initialized channels</remarks>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_Uninitialize Lib "PCANBasic.DLL" _
(ByVal Channel As Byte) As Long
''' <summary>
''' Resets the receive and transmit queues of the PCAN Channel
''' </summary>
''' <remarks>
''' A reset of the CAN controller is not performed.
''' </remarks>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_Reset Lib "PCANBasic.DLL" _
(ByVal Channel As Byte) As Long
''' <summary>
''' Gets the current status of a PCAN Channel
''' </summary>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_GetStatus Lib "PCANBasic.DLL" _
(ByVal Channel As Byte) As Long
''' <summary>
''' Reads a CAN message from the receive queue of a PCAN Channel
''' </summary>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <param name="MessageBuffer">"A TPCANMsg structure buffer to store the CAN message"</param>
''' <param name="TimestampBuffer">"A TPCANTimestamp structure buffer to get
''' the reception time of the message. If this value is not desired, this parameter
''' should be passed as NULL"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_Read Lib "PCANBasic.DLL" _
(ByVal Channel As Byte, _
ByRef MessageBuffer As TPCANMsg, _
ByRef TimestampBuffer As TPCANTimestamp) As Long
''' <summary>
''' Transmits a CAN message
''' </summary>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <param name="MessageBuffer">"A TPCANMsg buffer with the message to be sent"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_Write Lib "PCANBasic.DLL" _
(ByVal Channel As Byte, _
ByRef MessageBuffer As TPCANMsg) As Long
''' <summary>
''' Configures the reception filter.
''' </summary>
''' <remarks>The message filter will be expanded with every call to
''' this function. If it is desired to reset the filter, please use
''' the CAN_SetValue function</remarks>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <param name="FromID">"The lowest CAN ID to be received"</param>
''' <param name="ToID">"The highest CAN ID to be received"</param>
''' <param name="Mode">"Message type, Standard (11-bit identifier) or
''' Extended (29-bit identifier)"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_FilterMessages Lib "PCANBasic.DLL" _
(ByVal Channel As Byte, _
ByVal FromID As Long, _
ByVal ToID As Long, _
ByVal Mode As Byte) As Long
''' <summary>
''' Retrieves a PCAN Channel value
''' </summary>
''' <remarks>Parameters can be present or not according with the kind
''' of Hardware (PCAN Channel) being used. If a parameter is not available,
''' a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <param name="Parameter">"The TPCANParameter parameter to get"</param>
''' <param name="Buffer">"Buffer for the parameter value"</param>
''' <param name="BufferLength">"Size in bytes of the buffer"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_GetValue Lib "PCANBasic.DLL" _
(ByVal Channel As Byte, _
ByVal Parameter As Byte, _
ByRef Buffer As Any, _
ByVal BufferLength As Long) As Long
''' <summary>
''' Configures or sets a PCAN Channel value
''' </summary>
''' <remarks>Parameters can be present or not according with the kind
''' of Hardware (PCAN Channel) being used. If a parameter is not available,
''' a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
''' <param name="Channel">"The handle of a PCAN Channel"</param>
''' <param name="Parameter">"The TPCANParameter parameter to set"</param>
''' <param name="Buffer">"Buffer with the value to be set"</param>
''' <param name="BufferLength">"Size in bytes of the buffer"</param>
''' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_SetValue Lib "PCANBasic.DLL" _
(ByVal Channel As Byte, _
ByVal Parameter As Byte, _
ByRef Buffer As Any, _
ByVal BufferLength As Long) As Long
' <summary>
' Returns a descriptive text of a given TPCANStatus error
' code, in any desired language
' </summary>
' <remarks>The current languages available for translation are:
' Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
' Italian (0x10) and French (0x0C)</remarks>
' <param name="Error">"A TPCANStatus error code"</param>
' <param name="Language">"Indicates a 'Primary language ID'"</param>
' <param name="Buffer">"Buffer for a null terminated char array"</param>
' <returns>"A TPCANStatus error code"</returns>
Public Declare Function CAN_GetErrorText Lib "PCANBasic.DLL" _
(ByVal ErrorCode As Long, _
ByVal Language As Integer, _
ByVal Buffer As String) As Long

850
PCANBasic/PCANBasic.cs Normal file
View file

@ -0,0 +1,850 @@
// PCANBasic.cs
//
// ~~~~~~~~~~~~
//
// PCAN-Basic API
//
// ~~~~~~~~~~~~
//
// ------------------------------------------------------------------
// Author : Keneth Wagner
// Last change: 08.11.2011 Wagner
//
// Language: C# 1.0
// ------------------------------------------------------------------
//
// Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
// more Info at http://www.peak-system.com
//
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Peak.Can.Basic
{
using TPCANHandle = System.Byte;
#region Enumerations
/// <summary>
/// Represents a PCAN status/error code
/// </summary>
[Flags]
public enum TPCANStatus : uint
{
/// <summary>
/// No error
/// </summary>
PCAN_ERROR_OK = 0x00000,
/// <summary>
/// Transmit buffer in CAN controller is full
/// </summary>
PCAN_ERROR_XMTFULL = 0x00001,
/// <summary>
/// CAN controller was read too late
/// </summary>
PCAN_ERROR_OVERRUN = 0x00002,
/// <summary>
/// Bus error: an error counter reached the 'light' limit
/// </summary>
PCAN_ERROR_BUSLIGHT = 0x00004,
/// <summary>
/// Bus error: an error counter reached the 'heavy' limit
/// </summary>
PCAN_ERROR_BUSHEAVY = 0x00008,
/// <summary>
/// Bus error: the CAN controller is in bus-off state
/// </summary>
PCAN_ERROR_BUSOFF = 0x00010,
/// <summary>
/// Mask for all bus errors
/// </summary>
PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF),
/// <summary>
/// Receive queue is empty
/// </summary>
PCAN_ERROR_QRCVEMPTY = 0x00020,
/// <summary>
/// Receive queue was read too late
/// </summary>
PCAN_ERROR_QOVERRUN = 0x00040,
/// <summary>
/// Transmit queue is full
/// </summary>
PCAN_ERROR_QXMTFULL = 0x00080,
/// <summary>
/// Test of the CAN controller hardware registers failed (no hardware found)
/// </summary>
PCAN_ERROR_REGTEST = 0x00100,
/// <summary>
/// Driver not loaded
/// </summary>
PCAN_ERROR_NODRIVER = 0x00200,
/// <summary>
/// Hardware already in use by a Net
/// </summary>
PCAN_ERROR_HWINUSE = 0x00400,
/// <summary>
/// A Client is already connected to the Net
/// </summary>
PCAN_ERROR_NETINUSE = 0x00800,
/// <summary>
/// Hardware handle is invalid
/// </summary>
PCAN_ERROR_ILLHW = 0x01400,
/// <summary>
/// Net handle is invalid
/// </summary>
PCAN_ERROR_ILLNET = 0x01800,
/// <summary>
/// Client handle is invalid
/// </summary>
PCAN_ERROR_ILLCLIENT = 0x01C00,
/// <summary>
/// Mask for all handle errors
/// </summary>
PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT),
/// <summary>
/// Resource (FIFO, Client, timeout) cannot be created
/// </summary>
PCAN_ERROR_RESOURCE = 0x02000,
/// <summary>
/// Invalid parameter
/// </summary>
PCAN_ERROR_ILLPARAMTYPE = 0x04000,
/// <summary>
/// Invalid parameter value
/// </summary>
PCAN_ERROR_ILLPARAMVAL = 0x08000,
/// <summary>
/// Unknow error
/// </summary>
PCAN_ERROR_UNKNOWN = 0x10000,
/// <summary>
/// Invalid data, function, or action.
/// </summary>
PCAN_ERROR_ILLDATA = 0x20000,
/// <summary>
/// Channel is not initialized
/// </summary>
PCAN_ERROR_INITIALIZE = 0x40000,
}
/// <summary>
/// Represents a PCAN device
/// </summary>
public enum TPCANDevice : byte
{
/// <summary>
/// Undefined, unknown or not selected PCAN device value
/// </summary>
PCAN_NONE = 0,
/// <summary>
/// PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
/// </summary>
PCAN_PEAKCAN = 1,
/// <summary>
/// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
/// </summary>
PCAN_ISA = 2,
/// <summary>
/// PCAN-Dongle
/// </summary>
PCAN_DNG = 3,
/// <summary>
/// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
/// </summary>
PCAN_PCI = 4,
/// <summary>
/// PCAN-USB and PCAN-USB Pro
/// </summary>
PCAN_USB = 5,
/// <summary>
/// PCAN-PC Card
/// </summary>
PCAN_PCC = 6
}
/// <summary>
/// Represents a PCAN parameter to be read or set
/// </summary>
public enum TPCANParameter : byte
{
/// <summary>
/// PCAN-USB device number parameter
/// </summary>
PCAN_DEVICE_NUMBER = 1,
/// <summary>
/// PCAN-PC Card 5-Volt power parameter
/// </summary>
PCAN_5VOLTS_POWER = 2,
/// <summary>
/// PCAN receive event handler parameter
/// </summary>
PCAN_RECEIVE_EVENT = 3,
/// <summary>
/// PCAN message filter parameter
/// </summary>
PCAN_MESSAGE_FILTER = 4,
/// <summary>
/// PCAN-Basic API version parameter
/// </summary>
PCAN_API_VERSION = 5,
/// <summary>
/// PCAN device channel version parameter
/// </summary>
PCAN_CHANNEL_VERSION = 6,
/// <summary>
/// PCAN Reset-On-Busoff parameter
/// </summary>
PCAN_BUSOFF_AUTORESET = 7,
/// <summary>
/// PCAN Listen-Only parameter
/// </summary>
PCAN_LISTEN_ONLY = 8,
/// <summary>
/// Directory path for trace files
/// </summary>
PCAN_LOG_LOCATION = 9,
/// <summary>
/// Debug-Trace activation status
/// </summary>
PCAN_LOG_STATUS = 10,
/// <summary>
/// Configuration of the debugged information (LOG_FUNCTION_***)
/// </summary>
PCAN_LOG_CONFIGURE = 11,
/// <summary>
/// Custom insertion of text into the log file
/// </summary>
PCAN_LOG_TEXT = 12,
/// <summary>
/// Availability status of a PCAN-Channel
/// </summary>
PCAN_CHANNEL_CONDITION = 13,
/// <summary>
/// PCAN hardware name parameter
/// </summary>
PCAN_HARDWARE_NAME = 14,
/// <summary>
/// Message reception status of a PCAN-Channel
/// </summary>
PCAN_RECEIVE_STATUS = 15,
/// <summary>
/// CAN-Controller number of a PCAN-Channel
/// </summary>
PCAN_CONTROLLER_NUMBER = 16,
}
/// <summary>
/// Represents the type of a PCAN message
/// </summary>
[Flags]
public enum TPCANMessageType : byte
{
/// <summary>
/// The PCAN message is a CAN Standard Frame (11-bit identifier)
/// </summary>
PCAN_MESSAGE_STANDARD = 0x00,
/// <summary>
/// The PCAN message is a CAN Remote-Transfer-Request Frame
/// </summary>
PCAN_MESSAGE_RTR = 0x01,
/// <summary>
/// The PCAN message is a CAN Extended Frame (29-bit identifier)
/// </summary>
PCAN_MESSAGE_EXTENDED = 0x02,
/// <summary>
/// The PCAN message represents a PCAN status message
/// </summary>
PCAN_MESSAGE_STATUS = 0x80,
}
/// <summary>
/// Represents a PCAN filter mode
/// </summary>
public enum TPCANMode : byte
{
/// <summary>
/// Mode is Standard (11-bit identifier)
/// </summary>
PCAN_MODE_STANDARD = TPCANMessageType.PCAN_MESSAGE_STANDARD,
/// <summary>
/// Mode is Extended (29-bit identifier)
/// </summary>
PCAN_MODE_EXTENDED = TPCANMessageType.PCAN_MESSAGE_EXTENDED,
}
/// <summary>
/// Represents a PCAN Baud rate register value
/// </summary>
public enum TPCANBaudrate : ushort
{
/// <summary>
/// 1 MBit/s
/// </summary>
PCAN_BAUD_1M = 0x0014,
/// <summary>
/// 800 KBit/s
/// </summary>
PCAN_BAUD_800K = 0x0016,
/// <summary>
/// 500 kBit/s
/// </summary>
PCAN_BAUD_500K = 0x001C,
/// <summary>
/// 250 kBit/s
/// </summary>
PCAN_BAUD_250K = 0x011C,
/// <summary>
/// 125 kBit/s
/// </summary>
PCAN_BAUD_125K = 0x031C,
/// <summary>
/// 100 kBit/s
/// </summary>
PCAN_BAUD_100K = 0x432F,
/// <summary>
/// 95,238 KBit/s
/// </summary>
PCAN_BAUD_95K = 0xC34E,
/// <summary>
/// 83,333 KBit/s
/// </summary>
PCAN_BAUD_83K = 0x4B14,
/// <summary>
/// 50 kBit/s
/// </summary>
PCAN_BAUD_50K = 0x472F,
/// <summary>
/// 47,619 KBit/s
/// </summary>
PCAN_BAUD_47K = 0x1414,
/// <summary>
/// 33,333 KBit/s
/// </summary>
PCAN_BAUD_33K = 0x1D14,
/// <summary>
/// 20 kBit/s
/// </summary>
PCAN_BAUD_20K = 0x532F,
/// <summary>
/// 10 kBit/s
/// </summary>
PCAN_BAUD_10K = 0x672F,
/// <summary>
/// 5 kBit/s
/// </summary>
PCAN_BAUD_5K = 0x7F7F,
}
/// <summary>
/// Represents the type of PCAN (non plug&play) hardware to be initialized
/// </summary>
public enum TPCANType : byte
{
/// <summary>
/// PCAN-ISA 82C200
/// </summary>
PCAN_TYPE_ISA = 0x01,
/// <summary>
/// PCAN-ISA SJA1000
/// </summary>
PCAN_TYPE_ISA_SJA = 0x09,
/// <summary>
/// PHYTEC ISA
/// </summary>
PCAN_TYPE_ISA_PHYTEC = 0x04,
/// <summary>
/// PCAN-Dongle 82C200
/// </summary>
PCAN_TYPE_DNG = 0x02,
/// <summary>
/// PCAN-Dongle EPP 82C200
/// </summary>
PCAN_TYPE_DNG_EPP = 0x03,
/// <summary>
/// PCAN-Dongle SJA1000
/// </summary>
PCAN_TYPE_DNG_SJA = 0x05,
/// <summary>
/// PCAN-Dongle EPP SJA1000
/// </summary>
PCAN_TYPE_DNG_SJA_EPP = 0x06,
}
#endregion
#region Structures
/// <summary>
/// Represents a PCAN message
/// </summary>
public struct TPCANMsg
{
/// <summary>
/// 11/29-bit message identifier
/// </summary>
public uint ID;
/// <summary>
/// Type of the message
/// </summary>
[MarshalAs(UnmanagedType.U1)]
public TPCANMessageType MSGTYPE;
/// <summary>
/// Data Length Code of the message (0..8)
/// </summary>
public byte LEN;
/// <summary>
/// Data of the message (DATA[0]..DATA[7])
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] DATA;
}
/// <summary>
/// Represents a timestamp of a received PCAN message.
/// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
/// </summary>
public struct TPCANTimestamp
{
/// <summary>
/// Base-value: milliseconds: 0.. 2^32-1
/// </summary>
public uint millis;
/// <summary>
/// Roll-arounds of millis
/// </summary>
public ushort millis_overflow;
/// <summary>
/// Microseconds: 0..999
/// </summary>
public ushort micros;
}
#endregion
#region PCANBasic class
/// <summary>
/// PCAN-Basic API class implementation
/// </summary>
public static class PCANBasic
{
#region PCAN-BUS Handles Definition
/// <summary>
/// Undefined/default value for a PCAN bus
/// </summary>
public const TPCANHandle PCAN_NONEBUS = 0x00;
/// <summary>
/// PCAN-ISA interface, channel 1
/// </summary>
public const TPCANHandle PCAN_ISABUS1 = 0x21;
/// <summary>
/// PCAN-ISA interface, channel 2
/// </summary>
public const TPCANHandle PCAN_ISABUS2 = 0x22;
/// <summary>
/// PCAN-ISA interface, channel 3
/// </summary>
public const TPCANHandle PCAN_ISABUS3 = 0x23;
/// <summary>
/// PCAN-ISA interface, channel 4
/// </summary>
public const TPCANHandle PCAN_ISABUS4 = 0x24;
/// <summary>
/// PCAN-ISA interface, channel 5
/// </summary>
public const TPCANHandle PCAN_ISABUS5 = 0x25;
/// <summary>
/// PCAN-ISA interface, channel 6
/// </summary>
public const TPCANHandle PCAN_ISABUS6 = 0x26;
/// <summary>
/// PCAN-ISA interface, channel 7
/// </summary>
public const TPCANHandle PCAN_ISABUS7 = 0x27;
/// <summary>
/// PCAN-ISA interface, channel 8
/// </summary>
public const TPCANHandle PCAN_ISABUS8 = 0x28;
/// <summary>
/// PPCAN-Dongle/LPT interface, channel 1
/// </summary>
public const TPCANHandle PCAN_DNGBUS1 = 0x31;
/// <summary>
/// PCAN-PCI interface, channel 1
/// </summary>
public const TPCANHandle PCAN_PCIBUS1 = 0x41;
/// <summary>
/// PCAN-PCI interface, channel 2
/// </summary>
public const TPCANHandle PCAN_PCIBUS2 = 0x42;
/// <summary>
/// PCAN-PCI interface, channel 3
/// </summary>
public const TPCANHandle PCAN_PCIBUS3 = 0x43;
/// <summary>
/// PCAN-PCI interface, channel 4
/// </summary>
public const TPCANHandle PCAN_PCIBUS4 = 0x44;
/// <summary>
/// PCAN-PCI interface, channel 5
/// </summary>
public const TPCANHandle PCAN_PCIBUS5 = 0x45;
/// <summary>
/// PCAN-PCI interface, channel 6
/// </summary>
public const TPCANHandle PCAN_PCIBUS6 = 0x46;
/// <summary>
/// PCAN-PCI interface, channel 7
/// </summary>
public const TPCANHandle PCAN_PCIBUS7 = 0x47;
/// <summary>
/// PCAN-PCI interface, channel 8
/// </summary>
public const TPCANHandle PCAN_PCIBUS8 = 0x48;
/// <summary>
/// PCAN-USB interface, channel 1
/// </summary>
public const TPCANHandle PCAN_USBBUS1 = 0x51;
/// <summary>
/// PCAN-USB interface, channel 2
/// </summary>
public const TPCANHandle PCAN_USBBUS2 = 0x52;
/// <summary>
/// PCAN-USB interface, channel 3
/// </summary>
public const TPCANHandle PCAN_USBBUS3 = 0x53;
/// <summary>
/// PCAN-USB interface, channel 4
/// </summary>
public const TPCANHandle PCAN_USBBUS4 = 0x54;
/// <summary>
/// PCAN-USB interface, channel 5
/// </summary>
public const TPCANHandle PCAN_USBBUS5 = 0x55;
/// <summary>
/// PCAN-USB interface, channel 6
/// </summary>
public const TPCANHandle PCAN_USBBUS6 = 0x56;
/// <summary>
/// PCAN-USB interface, channel 7
/// </summary>
public const TPCANHandle PCAN_USBBUS7 = 0x57;
/// <summary>
/// PCAN-USB interface, channel 8
/// </summary>
public const TPCANHandle PCAN_USBBUS8 = 0x58;
/// <summary>
/// PCAN-PC Card interface, channel 1
/// </summary>
public const TPCANHandle PCAN_PCCBUS1 = 0x61;
/// <summary>
/// PCAN-PC Card interface, channel 2
/// </summary>
public const TPCANHandle PCAN_PCCBUS2 = 0x62;
#endregion
#region Parameter values definition
/// <summary>
/// The PCAN parameter is not set (inactive)
/// </summary>
public const int PCAN_PARAMETER_OFF = 0;
/// <summary>
/// The PCAN parameter is set (active)
/// </summary>
public const int PCAN_PARAMETER_ON = 1;
/// <summary>
/// The PCAN filter is closed. No messages will be received
/// </summary>
public const int PCAN_FILTER_CLOSE = 0;
/// <summary>
/// The PCAN filter is fully opened. All messages will be received
/// </summary>
public const int PCAN_FILTER_OPEN = 1;
/// <summary>
/// The PCAN filter is custom configured. Only registered
/// messages will be received
/// </summary>
public const int PCAN_FILTER_CUSTOM = 2;
/// <summary>
/// The PCAN-Channel handle is illegal, or its associated hadware is not available
/// </summary>
public const int PCAN_CHANNEL_UNAVAILABLE = 0;
/// <summary>
/// The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
/// </summary>
public const int PCAN_CHANNEL_AVAILABLE = 1;
/// <summary>
/// The PCAN-Channel handle is valid, and is already being used
/// </summary>
public const int PCAN_CHANNEL_OCCUPIED = 2;
/// <summary>
/// Logs system exceptions / errors
/// </summary>
public const int LOG_FUNCTION_DEFAULT = 0x00;
/// <summary>
/// Logs the entries to the PCAN-Basic API functions
/// </summary>
public const int LOG_FUNCTION_ENTRY = 0x01;
/// <summary>
/// Logs the parameters passed to the PCAN-Basic API functions
/// </summary>
public const int LOG_FUNCTION_PARAMETERS = 0x02;
/// <summary>
/// Logs the exits from the PCAN-Basic API functions
/// </summary>
public const int LOG_FUNCTION_LEAVE = 0x04;
/// <summary>
/// Logs the CAN messages passed to the CAN_Write function
/// </summary>
public const int LOG_FUNCTION_WRITE = 0x08;
/// <summary>
/// Logs the CAN messages received within the CAN_Read function
/// </summary>
public const int LOG_FUNCTION_READ = 0x10;
/// <summary>
/// Logs all possible information within the PCAN-Basic API functions
/// </summary>
public const int LOG_FUNCTION_ALL = 0xFFFF;
#endregion
#region PCANBasic API Implementation
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
/// <param name="HwType">NON PLUG&PLAY: The type of hardware and operation mode</param>
/// <param name="IOPort">NON PLUG&PLAY: The I/O address for the parallel port</param>
/// <param name="Interrupt">NON PLUG&PLAY: Interrupt number of the parallel por</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")]
public static extern TPCANStatus Initialize(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType.U2)]
TPCANBaudrate Btr0Btr1,
[MarshalAs(UnmanagedType.U1)]
TPCANType HwType,
UInt32 IOPort,
UInt16 Interrupt);
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
/// <returns>A TPCANStatus error code</returns>
public static TPCANStatus Initialize(
TPCANHandle Channel,
TPCANBaudrate Btr0Btr1)
{
return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0);
}
/// <summary>
/// Uninitializes one or all PCAN Channels initialized by CAN_Initialize
/// </summary>
/// <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
/// uninitialize all initialized channels</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Uninitialize")]
public static extern TPCANStatus Uninitialize(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel);
/// <summary>
/// Resets the receive and transmit queues of the PCAN Channel
/// </summary>
/// <remarks>A reset of the CAN controller is not performed</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Reset")]
public static extern TPCANStatus Reset(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel);
/// <summary>
/// Gets the current status of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetStatus")]
public static extern TPCANStatus GetStatus(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel);
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
/// <param name="TimestampBuffer">A TPCANTimestamp structure buffer to get
/// the reception time of the message</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
public static extern TPCANStatus Read(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
out TPCANMsg MessageBuffer,
out TPCANTimestamp TimestampBuffer);
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
private static extern TPCANStatus Read(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
out TPCANMsg MessageBuffer,
IntPtr bufferPointer);
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
/// <returns>A TPCANStatus error code</returns>
public static TPCANStatus Read(
TPCANHandle Channel,
out TPCANMsg MessageBuffer)
{
return Read(Channel, out MessageBuffer, IntPtr.Zero);
}
/// <summary>
/// Transmits a CAN message
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg buffer with the message to be sent</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")]
public static extern TPCANStatus Write(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
ref TPCANMsg MessageBuffer);
/// <summary>
/// Configures the reception filter
/// </summary>
/// <remarks>The message filter will be expanded with every call to
/// this function. If it is desired to reset the filter, please use
/// the 'SetValue' function</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="FromID">The lowest CAN ID to be received</param>
/// <param name="ToID">The highest CAN ID to be received</param>
/// <param name="Mode">Message type, Standard (11-bit identifier) or
/// Extended (29-bit identifier)</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_FilterMessages")]
public static extern TPCANStatus FilterMessages(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
UInt32 FromID,
UInt32 ToID,
[MarshalAs(UnmanagedType.U1)]
TPCANMode Mode);
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to get</param>
/// <param name="StringBuffer">Buffer for the parameter value</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
public static extern TPCANStatus GetValue(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType.U1)]
TPCANParameter Parameter,
StringBuilder StringBuffer,
UInt32 BufferLength);
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to get</param>
/// <param name="NumericBuffer">Buffer for the parameter value</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
public static extern TPCANStatus GetValue(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType.U1)]
TPCANParameter Parameter,
out UInt32 NumericBuffer,
UInt32 BufferLength);
/// <summary>
/// Configures or sets a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to set</param>
/// <param name="NumericBuffer">Buffer with the value to be set</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
public static extern TPCANStatus SetValue(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType.U1)]
TPCANParameter Parameter,
ref UInt32 NumericBuffer,
UInt32 BufferLength);
/// <summary>
/// Configures or sets a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter"></param>
/// <param name="StringBuffer">Buffer with the value to be set</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
public static extern TPCANStatus SetValue(
[MarshalAs(UnmanagedType.U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType.U1)]
TPCANParameter Parameter,
[MarshalAs(UnmanagedType.LPStr,SizeParamIndex=3)]
string StringBuffer,
UInt32 BufferLength);
/// <summary>
/// Returns a descriptive text of a given TPCANStatus error
/// code, in any desired language
/// </summary>
/// <remarks>The current languages available for translation are:
/// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
/// Italian (0x10) and French (0x0C)</remarks>
/// <param name="Error">A TPCANStatus error code</param>
/// <param name="Language">Indicates a 'Primary language ID'</param>
/// <param name="StringBuffer">Buffer for the text (must be at least 256 in length)</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetErrorText")]
public static extern TPCANStatus GetErrorText(
[MarshalAs(UnmanagedType.U4)]
TPCANStatus Error,
UInt16 Language,
StringBuilder StringBuffer);
#endregion
}
#endregion
}

BIN
PCANBasic/PCANBasic.dll Normal file

Binary file not shown.

373
PCANBasic/PCANBasic.h Normal file
View file

@ -0,0 +1,373 @@
// PCANBasic.h
//
// ~~~~~~~~~~~~
//
// PCAN-Basic API
//
// ~~~~~~~~~~~~
//
// ------------------------------------------------------------------
// Author : Keneth Wagner
// Last change: 08.11.2011 Wagner
//
// Language: ANSI-C
// ------------------------------------------------------------------
//
// Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
// more Info at http://www.peak-system.com
//
#ifndef __PCANBASICH__
#define __PCANBASICH__
////////////////////////////////////////////////////////////
// Value definitions
////////////////////////////////////////////////////////////
// Currently defined and supported PCAN channels
//
#define PCAN_NONEBUS 0x00 // Undefined/default value for a PCAN bus
#define PCAN_ISABUS1 0x21 // PCAN-ISA interface, channel 1
#define PCAN_ISABUS2 0x22 // PCAN-ISA interface, channel 2
#define PCAN_ISABUS3 0x23 // PCAN-ISA interface, channel 3
#define PCAN_ISABUS4 0x24 // PCAN-ISA interface, channel 4
#define PCAN_ISABUS5 0x25 // PCAN-ISA interface, channel 5
#define PCAN_ISABUS6 0x26 // PCAN-ISA interface, channel 6
#define PCAN_ISABUS7 0x27 // PCAN-ISA interface, channel 7
#define PCAN_ISABUS8 0x28 // PCAN-ISA interface, channel 8
#define PCAN_DNGBUS1 0x31 // PCAN-Dongle/LPT interface, channel 1
#define PCAN_PCIBUS1 0x41 // PCAN-PCI interface, channel 1
#define PCAN_PCIBUS2 0x42 // PCAN-PCI interface, channel 2
#define PCAN_PCIBUS3 0x43 // PCAN-PCI interface, channel 3
#define PCAN_PCIBUS4 0x44 // PCAN-PCI interface, channel 4
#define PCAN_PCIBUS5 0x45 // PCAN-PCI interface, channel 5
#define PCAN_PCIBUS6 0x46 // PCAN-PCI interface, channel 6
#define PCAN_PCIBUS7 0x47 // PCAN-PCI interface, channel 7
#define PCAN_PCIBUS8 0x48 // PCAN-PCI interface, channel 8
#define PCAN_USBBUS1 0x51 // PCAN-USB interface, channel 1
#define PCAN_USBBUS2 0x52 // PCAN-USB interface, channel 2
#define PCAN_USBBUS3 0x53 // PCAN-USB interface, channel 3
#define PCAN_USBBUS4 0x54 // PCAN-USB interface, channel 4
#define PCAN_USBBUS5 0x55 // PCAN-USB interface, channel 5
#define PCAN_USBBUS6 0x56 // PCAN-USB interface, channel 6
#define PCAN_USBBUS7 0x57 // PCAN-USB interface, channel 7
#define PCAN_USBBUS8 0x58 // PCAN-USB interface, channel 8
#define PCAN_PCCBUS1 0x61 // PCAN-PC Card interface, channel 1
#define PCAN_PCCBUS2 0x62 // PCAN-PC Card interface, channel 2
// Represent the PCAN error and status codes
//
#define PCAN_ERROR_OK 0x00000 // No error
#define PCAN_ERROR_XMTFULL 0x00001 // Transmit buffer in CAN controller is full
#define PCAN_ERROR_OVERRUN 0x00002 // CAN controller was read too late
#define PCAN_ERROR_BUSLIGHT 0x00004 // Bus error: an error counter reached the 'light' limit
#define PCAN_ERROR_BUSHEAVY 0x00008 // Bus error: an error counter reached the 'heavy' limit
#define PCAN_ERROR_BUSOFF 0x00010 // Bus error: the CAN controller is in bus-off state
#define PCAN_ERROR_ANYBUSERR (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF) // Mask for all bus errors
#define PCAN_ERROR_QRCVEMPTY 0x00020 // Receive queue is empty
#define PCAN_ERROR_QOVERRUN 0x00040 // Receive queue was read too late
#define PCAN_ERROR_QXMTFULL 0x00080 // Transmit queue is full
#define PCAN_ERROR_REGTEST 0x00100 // Test of the CAN controller hardware registers failed (no hardware found)
#define PCAN_ERROR_NODRIVER 0x00200 // Driver not loaded
#define PCAN_ERROR_HWINUSE 0x00400 // Hardware already in use by a Net
#define PCAN_ERROR_NETINUSE 0x00800 // A Client is already connected to the Net
#define PCAN_ERROR_ILLHW 0x01400 // Hardware handle is invalid
#define PCAN_ERROR_ILLNET 0x01800 // Net handle is invalid
#define PCAN_ERROR_ILLCLIENT 0x01C00 // Client handle is invalid
#define PCAN_ERROR_ILLHANDLE (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT) // Mask for all handle errors
#define PCAN_ERROR_RESOURCE 0x02000 // Resource (FIFO, Client, timeout) cannot be created
#define PCAN_ERROR_ILLPARAMTYPE 0x04000 // Invalid parameter
#define PCAN_ERROR_ILLPARAMVAL 0x08000 // Invalid parameter value
#define PCAN_ERROR_UNKNOWN 0x10000 // Unknow error
#define PCAN_ERROR_ILLDATA 0x20000 // Invalid data, function, or action.
#define PCAN_ERROR_INITIALIZE 0x40000 // Channel is not initialized
// PCAN devices
//
#define PCAN_NONE 0x00 // Undefined, unknown or not selected PCAN device value
#define PCAN_PEAKCAN 0x01 // PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
#define PCAN_ISA 0x02 // PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
#define PCAN_DNG 0x03 // PCAN-Dongle
#define PCAN_PCI 0x04 // PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
#define PCAN_USB 0x05 // PCAN-USB and PCAN-USB Pro
#define PCAN_PCC 0x06 // PCAN-PC Card
// PCAN parameters
//
#define PCAN_DEVICE_NUMBER 0x01 // PCAN-USB device number parameter
#define PCAN_5VOLTS_POWER 0x02 // PCAN-PC Card 5-Volt power parameter
#define PCAN_RECEIVE_EVENT 0x03 // PCAN receive event handler parameter
#define PCAN_MESSAGE_FILTER 0x04 // PCAN message filter parameter
#define PCAN_API_VERSION 0x05 // PCAN-Basic API version parameter
#define PCAN_CHANNEL_VERSION 0x06 // PCAN device channel version parameter
#define PCAN_BUSOFF_AUTORESET 0x07 // PCAN Reset-On-Busoff parameter
#define PCAN_LISTEN_ONLY 0x08 // PCAN Listen-Only parameter
#define PCAN_LOG_LOCATION 0x09 // Directory path for trace files
#define PCAN_LOG_STATUS 0x0A // Debug-Trace activation status
#define PCAN_LOG_CONFIGURE 0x0B // Configuration of the debugged information (LOG_FUNCTION_***)
#define PCAN_LOG_TEXT 0x0C // Custom insertion of text into the log file
#define PCAN_CHANNEL_CONDITION 0x0D // Availability status of a PCAN-Channel
#define PCAN_HARDWARE_NAME 0x0E // PCAN hardware name parameter
#define PCAN_RECEIVE_STATUS 0x0F // Message reception status of a PCAN-Channel
#define PCAN_CONTROLLER_NUMBER 0x10 // CAN-Controller number of a PCAN-Channel
// PCAN parameter values
//
#define PCAN_PARAMETER_OFF 0x00 // The PCAN parameter is not set (inactive)
#define PCAN_PARAMETER_ON 0x01 // The PCAN parameter is set (active)
#define PCAN_FILTER_CLOSE 0x00 // The PCAN filter is closed. No messages will be received
#define PCAN_FILTER_OPEN 0x01 // The PCAN filter is fully opened. All messages will be received
#define PCAN_FILTER_CUSTOM 0x02 // The PCAN filter is custom configured. Only registered messages will be received
#define PCAN_CHANNEL_UNAVAILABLE 0x00 // The PCAN-Channel handle is illegal, or its associated hadware is not available
#define PCAN_CHANNEL_AVAILABLE 0x01 // The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
#define PCAN_CHANNEL_OCCUPIED 0x02 // The PCAN-Channel handle is valid, and is already being used
#define LOG_FUNCTION_DEFAULT 0x00 // Logs system exceptions / errors
#define LOG_FUNCTION_ENTRY 0x01 // Logs the entries to the PCAN-Basic API functions
#define LOG_FUNCTION_PARAMETERS 0x02 // Logs the parameters passed to the PCAN-Basic API functions
#define LOG_FUNCTION_LEAVE 0x04 // Logs the exits from the PCAN-Basic API functions
#define LOG_FUNCTION_WRITE 0x08 // Logs the CAN messages passed to the CAN_Write function
#define LOG_FUNCTION_READ 0x10 // Logs the CAN messages received within the CAN_Read function
#define LOG_FUNCTION_ALL 0xFFFF // Logs all possible information within the PCAN-Basic API functions
// PCAN message types
//
#define PCAN_MESSAGE_STANDARD 0x00 // The PCAN message is a CAN Standard Frame (11-bit identifier)
#define PCAN_MESSAGE_RTR 0x01 // The PCAN message is a CAN Remote-Transfer-Request Frame
#define PCAN_MESSAGE_EXTENDED 0x02 // The PCAN message is a CAN Extended Frame (29-bit identifier)
#define PCAN_MESSAGE_STATUS 0x80 // The PCAN message represents a PCAN status message
// Frame Type / Initialization Mode
//
#define PCAN_MODE_STANDARD PCAN_MESSAGE_STANDARD
#define PCAN_MODE_EXTENDED PCAN_MESSAGE_EXTENDED
// Baud rate codes = BTR0/BTR1 register values for the CAN controller.
// You can define your own Baud rate with the BTROBTR1 register.
// Take a look at www.peak-system.com for our free software "BAUDTOOL"
// to calculate the BTROBTR1 register for every baudrate and sample point.
//
#define PCAN_BAUD_1M 0x0014 // 1 MBit/s
#define PCAN_BAUD_800K 0x0016 // 800 kBit/s
#define PCAN_BAUD_500K 0x001C // 500 kBit/s
#define PCAN_BAUD_250K 0x011C // 250 kBit/s
#define PCAN_BAUD_125K 0x031C // 125 kBit/s
#define PCAN_BAUD_100K 0x432F // 100 kBit/s
#define PCAN_BAUD_95K 0xC34E // 95,238 kBit/s
#define PCAN_BAUD_83K 0x4B14 // 83,333 kBit/s
#define PCAN_BAUD_50K 0x472F // 50 kBit/s
#define PCAN_BAUD_47K 0x1414 // 47,619 kBit/s
#define PCAN_BAUD_33K 0x1D14 // 33,333 kBit/s
#define PCAN_BAUD_20K 0x532F // 20 kBit/s
#define PCAN_BAUD_10K 0x672F // 10 kBit/s
#define PCAN_BAUD_5K 0x7F7F // 5 kBit/s
#define PCAN_TYPE_ISA 0x01 // PCAN-ISA 82C200
#define PCAN_TYPE_ISA_SJA 0x09 // PCAN-ISA SJA1000
#define PCAN_TYPE_ISA_PHYTEC 0x04 // PHYTEC ISA
#define PCAN_TYPE_DNG 0x02 // PCAN-Dongle 82C200
#define PCAN_TYPE_DNG_EPP 0x03 // PCAN-Dongle EPP 82C200
#define PCAN_TYPE_DNG_SJA 0x05 // PCAN-Dongle SJA1000
#define PCAN_TYPE_DNG_SJA_EPP 0x06 // PCAN-Dongle EPP SJA1000
////////////////////////////////////////////////////////////
// Type definitions
////////////////////////////////////////////////////////////
#define TPCANHandle BYTE // Represents a PCAN hardware channel handle
#define TPCANStatus DWORD // Represents a PCAN status/error code
#define TPCANParameter BYTE // Represents a PCAN parameter to be read or set
#define TPCANDevice BYTE // Represents a PCAN device
#define TPCANMessageType BYTE // Represents the type of a PCAN message
#define TPCANType BYTE // Represents the type of PCAN hardware to be initialized
#define TPCANMode BYTE // Represents a PCAN filter mode
#define TPCANBaudrate WORD // Represents a PCAN Baud rate register value
////////////////////////////////////////////////////////////
// Structure definitions
////////////////////////////////////////////////////////////
// Represents a PCAN message
//
typedef struct tagTPCANMsg
{
DWORD ID; // 11/29-bit message identifier
TPCANMessageType MSGTYPE; // Type of the message
BYTE LEN; // Data Length Code of the message (0..8)
BYTE DATA[8]; // Data of the message (DATA[0]..DATA[7])
} TPCANMsg;
// Represents a timestamp of a received PCAN message
// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
//
typedef struct tagTPCANTimestamp
{
DWORD millis; // Base-value: milliseconds: 0.. 2^32-1
WORD millis_overflow; // Roll-arounds of millis
WORD micros; // Microseconds: 0..999
} TPCANTimestamp;
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////////////////////////////////
// PCAN-Basic API function declarations
////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="Btr0Btr1">"The speed for the communication (BTR0BTR1 code)"</param>
/// <param name="HwType">"NON PLUG&PLAY: The type of hardware and operation mode"</param>
/// <param name="IOPort">"NON PLUG&PLAY: The I/O address for the parallel port"</param>
/// <param name="Interupt">"NON PLUG&PLAY: Interrupt number of the parallel port"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_Initialize(
TPCANHandle Channel,
TPCANBaudrate Btr0Btr1,
TPCANType HwType = 0,
DWORD IOPort = 0,
WORD Interrupt = 0);
/// <summary>
/// Uninitializes one or all PCAN Channels initialized by CAN_Initialize
/// </summary>
/// <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
/// uninitialize all initialized channels</remarks>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_Uninitialize(
TPCANHandle Channel);
/// <summary>
/// Resets the receive and transmit queues of the PCAN Channel
/// </summary>
/// <remarks>
/// A reset of the CAN controller is not performed.
/// </remarks>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_Reset(
TPCANHandle Channel);
/// <summary>
/// Gets the current status of a PCAN Channel
/// </summary>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_GetStatus(
TPCANHandle Channel);
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="MessageBuffer">"A TPCANMsg structure buffer to store the CAN message"</param>
/// <param name="TimestampBuffer">"A TPCANTimestamp structure buffer to get
/// the reception time of the message. If this value is not desired, this parameter
/// should be passed as NULL"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_Read(
TPCANHandle Channel,
TPCANMsg* MessageBuffer,
TPCANTimestamp* TimestampBuffer);
/// <summary>
/// Transmits a CAN message
/// </summary>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="MessageBuffer">"A TPCANMsg buffer with the message to be sent"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_Write(
TPCANHandle Channel,
TPCANMsg* MessageBuffer);
/// <summary>
/// Configures the reception filter.
/// </summary>
/// <remarks>The message filter will be expanded with every call to
/// this function. If it is desired to reset the filter, please use
/// the CAN_SetValue function</remarks>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="FromID">"The lowest CAN ID to be received"</param>
/// <param name="ToID">"The highest CAN ID to be received"</param>
/// <param name="Mode">"Message type, Standard (11-bit identifier) or
/// Extended (29-bit identifier)"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_FilterMessages(
TPCANHandle Channel,
DWORD FromID,
DWORD ToID,
TPCANMode Mode);
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="Parameter">"The TPCANParameter parameter to get"</param>
/// <param name="Buffer">"Buffer for the parameter value"</param>
/// <param name="BufferLength">"Size in bytes of the buffer"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_GetValue(
TPCANHandle Channel,
TPCANParameter Parameter,
void* Buffer,
DWORD BufferLength);
/// <summary>
/// Configures or sets a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="Parameter">"The TPCANParameter parameter to set"</param>
/// <param name="Buffer">"Buffer with the value to be set"</param>
/// <param name="BufferLength">"Size in bytes of the buffer"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_SetValue(
TPCANHandle Channel,
TPCANParameter Parameter,
void* Buffer,
DWORD BufferLength);
/// <summary>
/// Returns a descriptive text of a given TPCANStatus error
/// code, in any desired language
/// </summary>
/// <remarks>The current languages available for translation are:
/// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
/// Italian (0x10) and French (0x0C)</remarks>
/// <param name="Error">"A TPCANStatus error code"</param>
/// <param name="Language">"Indicates a 'Primary language ID'"</param>
/// <param name="Buffer">"Buffer for a null terminated char array"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_GetErrorText(
TPCANStatus Error,
WORD Language,
LPSTR Buffer);
#ifdef __cplusplus
}
#endif
#endif

895
PCANBasic/PCANBasic.pas Normal file
View file

@ -0,0 +1,895 @@
// PCANBasic.pas
//
// ~~~~~~~~~~~~
//
// PCAN-Basic API
//
// ~~~~~~~~~~~~
//
// ------------------------------------------------------------------
// Author : Keneth Wagner
// Last change: 08.11.2011 Wagner
//
// Language: Pascal OO
// ------------------------------------------------------------------
//
// Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
// more Info at http://www.peak-system.com
//
unit PCANBasic;
interface
type
TPCANHandle = Byte;
{$Z4}
/// <summary>
/// Represents a PCAN status/error code
/// </summary>
TPCANStatus = (
/// <summary>
/// No error
/// </summary>
PCAN_ERROR_OK = $00000,
/// <summary>
/// Transmit buffer in CAN controller is full
/// </summary>
PCAN_ERROR_XMTFULL = $00001,
/// <summary>
/// CAN controller was read too late
/// </summary>
PCAN_ERROR_OVERRUN = $00002,
/// <summary>
/// Bus error: an error counter reached the 'light' limit
/// </summary>
PCAN_ERROR_BUSLIGHT = $00004,
/// <summary>
/// Bus error: an error counter reached the 'heavy' limit
/// </summary>
PCAN_ERROR_BUSHEAVY = $00008,
/// <summary>
/// Bus error: the CAN controller is in bus-off state
/// </summary>
PCAN_ERROR_BUSOFF = $00010,
/// <summary>
/// Mask for all bus errors
/// </summary>
PCAN_ERROR_ANYBUSERR = Byte(PCAN_ERROR_BUSLIGHT) Or Byte(PCAN_ERROR_BUSHEAVY) Or Byte(PCAN_ERROR_BUSOFF),
/// <summary>
/// Receive queue is empty
/// </summary>
PCAN_ERROR_QRCVEMPTY = $00020,
/// <summary>
/// Receive queue was read too late
/// </summary>
PCAN_ERROR_QOVERRUN = $00040,
/// <summary>
/// Transmit queue is full
/// </summary>
PCAN_ERROR_QXMTFULL = $00080,
/// <summary>
/// Test of the CAN controller hardware registers failed (no hardware found)
/// </summary>
PCAN_ERROR_REGTEST = $00100,
/// <summary>
/// Driver not loaded
/// </summary>
PCAN_ERROR_NODRIVER = $00200,
/// <summary>
/// Hardware already in use by a Net
/// </summary>
PCAN_ERROR_HWINUSE = $00400,
/// <summary>
/// A Client is already connected to the Net
/// </summary>
PCAN_ERROR_NETINUSE = $00800,
/// <summary>
/// Hardware handle is invalid
/// </summary>
PCAN_ERROR_ILLHW = $01400,
/// <summary>
/// Net handle is invalid
/// </summary>
PCAN_ERROR_ILLNET = $01800,
/// <summary>
/// Client handle is invalid
/// </summary>
PCAN_ERROR_ILLCLIENT = $01C00,
/// <summary>
/// Mask for all handle errors
/// </summary>
PCAN_ERROR_ILLHANDLE = Byte(PCAN_ERROR_ILLHW) Or Byte(PCAN_ERROR_ILLNET) Or Byte(PCAN_ERROR_ILLCLIENT),
/// <summary>
/// Resource (FIFO, Client, timeout) cannot be created
/// </summary>
PCAN_ERROR_RESOURCE = $02000,
/// <summary>
/// Invalid parameter
/// </summary>
PCAN_ERROR_ILLPARAMTYPE = $04000,
/// <summary>
/// Invalid parameter value
/// </summary>
PCAN_ERROR_ILLPARAMVAL = $08000,
/// <summary>
/// Unknow error
/// </summary>
PCAN_ERROR_UNKNOWN = $10000,
/// <summary>
/// Invalid data, function, or action
/// </summary>
PCAN_ERROR_ILLDATA = $20000,
/// <summary>
/// Channel is not initialized
/// </summary>
PCAN_ERROR_INITIALIZE = $40000
);
{$Z1}
/// <summary>
/// Represents a PCAN device
/// </summary>
TPCANDevice = (
/// <summary>
/// Undefined, unknown or not selected PCAN device value
/// </summary>
PCAN_NONE = 0,
/// <summary>
/// PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
/// </summary>
PCAN_PEAKCAN = 1,
/// <summary>
/// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
/// </summary>
PCAN_ISA = 2,
/// <summary>
/// PCAN-Dongle
/// </summary>
PCAN_DNG = 3,
/// <summary>
/// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
/// </summary>
PCAN_PCI = 4,
/// <summary>
/// PCAN-USB and PCAN-USB Pro
/// </summary>
PCAN_USB = 5,
/// <summary>
/// PCAN-PC Card
/// </summary>
PCAN_PCC = 6
);
/// <summary>
/// Represents a PCAN parameter to be read or set
/// </summary>
TPCANParameter = (
/// <summary>
/// PCAN-USB device number parameter
/// </summary>
PCAN_DEVICE_NUMBER = 1,
/// <summary>
/// PCAN-PC Card 5-Volt power parameter
/// </summary>
PCAN_5VOLTS_POWER = 2,
/// <summary>
/// PCAN receive event handler parameter
/// </summary>
PCAN_RECEIVE_EVENT = 3,
/// <summary>
/// PCAN message filter parameter
/// </summary>
PCAN_MESSAGE_FILTER = 4,
/// <summary>
/// PCAN-Basic API version parameter
/// </summary>
PCAN_API_VERSION = 5,
/// <summary>
/// PCAN device channel version parameter
/// </summary>
PCAN_CHANNEL_VERSION = 6,
/// <summary>
/// PCAN Reset-On-Busoff parameter
/// </summary>
PCAN_BUSOFF_AUTORESET = 7,
/// <summary>
/// PCAN Listen-Only parameter
/// </summary>
PCAN_LISTEN_ONLY = 8,
/// <summary>
/// Directory path for trace files
/// </summary>
PCAN_LOG_LOCATION = 9,
/// <summary>
/// Debug-Trace activation status
/// </summary>
PCAN_LOG_STATUS = 10,
/// <summary>
/// Configuration of the debugged information (LOG_FUNCTION_***)
/// </summary>
PCAN_LOG_CONFIGURE = 11,
/// <summary>
/// Custom insertion of text into the log file
/// </summary>
PCAN_LOG_TEXT = 12,
/// <summary>
/// Availability status of a PCAN-Channel
/// </summary>
PCAN_CHANNEL_CONDITION = 13,
/// <summary>
/// PCAN hardware name parameter
/// </summary>
PCAN_HARDWARE_NAME = 14,
/// <summary>
/// Message reception status of a PCAN-Channel
/// </summary>
PCAN_RECEIVE_STATUS = 15,
/// <summary>
/// CAN-Controller number of a PCAN-Channel
/// </summary>
PCAN_CONTROLLER_NUMBER = 16
);
/// <summary>
/// Represents the type of a PCAN message
/// </summary>
TPCANMessageType = (
/// <summary>
/// The PCAN message is a CAN Standard Frame (11-bit identifier)
/// </summary>
PCAN_MESSAGE_STANDARD = $00,
/// <summary>
/// The PCAN message is a CAN Remote-Transfer-Request Frame
/// </summary>
PCAN_MESSAGE_RTR = $01,
/// <summary>
/// The PCAN message is a CAN Extended Frame (29-bit identifier)
/// </summary>
PCAN_MESSAGE_EXTENDED = $02,
/// <summary>
/// The PCAN message represents a PCAN status message
/// </summary>
PCAN_MESSAGE_STATUS = $80
);
/// <summary>
/// Represents a PCAN filter mode
/// </summary>
TPCANMode = (
/// <summary>
/// Mode is Standard (11-bit identifier)
/// </summary>
PCAN_MODE_STANDARD = Byte(PCAN_MESSAGE_STANDARD),
/// <summary>
/// Mode is Extended (29-bit identifier)
/// </summary>
PCAN_MODE_EXTENDED = Byte(PCAN_MESSAGE_EXTENDED)
);
{$Z2}
/// <summary>
/// Represents a PCAN Baud rate register value
/// </summary>
TPCANBaudrate = (
/// <summary>
/// 1 MBit/s
/// </summary>
PCAN_BAUD_1M = $0014,
/// <summary>
/// 800 kBit/s
/// </summary>
PCAN_BAUD_800K = $0016,
/// <summary>
/// 500 kBit/s
/// </summary>
PCAN_BAUD_500K = $001C,
/// <summary>
/// 250 kBit/s
/// </summary>
PCAN_BAUD_250K = $011C,
/// <summary>
/// 125 kBit/s
/// </summary>
PCAN_BAUD_125K = $031C,
/// <summary>
/// 100 kBit/s
/// </summary>
PCAN_BAUD_100K = $432F,
/// <summary>
/// 95,238 kBit/s
/// </summary>
PCAN_BAUD_95K = $C34E,
/// <summary>
/// 83,333 kBit/s
/// </summary>
PCAN_BAUD_83K = $4B14,
/// <summary>
/// 50 kBit/s
/// </summary>
PCAN_BAUD_50K = $472F,
/// <summary>
/// 47,619 kBit/s
/// </summary>
PCAN_BAUD_47K = $1414,
/// <summary>
/// 33,333 kBit/s
/// </summary>
PCAN_BAUD_33K = $1D14,
/// <summary>
/// 20 kBit/s
/// </summary>
PCAN_BAUD_20K = $532F,
/// <summary>
/// 10 kBit/s
/// </summary>
PCAN_BAUD_10K = $672F,
/// <summary>
/// 5 kBit/s
/// </summary>
PCAN_BAUD_5K = $7F7F
);
{$Z1}
/// <summary>
/// Represents the type of PCAN (non plug&play) hardware to be initialized
/// </summary>
TPCANType = (
/// <summary>
/// PCAN-ISA 82C200
/// </summary>
PCAN_TYPE_ISA = $01,
/// <summary>
/// PCAN-ISA SJA1000
/// </summary>
PCAN_TYPE_ISA_SJA = $09,
/// <summary>
/// PHYTEC ISA
/// </summary>
PCAN_TYPE_ISA_PHYTEC = $04,
/// <summary>
/// PCAN-Dongle 82C200
/// </summary>
PCAN_TYPE_DNG = $02,
/// <summary>
/// PCAN-Dongle EPP 82C200
/// </summary>
PCAN_TYPE_DNG_EPP = $03,
/// <summary>
/// PCAN-Dongle SJA1000
/// </summary>
PCAN_TYPE_DNG_SJA = $05,
/// <summary>
/// PCAN-Dongle EPP SJA1000
/// </summary>
PCAN_TYPE_DNG_SJA_EPP = $06
);
/// <summary>
/// Represents a PCAN message
/// </summary>
TPCANMsg = record
/// <summary>
/// 11/29-bit message identifier
/// </summary>
ID: Longword;
/// <summary>
/// Type of the message
/// </summary>
MSGTYPE: TPCANMessageType;
/// <summary>
/// Data Length Code of the message (0..8)
/// </summary>
LEN: Byte;
/// <summary>
/// Data of the message (DATA[0]..DATA[7])
/// </summary>
DATA: array[0..7] of Byte;
end;
/// <summary>
/// Represents a timestamp of a received PCAN message.
/// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
/// </summary>
TPCANTimestamp = record
/// <summary>
/// Base-value: milliseconds: 0.. 2^32-1
/// </summary>
millis: Longword;
/// <summary>
/// Roll-arounds of millis
/// </summary>
millis_overflow: Word;
/// <summary>
/// Microseconds: 0..999
/// </summary>
micros: Word;
end;
PTPCANTimestamp = ^TPCANTimestamp;
/// <summary>
/// PCAN-Basic API class implementation
/// </summary>
TPCANBasic = class
public
class var
/// <summary>
/// Undefined/default value for a PCAN bus
/// </summary>
const PCAN_NONEBUS: TPCANHandle = $00;
/// <summary>
/// PCAN-ISA interface, channel 1
/// </summary>
const PCAN_ISABUS1: TPCANHandle = $21;
/// <summary>
/// PCAN-ISA interface, channel 2
/// </summary>
const PCAN_ISABUS2: TPCANHandle = $22;
/// <summary>
/// PCAN-ISA interface, channel 3
/// </summary>
const PCAN_ISABUS3: TPCANHandle = $23;
/// <summary>
/// PCAN-ISA interface, channel 4
/// </summary>
const PCAN_ISABUS4: TPCANHandle = $24;
/// <summary>
/// PCAN-ISA interface, channel 5
/// </summary>
const PCAN_ISABUS5: TPCANHandle = $25;
/// <summary>
/// PCAN-ISA interface, channel 6
/// </summary>
const PCAN_ISABUS6: TPCANHandle = $26;
/// <summary>
/// PCAN-ISA interface, channel 7
/// </summary>
const PCAN_ISABUS7: TPCANHandle = $27;
/// <summary>
/// PCAN-ISA interface, channel 8
/// </summary>
const PCAN_ISABUS8: TPCANHandle = $28;
/// <summary>
/// PPCAN-Dongle/LPT interface, channel 1
/// </summary>
const PCAN_DNGBUS1: TPCANHandle = $31;
/// <summary>
/// PCAN-PCI interface, channel 1
/// </summary>
const PCAN_PCIBUS1: TPCANHandle = $41;
/// <summary>
/// PCAN-PCI interface, channel 2
/// </summary>
const PCAN_PCIBUS2: TPCANHandle = $42;
/// <summary>
/// PCAN-PCI interface, channel 3
/// </summary>
const PCAN_PCIBUS3: TPCANHandle = $43;
/// <summary>
/// PCAN-PCI interface, channel 4
/// </summary>
const PCAN_PCIBUS4: TPCANHandle = $44;
/// <summary>
/// PCAN-PCI interface, channel 5
/// </summary>
const PCAN_PCIBUS5: TPCANHandle = $45;
/// <summary>
/// PCAN-PCI interface, channel 6
/// </summary>
const PCAN_PCIBUS6: TPCANHandle = $46;
/// <summary>
/// PCAN-PCI interface, channel 7
/// </summary>
const PCAN_PCIBUS7: TPCANHandle = $47;
/// <summary>
/// PCAN-PCI interface, channel 8
/// </summary>
const PCAN_PCIBUS8: TPCANHandle = $48;
/// <summary>
/// PCAN-USB interface, channel 1
/// </summary>
const PCAN_USBBUS1: TPCANHandle = $51;
/// <summary>
/// PCAN-USB interface, channel 2
/// </summary>
const PCAN_USBBUS2: TPCANHandle = $52;
/// <summary>
/// PCAN-USB interface, channel 3
/// </summary>
const PCAN_USBBUS3: TPCANHandle = $53;
/// <summary>
/// PCAN-USB interface, channel 4
/// </summary>
const PCAN_USBBUS4: TPCANHandle = $54;
/// <summary>
/// PCAN-USB interface, channel 5
/// </summary>
const PCAN_USBBUS5: TPCANHandle = $55;
/// <summary>
/// PCAN-USB interface, channel 6
/// </summary>
const PCAN_USBBUS6: TPCANHandle = $56;
/// <summary>
/// PCAN-USB interface, channel 7
/// </summary>
const PCAN_USBBUS7: TPCANHandle = $57;
/// <summary>
/// PCAN-USB interface, channel 8
/// </summary>
const PCAN_USBBUS8: TPCANHandle = $58;
/// <summary>
/// PCAN-PC Card interface, channel 1
/// </summary>
const PCAN_PCCBUS1: TPCANHandle = $61;
/// <summary>
/// PCAN-PC Card interface, channel 2
/// </summary>
const PCAN_PCCBUS2: TPCANHandle = $62;
/// <summary>
/// The PCAN parameter is not set (inactive)
/// </summary>
const PCAN_PARAMETER_OFF: Integer = 0;
/// <summary>
/// The PCAN parameter is set (active)
/// </summary>
const PCAN_PARAMETER_ON: Integer = 1;
/// <summary>
/// The PCAN filter is closed. No messages will be received
/// </summary>
const PCAN_FILTER_CLOSE: Integer = 0;
/// <summary>
/// The PCAN filter is fully opened. All messages will be received
/// </summary>
const PCAN_FILTER_OPEN: Integer = 1;
/// <summary>
/// The PCAN filter is custom configured. Only registered
/// messages will be received
/// </summary>
const PCAN_FILTER_CUSTOM: Integer = 2;
/// <summary>
/// The PCAN-Channel handle is illegal, or its associated hadware is not available
/// </summary>
const PCAN_CHANNEL_UNAVAILABLE: Integer = 0;
/// <summary>
/// The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
/// </summary>
const PCAN_CHANNEL_AVAILABLE: Integer = 1;
/// <summary>
/// The PCAN-Channel handle is valid, and is already being used
/// </summary>
const PCAN_CHANNEL_OCCUPIED: Integer = 2;
/// <summary>
/// Logs system exceptions / errors
/// </summary>
const LOG_FUNCTION_DEFAULT: Integer = $00;
/// <summary>
/// Logs the entries to the PCAN-Basic API functions
/// </summary>
const LOG_FUNCTION_ENTRY: Integer = $01;
/// <summary>
/// Logs the parameters passed to the PCAN-Basic API functions
/// </summary>
const LOG_FUNCTION_PARAMETERS: Integer = $02;
/// <summary>
/// Logs the exits from the PCAN-Basic API functions
/// </summary>
const LOG_FUNCTION_LEAVE: Integer = $04;
/// <summary>
/// Logs the CAN messages passed to the CAN_Write function
/// </summary>
const LOG_FUNCTION_WRITE: Integer = $08;
/// <summary>
/// Logs the CAN messages received within the CAN_Read function
/// </summary>
const LOG_FUNCTION_READ: Integer = $10;
/// <summary>
/// Logs all possible information within the PCAN-Basic API functions
/// </summary>
const LOG_FUNCTION_ALL: Integer = $FFFF;
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
/// <param name="HwType">NON PLUG&PLAY: The type of hardware and operation mode</param>
/// <param name="IOPort">NON PLUG&PLAY: The I/O address for the parallel port</param>
/// <param name="Interrupt">NON PLUG&PLAY: Interrupt number of the parallel port</param>
/// <returns>A TPCANStatus error code</returns>
class function Initialize(
Channel: TPCANHandle;
Btr0Btr1: TPCANBaudrate;
HwType: TPCANType;
IOPort: LongWord;
Interrupt: Word
): TPCANStatus; overload;
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
/// <returns>A TPCANStatus error code</returns>
class function Initialize(
Channel: TPCANHandle;
Btr0Btr1: TPCANBaudrate
): TPCANStatus; overload;
/// <summary>
/// Uninitializes one or all PCAN Channels initialized by CAN_Initialize
/// </summary>
/// <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
/// uninitialize all initialized channels</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
class function Uninitialize(
Channel: TPCANHandle
): TPCANStatus;
/// <summary>
/// Resets the receive and transmit queues of the PCAN Channel
/// </summary>
/// <remarks>A reset of the CAN controller is not performed</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
class function Reset(
Channel: TPCANHandle
): TPCANStatus;
/// <summary>
/// Gets the current status of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
class function GetStatus(
Channel: TPCANHandle
): TPCANStatus;
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
/// <param name="TimestampBuffer">A TPCANTimestamp structure buffer to get
/// the reception time of the message</param>
/// <returns>A TPCANStatus error code</returns>
class function Read(
Channel: TPCANHandle;
var MessageBuffer: TPCANMsg;
var TimestampBuffer: TPCANTimestamp
):TPCANStatus; overload;
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
/// <returns>A TPCANStatus error code</returns>
class function Read(
Channel: TPCANHandle;
var MessageBuffer: TPCANMsg
): TPCANStatus; overload;
/// <summary>
/// Transmits a CAN message
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg buffer with the message to be sent</param>
/// <returns>A TPCANStatus error code</returns>
class function Write(
Channel: TPCANHandle;
var MessageBuffer: TPCANMsg
): TPCANStatus;
/// <summary>
/// Configures the reception filter
/// </summary>
/// <remarks>The message filter will be expanded with every call to
/// this function. If it is desired to reset the filter, please use
/// the 'SetValue' function</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="FromID">The lowest CAN ID to be received</param>
/// <param name="ToID">The highest CAN ID to be received</param>
/// <param name="Mode">Message type, Standard (11-bit identifier) or
/// Extended (29-bit identifier)</param>
/// <returns>A TPCANStatus error code</returns>
class function FilterMessages(
Channel: TPCANHandle;
FromID: LongWord;
ToID: LongWord;
Mode: TPCANMode
): TPCANStatus;
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to get</param>
/// <param name="NumericBuffer">Buffer for the parameter value</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
class function GetValue(
Channel: TPCANHandle;
Parameter: TPCANParameter;
NumericBuffer: PLongWord;
BufferLength: LongWord
): TPCANStatus; overload;
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to get</param>
/// <param name="StringBuffer">Buffer for the parameter value</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
class function GetValue(
Channel: TPCANHandle;
Parameter: TPCANParameter;
StringBuffer: PAnsiChar;
BufferLength: LongWord
): TPCANStatus; overload;
/// <summary>
/// Configures or sets a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to set</param>
/// <param name="NumericBuffer">Buffer with the value to be set</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
class function SetValue(
Channel: TPCANHandle;
Parameter: TPCANParameter;
NumericBuffer: PLongWord;
BufferLength: LongWord
): TPCANStatus; overload;
/// <summary>
/// Configures or sets a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to set</param>
/// <param name="StringBuffer">Buffer with the value to be set</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
class function SetValue(
Channel: TPCANHandle;
Parameter: TPCANParameter;
StringBuffer: PAnsiChar;
BufferLength: LongWord
): TPCANStatus; overload;
/// <summary>
/// Returns a descriptive text of a given TPCANStatus error
/// code, in any desired language
/// </summary>
/// <remarks>The current languages available for translation are:
/// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
/// Italian (0x10) and French (0x0C)</remarks>
/// <param name="Error">A TPCANStatus error code</param>
/// <param name="Language">Indicates a 'Primary language ID'</param>
/// <param name="StringBuffer">Buffer for the text (must be at least 256 in length)</param>
/// <returns>A TPCANStatus error code</returns>
class function GetErrorText(
Error: TPCANStatus;
Language: Word;
StringBuffer: PAnsiChar
): TPCANStatus;
end;
implementation
uses SysUtils;
const DLL_Name = 'PCANBASIC.DLL';
function CAN_Initialize(Channel: TPCANHandle; Btr0Btr1: TPCANBaudrate; HwType: TPCANType; IOPort: LongWord; Interrupt: Word): TPCANStatus; stdcall;
external DLL_Name;
function CAN_Uninitialize(Channel: TPCANHandle): TPCANStatus; stdcall;
external DLL_Name;
function CAN_Reset(Channel: TPCANHandle): TPCANStatus; stdcall;
external DLL_Name;
function CAN_GetStatus(Channel: TPCANHandle): TPCANStatus; stdcall;
external DLL_Name;
function CAN_Read(Channel: TPCANHandle; var MessageBuffer: TPCANMsg; TimestampBuffer: PTPCANTimestamp):TPCANStatus; overload; stdcall;
external DLL_Name;
function CAN_Write(Channel: TPCANHandle; var MessageBuffer: TPCANMsg): TPCANStatus; stdcall;
external DLL_Name;
function CAN_FilterMessages(Channel: TPCANHandle; FromID: LongWord; ToID: LongWord; Mode: TPCANMode): TPCANStatus; stdcall;
external DLL_Name;
function CAN_GetValue(Channel: TPCANHandle; Parameter: TPCANParameter; Buffer: Pointer; BufferLength: LongWord): TPCANStatus; stdcall;
external DLL_Name;
function CAN_SetValue(Channel: TPCANHandle; Parameter: TPCANParameter; Buffer: Pointer; BufferLength: LongWord): TPCANStatus; stdcall;
external DLL_Name;
function CAN_GetErrorText(Error: TPCANStatus; Language: Word; StringBuffer: PAnsiChar): TPCANStatus; stdcall;
external DLL_Name;
class function TPCANBasic.Initialize(Channel: TPCANHandle; Btr0Btr1: TPCANBaudrate; HwType: TPCANType; IOPort: LongWord; Interrupt: Word): TPCANStatus;
begin
Result:= CAN_Initialize(Channel,Btr0Btr1,HwType,IOPort,Interrupt);
end;
class function TPCANBasic.Initialize(Channel: TPCANHandle; Btr0Btr1: TPCANBaudrate): TPCANStatus;
begin
Result:= CAN_Initialize(Channel,Btr0Btr1,TPCANType(0), 0,0);
end;
class function TPCANBasic.Uninitialize(Channel: TPCANHandle): TPCANStatus;
begin
Result:= CAN_Uninitialize(Channel);
end;
class function TPCANBasic.Reset(Channel: TPCANHandle): TPCANStatus;
begin
Result:= CAN_Reset(Channel);
end;
class function TPCANBasic.GetStatus(Channel: TPCANHandle): TPCANStatus;
begin
Result:= CAN_GetStatus(Channel);
end;
class function TPCANBasic.Read(Channel: TPCANHandle; var MessageBuffer: TPCANMsg; var TimestampBuffer: TPCANTimestamp):TPCANStatus;
begin
Result:= CAN_Read(Channel, MessageBuffer, @TimestampBuffer);
end;
class function TPCANBasic.Read(Channel: TPCANHandle; var MessageBuffer: TPCANMsg):TPCANStatus;
begin
Result:= CAN_Read(Channel, MessageBuffer, nil);
end;
class function TPCANBasic.Write(Channel: TPCANHandle; var MessageBuffer: TPCANMsg): TPCANStatus;
begin
Result:= CAN_Write(Channel, MessageBuffer);
end;
class function TPCANBasic.FilterMessages(Channel: TPCANHandle; FromID: LongWord; ToID: LongWord; Mode: TPCANMode): TPCANStatus;
begin
Result:= CAN_FilterMessages(Channel, FromID,ToID,Mode);
end;
class function TPCANBasic.GetValue(Channel: TPCANHandle; Parameter: TPCANParameter; NumericBuffer: PLongWord; BufferLength: LongWord): TPCANStatus;
begin
Result:= CAN_GetValue(Channel, Parameter, NumericBuffer, BufferLength);
end;
class function TPCANBasic.GetValue(Channel: TPCANHandle; Parameter: TPCANParameter; StringBuffer: PAnsiChar; BufferLength: LongWord): TPCANStatus;
begin
Result:= CAN_GetValue(Channel, Parameter, StringBuffer, BufferLength);
end;
class function TPCANBasic.SetValue(Channel: TPCANHandle; Parameter: TPCANParameter; NumericBuffer: PLongWord; BufferLength: LongWord): TPCANStatus;
begin
Result:= CAN_SetValue(Channel, Parameter, NumericBuffer, BufferLength);
end;
class function TPCANBasic.SetValue(Channel: TPCANHandle; Parameter: TPCANParameter; StringBuffer: PAnsiChar; BufferLength: LongWord): TPCANStatus;
begin
Result:= CAN_SetValue(Channel, Parameter, StringBuffer, BufferLength);
end;
class function TPCANBasic.GetErrorText(Error: TPCANStatus; Language: Word; StringBuffer: PAnsiChar): TPCANStatus;
begin
Result:= CAN_GetErrorText(Error, Language, StringBuffer);
end;
end.

567
PCANBasic/PCANBasic.py Normal file
View file

@ -0,0 +1,567 @@
# PCANBasic.py
#
# ~~~~~~~~~~~~
#
# PCAN-Basic API
#
# ~~~~~~~~~~~~
#
# ------------------------------------------------------------------
# Author : Keneth Wagner
# Last change: 08.11.2011 Wagner
#
# Language: Python 2.6
# ------------------------------------------------------------------
#
# Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
# more Info at http://www.peak-system.com
#
# Module Imports
#
from ctypes import *
#///////////////////////////////////////////////////////////
# Type definitions
#///////////////////////////////////////////////////////////
TPCANHandle = c_ubyte # Represents a PCAN hardware channel handle
TPCANStatus = int # Represents a PCAN status/error code
TPCANParameter = c_ubyte # Represents a PCAN parameter to be read or set
TPCANDevice = c_ubyte # Represents a PCAN device
TPCANMessageType = c_ubyte # Represents the type of a PCAN message
TPCANType = c_ubyte # Represents the type of PCAN hardware to be initialized
TPCANMode = c_ubyte # Represents a PCAN filter mode
TPCANBaudrate = c_ushort # Represents a PCAN Baud rate register value
#///////////////////////////////////////////////////////////
# Value definitions
#///////////////////////////////////////////////////////////
# Currently defined and supported PCAN channels
#
PCAN_NONEBUS = TPCANHandle(0x00) # Undefined/default value for a PCAN bus
PCAN_ISABUS1 = TPCANHandle(0x21) # PCAN-ISA interface, channel 1
PCAN_ISABUS2 = TPCANHandle(0x22) # PCAN-ISA interface, channel 2
PCAN_ISABUS3 = TPCANHandle(0x23) # PCAN-ISA interface, channel 3
PCAN_ISABUS4 = TPCANHandle(0x24) # PCAN-ISA interface, channel 4
PCAN_ISABUS5 = TPCANHandle(0x25) # PCAN-ISA interface, channel 5
PCAN_ISABUS6 = TPCANHandle(0x26) # PCAN-ISA interface, channel 6
PCAN_ISABUS7 = TPCANHandle(0x27) # PCAN-ISA interface, channel 7
PCAN_ISABUS8 = TPCANHandle(0x28) # PCAN-ISA interface, channel 8
PCAN_DNGBUS1 = TPCANHandle(0x31) # PCAN-Dongle/LPT interface, channel 1
PCAN_PCIBUS1 = TPCANHandle(0x41) # PCAN-PCI interface, channel 1
PCAN_PCIBUS2 = TPCANHandle(0x42) # PCAN-PCI interface, channel 2
PCAN_PCIBUS3 = TPCANHandle(0x43) # PCAN-PCI interface, channel 3
PCAN_PCIBUS4 = TPCANHandle(0x44) # PCAN-PCI interface, channel 4
PCAN_PCIBUS5 = TPCANHandle(0x45) # PCAN-PCI interface, channel 5
PCAN_PCIBUS6 = TPCANHandle(0x46) # PCAN-PCI interface, channel 6
PCAN_PCIBUS7 = TPCANHandle(0x47) # PCAN-PCI interface, channel 7
PCAN_PCIBUS8 = TPCANHandle(0x48) # PCAN-PCI interface, channel 8
PCAN_USBBUS1 = TPCANHandle(0x51) # PCAN-USB interface, channel 1
PCAN_USBBUS2 = TPCANHandle(0x52) # PCAN-USB interface, channel 2
PCAN_USBBUS3 = TPCANHandle(0x53) # PCAN-USB interface, channel 3
PCAN_USBBUS4 = TPCANHandle(0x54) # PCAN-USB interface, channel 4
PCAN_USBBUS5 = TPCANHandle(0x55) # PCAN-USB interface, channel 5
PCAN_USBBUS6 = TPCANHandle(0x56) # PCAN-USB interface, channel 6
PCAN_USBBUS7 = TPCANHandle(0x57) # PCAN-USB interface, channel 7
PCAN_USBBUS8 = TPCANHandle(0x58) # PCAN-USB interface, channel 8
PCAN_PCCBUS1 = TPCANHandle(0x61) # PCAN-PC Card interface, channel 1
PCAN_PCCBUS2 = TPCANHandle(0x62) # PCAN-PC Card interface, channel 2
# Represent the PCAN error and status codes
#
PCAN_ERROR_OK = TPCANStatus(0x00000) # No error
PCAN_ERROR_XMTFULL = TPCANStatus(0x00001) # Transmit buffer in CAN controller is full
PCAN_ERROR_OVERRUN = TPCANStatus(0x00002) # CAN controller was read too late
PCAN_ERROR_BUSLIGHT = TPCANStatus(0x00004) # Bus error: an error counter reached the 'light' limit
PCAN_ERROR_BUSHEAVY = TPCANStatus(0x00008) # Bus error: an error counter reached the 'heavy' limit
PCAN_ERROR_BUSOFF = TPCANStatus(0x00010) # Bus error: the CAN controller is in bus-off state
PCAN_ERROR_ANYBUSERR = TPCANStatus(PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF) # Mask for all bus errors
PCAN_ERROR_QRCVEMPTY = TPCANStatus(0x00020) # Receive queue is empty
PCAN_ERROR_QOVERRUN = TPCANStatus(0x00040) # Receive queue was read too late
PCAN_ERROR_QXMTFULL = TPCANStatus(0x00080) # Transmit queue is full
PCAN_ERROR_REGTEST = TPCANStatus(0x00100) # Test of the CAN controller hardware registers failed (no hardware found)
PCAN_ERROR_NODRIVER = TPCANStatus(0x00200) # Driver not loaded
PCAN_ERROR_HWINUSE = TPCANStatus(0x00400) # Hardware already in use by a Net
PCAN_ERROR_NETINUSE = TPCANStatus(0x00800) # A Client is already connected to the Net
PCAN_ERROR_ILLHW = TPCANStatus(0x01400) # Hardware handle is invalid
PCAN_ERROR_ILLNET = TPCANStatus(0x01800) # Net handle is invalid
PCAN_ERROR_ILLCLIENT = TPCANStatus(0x01C00) # Client handle is invalid
PCAN_ERROR_ILLHANDLE = TPCANStatus(PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT) # Mask for all handle errors
PCAN_ERROR_RESOURCE = TPCANStatus(0x02000) # Resource (FIFO, Client, timeout) cannot be created
PCAN_ERROR_ILLPARAMTYPE = TPCANStatus(0x04000) # Invalid parameter
PCAN_ERROR_ILLPARAMVAL = TPCANStatus(0x08000) # Invalid parameter value
PCAN_ERROR_UNKNOWN = TPCANStatus(0x10000) # Unknow error
PCAN_ERROR_ILLDATA = TPCANStatus(0x20000) # Invalid data, function, or action
PCAN_ERROR_INITIALIZE = TPCANStatus(0x40000) # Channel is not initialized
# PCAN devices
#
PCAN_NONE = TPCANDevice(0x00) # Undefined, unknown or not selected PCAN device value
PCAN_PEAKCAN = TPCANDevice(0x01) # PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
PCAN_ISA = TPCANDevice(0x02) # PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
PCAN_DNG = TPCANDevice(0x03) # PCAN-Dongle
PCAN_PCI = TPCANDevice(0x04) # PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
PCAN_USB = TPCANDevice(0x05) # PCAN-USB and PCAN-USB Pro
PCAN_PCC = TPCANDevice(0x06) # PCAN-PC Card
# PCAN parameters
#
PCAN_DEVICE_NUMBER = TPCANParameter(0x01) # PCAN-USB device number parameter
PCAN_5VOLTS_POWER = TPCANParameter(0x02) # PCAN-PC Card 5-Volt power parameter
PCAN_RECEIVE_EVENT = TPCANParameter(0x03) # PCAN receive event handler parameter
PCAN_MESSAGE_FILTER = TPCANParameter(0x04) # PCAN message filter parameter
PCAN_API_VERSION = TPCANParameter(0x05) # PCAN-Basic API version parameter
PCAN_CHANNEL_VERSION = TPCANParameter(0x06) # PCAN device channel version parameter
PCAN_BUSOFF_AUTORESET = TPCANParameter(0x07) # PCAN Reset-On-Busoff parameter
PCAN_LISTEN_ONLY = TPCANParameter(0x08) # PCAN Listen-Only parameter
PCAN_LOG_LOCATION = TPCANParameter(0x09) # Directory path for trace files
PCAN_LOG_STATUS = TPCANParameter(0x0A) # Debug-Trace activation status
PCAN_LOG_CONFIGURE = TPCANParameter(0x0B) # Configuration of the debugged information (LOG_FUNCTION_***)
PCAN_LOG_TEXT = TPCANParameter(0x0C) # Custom insertion of text into the log file
PCAN_CHANNEL_CONDITION = TPCANParameter(0x0D) # Availability status of a PCAN-Channel
PCAN_HARDWARE_NAME = TPCANParameter(0x0E) # PCAN hardware name parameter
PCAN_RECEIVE_STATUS = TPCANParameter(0x0F) # Message reception status of a PCAN-Channel
PCAN_CONTROLLER_NUMBER = TPCANParameter(0x010) # CAN-Controller number of a PCAN-Channel
# PCAN parameter values
#
PCAN_PARAMETER_OFF = int(0x00) # The PCAN parameter is not set (inactive)
PCAN_PARAMETER_ON = int(0x01) # The PCAN parameter is set (active)
PCAN_FILTER_CLOSE = int(0x00) # The PCAN filter is closed. No messages will be received
PCAN_FILTER_OPEN = int(0x01) # The PCAN filter is fully opened. All messages will be received
PCAN_FILTER_CUSTOM = int(0x02) # The PCAN filter is custom configured. Only registered messages will be received
PCAN_CHANNEL_UNAVAILABLE = int(0x00) # The PCAN-Channel handle is illegal, or its associated hadware is not available
PCAN_CHANNEL_AVAILABLE = int(0x01) # The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
PCAN_CHANNEL_OCCUPIED = int(0x02) # The PCAN-Channel handle is valid, and is already being used
LOG_FUNCTION_DEFAULT = int(0x00) # Logs system exceptions / errors
LOG_FUNCTION_ENTRY = int(0x01) # Logs the entries to the PCAN-Basic API functions
LOG_FUNCTION_PARAMETERS = int(0x02) # Logs the parameters passed to the PCAN-Basic API functions
LOG_FUNCTION_LEAVE = int(0x04) # Logs the exits from the PCAN-Basic API functions
LOG_FUNCTION_WRITE = int(0x08) # Logs the CAN messages passed to the CAN_Write function
LOG_FUNCTION_READ = int(0x10) # Logs the CAN messages received within the CAN_Read function
LOG_FUNCTION_ALL = int(0xFFFF) # Logs all possible information within the PCAN-Basic API functions
# PCAN message types
#
PCAN_MESSAGE_STANDARD = TPCANMessageType(0x00) # The PCAN message is a CAN Standard Frame (11-bit identifier)
PCAN_MESSAGE_RTR = TPCANMessageType(0x01) # The PCAN message is a CAN Remote-Transfer-Request Frame
PCAN_MESSAGE_EXTENDED = TPCANMessageType(0x02) # The PCAN message is a CAN Extended Frame (29-bit identifier)
PCAN_MESSAGE_STATUS = TPCANMessageType(0x80) # The PCAN message represents a PCAN status message
# Frame Type / Initialization Mode
#
PCAN_MODE_STANDARD = PCAN_MESSAGE_STANDARD
PCAN_MODE_EXTENDED = PCAN_MESSAGE_EXTENDED
# Baud rate codes = BTR0/BTR1 register values for the CAN controller.
# You can define your own Baud rate with the BTROBTR1 register.
# Take a look at www.peak-system.com for our free software "BAUDTOOL"
# to calculate the BTROBTR1 register for every baudrate and sample point.
#
PCAN_BAUD_1M = TPCANBaudrate(0x0014) # 1 MBit/s
PCAN_BAUD_800K = TPCANBaudrate(0x0016) # 800 kBit/s
PCAN_BAUD_500K = TPCANBaudrate(0x001C) # 500 kBit/s
PCAN_BAUD_250K = TPCANBaudrate(0x011C) # 250 kBit/s
PCAN_BAUD_125K = TPCANBaudrate(0x031C) # 125 kBit/s
PCAN_BAUD_100K = TPCANBaudrate(0x432F) # 100 kBit/s
PCAN_BAUD_95K = TPCANBaudrate(0xC34E) # 95,238 kBit/s
PCAN_BAUD_83K = TPCANBaudrate(0x4B14) # 83,333 kBit/s
PCAN_BAUD_50K = TPCANBaudrate(0x472F) # 50 kBit/s
PCAN_BAUD_47K = TPCANBaudrate(0x1414) # 47,619 kBit/s
PCAN_BAUD_33K = TPCANBaudrate(0x1D14) # 33,333 kBit/s
PCAN_BAUD_20K = TPCANBaudrate(0x532F) # 20 kBit/s
PCAN_BAUD_10K = TPCANBaudrate(0x672F) # 10 kBit/s
PCAN_BAUD_5K = TPCANBaudrate(0x7F7F) # 5 kBit/s
# Supported No-Plug-And-Play Hardware types
#
PCAN_TYPE_ISA = TPCANType(0x01) # PCAN-ISA 82C200
PCAN_TYPE_ISA_SJA = TPCANType(0x09) # PCAN-ISA SJA1000
PCAN_TYPE_ISA_PHYTEC = TPCANType(0x04) # PHYTEC ISA
PCAN_TYPE_DNG = TPCANType(0x02) # PCAN-Dongle 82C200
PCAN_TYPE_DNG_EPP = TPCANType(0x03) # PCAN-Dongle EPP 82C200
PCAN_TYPE_DNG_SJA = TPCANType(0x05) # PCAN-Dongle SJA1000
PCAN_TYPE_DNG_SJA_EPP = TPCANType(0x06) # PCAN-Dongle EPP SJA1000
# Represents a PCAN message
#
class TPCANMsg (Structure):
"""
Represents a PCAN message
"""
_fields_ = [ ("ID", c_ulong), # 11/29-bit message identifier
("MSGTYPE", TPCANMessageType), # Type of the message
("LEN", c_ubyte), # Data Length Code of the message (0..8)
("DATA", c_ubyte * 8) ] # Data of the message (DATA[0]..DATA[7])
# Represents a timestamp of a received PCAN message
# Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
#
class TPCANTimestamp (Structure):
"""
Represents a timestamp of a received PCAN message
Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
"""
_fields_ = [ ("millis", c_ulong), # Base-value: milliseconds: 0.. 2^32-1
("millis_overflow", c_ushort), # Roll-arounds of millis
("micros", c_ushort) ] # Microseconds: 0..999
#///////////////////////////////////////////////////////////
# PCAN-Basic API function declarations
#///////////////////////////////////////////////////////////
# PCAN-Basic API class implementation
#
class PCANBasic:
"""
PCAN-Basic API class implementation
"""
def __init__(self):
# Loads the PCANBasic.dll
#
self.__m_dllBasic = windll.LoadLibrary("PCANBasic")
if self.__m_dllBasic == None:
print "Exception: The PCAN-Basic DLL couldn't be loaded!"
# Initializes a PCAN Channel
#
def Initialize(
self,
Channel,
Btr0Btr1,
HwType = TPCANType(0),
IOPort = c_uint(0),
Interrupt = c_ushort(0)):
"""
Initializes a PCAN Channel
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Btr0Btr1 : The speed for the communication (BTR0BTR1 code)
HwType : NON PLUG&PLAY: The type of hardware and operation mode
IOPort : NON PLUG&PLAY: The I/O address for the parallel port
Interrupt: NON PLUG&PLAY: Interrupt number of the parallel port
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Initialize(Channel,Btr0Btr1,HwType,IOPort,Interrupt)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Initialize"
raise
# Uninitializes one or all PCAN Channels initialized by CAN_Initialize
#
def Uninitialize(
self,
Channel):
"""
Uninitializes one or all PCAN Channels initialized by CAN_Initialize
Remarks:
Giving the TPCANHandle value "PCAN_NONEBUS", uninitialize all initialized channels
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Uninitialize(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Uninitialize"
raise
# Resets the receive and transmit queues of the PCAN Channel
#
def Reset(
self,
Channel):
"""
Resets the receive and transmit queues of the PCAN Channel
Remarks:
A reset of the CAN controller is not performed
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Reset(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Reset"
raise
# Gets the current status of a PCAN Channel
#
def GetStatus(
self,
Channel):
"""
Gets the current status of a PCAN Channel
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_GetStatus(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.GetStatus"
raise
# Reads a CAN message from the receive queue of a PCAN Channel
#
def Read(
self,
Channel):
"""
Reads a CAN message from the receive queue of a PCAN Channel
Remarks:
The return value of this method is a 3-touple, where
the first value is the result (TPCANStatus) of the method.
The order of the values are:
[0]: A TPCANStatus error code
[1]: A TPCANMsg structure with the CAN message read
[2]: A TPCANTimestamp structure with the time when a message was read
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A touple with three values
"""
try:
msg = TPCANMsg()
timestamp = TPCANTimestamp()
res = self.__m_dllBasic.CAN_Read(Channel,byref(msg),byref(timestamp))
return TPCANStatus(res),msg,timestamp
except:
print "Exception on PCANBasic.Read"
raise
# Transmits a CAN message
#
def Write(
self,
Channel,
MessageBuffer):
"""
Transmits a CAN message
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
MessageBuffer: A TPCANMsg representing the CAN message to be sent
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Write(Channel,byref(MessageBuffer))
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Write"
raise
# Configures the reception filter
#
def FilterMessages(
self,
Channel,
FromID,
ToID,
Mode):
"""
Configures the reception filter
Remarks:
The message filter will be expanded with every call to this function.
If it is desired to reset the filter, please use the 'SetValue' function.
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
FromID : A c_ulong value with the lowest CAN ID to be received
ToID : A c_ulong value with the highest CAN ID to be received
Mode : A TPCANMode representing the message type (Standard, 11-bit
identifier, or Extended, 29-bit identifier)
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_FilterMessages(Channel,FromID,ToID,Mode)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.FilterMessages"
raise
# Retrieves a PCAN Channel value
#
def GetValue(
self,
Channel,
Parameter):
"""
Retrieves a PCAN Channel value
Remarks:
Parameters can be present or not according with the kind
of Hardware (PCAN Channel) being used. If a parameter is not available,
a PCAN_ERROR_ILLPARAMTYPE error will be returned.
The return value of this method is a 2-touple, where
the first value is the result (TPCANStatus) of the method and
the second one, the asked value
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Parameter : The TPCANParameter parameter to get
Returns:
A touple with 2 values
"""
try:
if Parameter == PCAN_API_VERSION or Parameter == PCAN_CHANNEL_VERSION or Parameter == PCAN_LOG_LOCATION:
mybuffer = create_string_buffer(256)
else:
mybuffer = c_int(0)
res = self.__m_dllBasic.CAN_GetValue(Channel,Parameter,byref(mybuffer),sizeof(mybuffer))
return TPCANStatus(res),mybuffer.value
except:
print "Exception on PCANBasic.GetValue"
raise
# Returns a descriptive text of a given TPCANStatus
# error code, in any desired language
#
def SetValue(
self,
Channel,
Parameter,
Buffer):
"""
Returns a descriptive text of a given TPCANStatus error
code, in any desired language
Remarks:
Parameters can be present or not according with the kind
of Hardware (PCAN Channel) being used. If a parameter is not available,
a PCAN_ERROR_ILLPARAMTYPE error will be returned.
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Parameter : The TPCANParameter parameter to set
Buffer : Buffer with the value to be set
BufferLength : Size in bytes of the buffer
Returns:
A TPCANStatus error code
"""
try:
if Parameter == PCAN_LOG_LOCATION or Parameter == PCAN_LOG_TEXT:
mybuffer = create_string_buffer(256)
else:
mybuffer = c_int(0)
mybuffer.value = Buffer
res = self.__m_dllBasic.CAN_SetValue(Channel,Parameter,byref(mybuffer),sizeof(mybuffer))
return TPCANStatus(res)
except:
print "Exception on PCANBasic.SetValue"
raise
def GetErrorText(
self,
Error,
Language = 0):
"""
Configures or sets a PCAN Channel value
Remarks:
The current languages available for translation are:
Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
Italian (0x10) and French (0x0C)
The return value of this method is a 2-touple, where
the first value is the result (TPCANStatus) of the method and
the second one, the error text
Parameters:
Error : A TPCANStatus error code
Language : Indicates a 'Primary language ID' (Default is Neutral(0))
Returns:
A touple with 2 values
"""
try:
mybuffer = create_string_buffer(256)
res = self.__m_dllBasic.CAN_GetErrorText(Error,Language,byref(mybuffer))
return TPCANStatus(res),mybuffer.value
except:
print "Exception on PCANBasic.GetErrorText"
raise

BIN
PCANBasic/PCANBasic.pyc Normal file

Binary file not shown.

852
PCANBasic/PCANBasic.vb Normal file
View file

@ -0,0 +1,852 @@
' PCANBasic.cs
'
' ~~~~~~~~~~~~
'
' PCAN-Basic API
'
' ~~~~~~~~~~~~
'
' ------------------------------------------------------------------
' Author : Keneth Wagner
' Last change: 08.11.2011 Wagner
'
' Language: VB .NET
' ------------------------------------------------------------------
'
' Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
' more Info at http://www.peak-system.com
'
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports TPCANHandle = System.Byte
Namespace Peak.Can.Basic
#Region "Enumerations"
''' <summary>
''' Represents a PCAN status/error code
''' </summary>
<Flags()> _
Public Enum TPCANStatus As UInt32
''' <summary>
''' No error
''' </summary>
PCAN_ERROR_OK = &H0
''' <summary>
''' Transmit buffer in CAN controller is full
''' </summary>
PCAN_ERROR_XMTFULL = &H1
''' <summary>
''' CAN controller was read too late
''' </summary>
PCAN_ERROR_OVERRUN = &H2
''' <summary>
''' Bus error: an error counter reached the 'light' limit
''' </summary>
PCAN_ERROR_BUSLIGHT = &H4
''' <summary>
''' Bus error: an error counter reached the 'heavy' limit
''' </summary>
PCAN_ERROR_BUSHEAVY = &H8
''' <summary>
''' Bus error: the CAN controller is in bus-off state
''' </summary>
PCAN_ERROR_BUSOFF = &H10
''' <summary>
''' Mask for all bus errors
''' </summary>
PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT Or PCAN_ERROR_BUSHEAVY Or PCAN_ERROR_BUSOFF)
''' <summary>
''' Receive queue is empty
''' </summary>
PCAN_ERROR_QRCVEMPTY = &H20
''' <summary>
''' Receive queue was read too late
''' </summary>
PCAN_ERROR_QOVERRUN = &H40
''' <summary>
''' Transmit queue is full
''' </summary>
PCAN_ERROR_QXMTFULL = &H80
''' <summary>
''' Test of the CAN controller hardware registers failed (no hardware found)
''' </summary>
PCAN_ERROR_REGTEST = &H100
''' <summary>
''' Driver not loaded
''' </summary>
PCAN_ERROR_NODRIVER = &H200
''' <summary>
''' Hardware already in use by a Net
''' </summary>
PCAN_ERROR_HWINUSE = &H400
''' <summary>
''' A Client is already connected to the Net
''' </summary>
PCAN_ERROR_NETINUSE = &H800
''' <summary>
''' Hardware handle is invalid
''' </summary>
PCAN_ERROR_ILLHW = &H1400
''' <summary>
''' Net handle is invalid
''' </summary>
PCAN_ERROR_ILLNET = &H1800
''' <summary>
''' Client handle is invalid
''' </summary>
PCAN_ERROR_ILLCLIENT = &H1C00
''' <summary>
''' Mask for all handle errors
''' </summary>
PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW Or PCAN_ERROR_ILLNET Or PCAN_ERROR_ILLCLIENT)
''' <summary>
''' Resource (FIFO, Client, timeout) cannot be created
''' </summary>
PCAN_ERROR_RESOURCE = &H2000
''' <summary>
''' Invalid parameter
''' </summary>
PCAN_ERROR_ILLPARAMTYPE = &H4000
''' <summary>
''' Invalid parameter value
''' </summary>
PCAN_ERROR_ILLPARAMVAL = &H8000
''' <summary>
''' Unknow error
''' </summary>
PCAN_ERROR_UNKNOWN = &H10000
''' <summary>
''' Invalid data, function, or action.
''' </summary>
PCAN_ERROR_ILLDATA = &H20000
''' <summary>
''' Channel is not initialized
''' </summary>
PCAN_ERROR_INITIALIZE = &H40000
End Enum
''' <summary>
''' Represents a PCAN device
''' </summary>
Public Enum TPCANDevice As Byte
''' <summary>
''' Undefined, unknown or not selected PCAN device value
''' </summary>
PCAN_NONE = 0
''' <summary>
''' PCAN Non-Plug And Play devices. NOT USED WITHIN PCAN-Basic API
''' </summary>
PCAN_PEAKCAN = 1
''' <summary>
''' PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
''' </summary>
PCAN_ISA = 2
''' <summary>
''' PCAN-Dongle
''' </summary>
PCAN_DNG = 3
''' <summary>
''' PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
''' </summary>
PCAN_PCI = 4
''' <summary>
''' PCAN-USB and PCAN-USB Pro
''' </summary>
PCAN_USB = 5
''' <summary>
''' PCAN-PC Card
''' </summary>
PCAN_PCC = 6
End Enum
''' <summary>
''' Represents a PCAN parameter to be read or set
''' </summary>
Public Enum TPCANParameter As Byte
''' <summary>
''' PCAN-USB device number parameter
''' </summary>
PCAN_DEVICE_NUMBER = 1
''' <summary>
''' PCAN-PC Card 5-Volt power parameter
''' </summary>
PCAN_5VOLTS_POWER = 2
''' <summary>
''' PCAN receive event handler parameter
''' </summary>
PCAN_RECEIVE_EVENT = 3
''' <summary>
''' PCAN message filter parameter
''' </summary>
PCAN_MESSAGE_FILTER = 4
''' <summary>
''' PCAN-Basic API version parameter
''' </summary>
PCAN_API_VERSION = 5
''' <summary>
''' PCAN device channel version parameter
''' </summary>
PCAN_CHANNEL_VERSION = 6
''' <summary>
''' PCAN Reset-On-Busoff parameter
''' </summary>
PCAN_BUSOFF_AUTORESET = 7
''' <summary>
''' PCAN Listen-Only parameter
''' </summary>
PCAN_LISTEN_ONLY = 8
''' <summary>
''' Directory path for trace files
''' </summary>
PCAN_LOG_LOCATION = 9
''' <summary>
''' Debug-Trace activation status
''' </summary>
PCAN_LOG_STATUS = 10
''' <summary>
''' Configuration of the debugged information (LOG_FUNCTION_***)
''' </summary>
PCAN_LOG_CONFIGURE = 11
''' <summary>
''' Custom insertion of text into the log file
''' </summary>
PCAN_LOG_TEXT = 12
''' <summary>
''' Availability status of a PCAN-Channel
''' </summary>
PCAN_CHANNEL_CONDITION = 13
''' <summary>
''' PCAN hardware name parameter
''' </summary>
PCAN_HARDWARE_NAME = 14
''' <summary>
''' Message reception status of a PCAN-Channel
''' </summary>
PCAN_RECEIVE_STATUS = 15
''' <summary>
''' CAN-Controller number of a PCAN-Channel
''' </summary>
PCAN_CONTROLLER_NUMBER = 16
End Enum
''' <summary>
''' Represents the type of a PCAN message
''' </summary>
<Flags()> _
Public Enum TPCANMessageType As Byte
''' <summary>
''' The PCAN message is a CAN Standard Frame (11-bit identifier)
''' </summary>
PCAN_MESSAGE_STANDARD = &H0
''' <summary>
''' The PCAN message is a CAN Remote-Transfer-Request Frame
''' </summary>
PCAN_MESSAGE_RTR = &H1
''' <summary>
''' The PCAN message is a CAN Extended Frame (29-bit identifier)
''' </summary>
PCAN_MESSAGE_EXTENDED = &H2
''' <summary>
''' The PCAN message represents a PCAN status message
''' </summary>
PCAN_MESSAGE_STATUS = &H80
End Enum
''' <summary>
''' Represents a PCAN filter mode
''' </summary>
Public Enum TPCANMode As Byte
''' <summary>
''' Mode is Standard (11-bit identifier)
''' </summary>
PCAN_MODE_STANDARD = TPCANMessageType.PCAN_MESSAGE_STANDARD
''' <summary>
''' Mode is Extended (29-bit identifier)
''' </summary>
PCAN_MODE_EXTENDED = TPCANMessageType.PCAN_MESSAGE_EXTENDED
End Enum
''' <summary>
''' Represents a PCAN Baud rate register value
''' </summary>
Public Enum TPCANBaudrate As UInt16
''' <summary>
''' 1 MBit/s
''' </summary>
PCAN_BAUD_1M = &H14
''' <summary>
''' 800 kBit/s
''' </summary>
PCAN_BAUD_800K = &H16
''' <summary>
''' 500 kBit/s
''' </summary>
PCAN_BAUD_500K = &H1C
''' <summary>
''' 250 kBit/s
''' </summary>
PCAN_BAUD_250K = &H11C
''' <summary>
''' 125 kBit/s
''' </summary>
PCAN_BAUD_125K = &H31C
''' <summary>
''' 100 kBit/s
''' </summary>
PCAN_BAUD_100K = &H432F
''' <summary>
''' 95,238 kBit/s
''' </summary>
PCAN_BAUD_95K = &HC34E
''' <summary>
''' 83,333 kBit/s
''' </summary>
PCAN_BAUD_83K = &H4B14
''' <summary>
''' 50 kBit/s
''' </summary>
PCAN_BAUD_50K = &H472F
''' <summary>
''' 47,619 kBit/s
''' </summary>
PCAN_BAUD_47K = &H1414
''' <summary>
''' 33,333 kBit/s
''' </summary>
PCAN_BAUD_33K = &H1D14
''' <summary>
''' 20 kBit/s
''' </summary>
PCAN_BAUD_20K = &H532F
''' <summary>
''' 10 kBit/s
''' </summary>
PCAN_BAUD_10K = &H672F
''' <summary>
''' 5 kBit/s
''' </summary>
PCAN_BAUD_5K = &H7F7F
End Enum
''' <summary>
''' Represents the type of PCAN (non plug and play) hardware to be initialized
''' </summary>
Public Enum TPCANType As Byte
''' <summary>
''' PCAN-ISA 82C200
''' </summary>
PCAN_TYPE_ISA = &H1
''' <summary>
''' PCAN-ISA SJA1000
''' </summary>
PCAN_TYPE_ISA_SJA = &H9
''' <summary>
''' PHYTEC ISA
''' </summary>
PCAN_TYPE_ISA_PHYTEC = &H4
''' <summary>
''' PCAN-Dongle 82C200
''' </summary>
PCAN_TYPE_DNG = &H2
''' <summary>
''' PCAN-Dongle EPP 82C200
''' </summary>
PCAN_TYPE_DNG_EPP = &H3
''' <summary>
''' PCAN-Dongle SJA1000
''' </summary>
PCAN_TYPE_DNG_SJA = &H5
''' <summary>
''' PCAN-Dongle EPP SJA1000
''' </summary>
PCAN_TYPE_DNG_SJA_EPP = &H6
End Enum
#End Region
#Region "Structures"
''' <summary>
''' Represents a PCAN message
''' </summary>
Public Structure TPCANMsg
''' <summary>
''' 11/29-bit message identifier
''' </summary>
Public ID As UInt32
''' <summary>
''' Type of the message
''' </summary>
<MarshalAs(UnmanagedType.U1)> _
Public MSGTYPE As TPCANMessageType
''' <summary>
''' Data Length Code of the message (0..8)
''' </summary>
Public LEN As Byte
''' <summary>
''' Data of the message (DATA[0]..DATA[7])
''' </summary>
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _
Public DATA As Byte()
End Structure
''' <summary>
''' Represents a timestamp of a received PCAN message.
''' Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
''' </summary>
Public Structure TPCANTimestamp
''' <summary>
''' Base-value: milliseconds: 0.. 2^32-1
''' </summary>
Public millis As UInt32
''' <summary>
''' Roll-arounds of millis
''' </summary>
Public millis_overflow As UInt16
''' <summary>
''' Microseconds: 0..999
''' </summary>
Public micros As UInt16
End Structure
#End Region
#Region "PCANBasic class"
''' <summary>
''' PCAN-Basic API class implementation
''' </summary>
Public NotInheritable Class PCANBasic
#Region "PCAN-BUS Handles Definition"
''' <summary>
''' Undefined/default value for a PCAN bus
''' </summary>
Public Const PCAN_NONEBUS As TPCANHandle = &H0
''' <summary>
''' PCAN-ISA interface, channel 1
''' </summary>
Public Const PCAN_ISABUS1 As TPCANHandle = &H21
''' <summary>
''' PCAN-ISA interface, channel 2
''' </summary>
Public Const PCAN_ISABUS2 As TPCANHandle = &H22
''' <summary>
''' PCAN-ISA interface, channel 3
''' </summary>
Public Const PCAN_ISABUS3 As TPCANHandle = &H23
''' <summary>
''' PCAN-ISA interface, channel 4
''' </summary>
Public Const PCAN_ISABUS4 As TPCANHandle = &H24
''' <summary>
''' PCAN-ISA interface, channel 5
''' </summary>
Public Const PCAN_ISABUS5 As TPCANHandle = &H25
''' <summary>
''' PCAN-ISA interface, channel 6
''' </summary>
Public Const PCAN_ISABUS6 As TPCANHandle = &H26
''' <summary>
''' PCAN-ISA interface, channel 7
''' </summary>
Public Const PCAN_ISABUS7 As TPCANHandle = &H27
''' <summary>
''' PCAN-ISA interface, channel 8
''' </summary>
Public Const PCAN_ISABUS8 As TPCANHandle = &H28
''' <summary>
''' PPCAN-Dongle/LPT interface, channel 1
''' </summary>
Public Const PCAN_DNGBUS1 As TPCANHandle = &H31
''' <summary>
''' PCAN-PCI interface, channel 1
''' </summary>
Public Const PCAN_PCIBUS1 As TPCANHandle = &H41
''' <summary>
''' PCAN-PCI interface, channel 2
''' </summary>
Public Const PCAN_PCIBUS2 As TPCANHandle = &H42
''' <summary>
''' PCAN-PCI interface, channel 3
''' </summary>
Public Const PCAN_PCIBUS3 As TPCANHandle = &H43
''' <summary>
''' PCAN-PCI interface, channel 4
''' </summary>
Public Const PCAN_PCIBUS4 As TPCANHandle = &H44
''' <summary>
''' PCAN-PCI interface, channel 5
''' </summary>
Public Const PCAN_PCIBUS5 As TPCANHandle = &H45
''' <summary>
''' PCAN-PCI interface, channel 6
''' </summary>
Public Const PCAN_PCIBUS6 As TPCANHandle = &H46
''' <summary>
''' PCAN-PCI interface, channel 7
''' </summary>
Public Const PCAN_PCIBUS7 As TPCANHandle = &H47
''' <summary>
''' PCAN-PCI interface, channel 8
''' </summary>
Public Const PCAN_PCIBUS8 As TPCANHandle = &H48
''' <summary>
''' PCAN-USB interface, channel 1
''' </summary>
Public Const PCAN_USBBUS1 As TPCANHandle = &H51
''' <summary>
''' PCAN-USB interface, channel 2
''' </summary>
Public Const PCAN_USBBUS2 As TPCANHandle = &H52
''' <summary>
''' PCAN-USB interface, channel 3
''' </summary>
Public Const PCAN_USBBUS3 As TPCANHandle = &H53
''' <summary>
''' PCAN-USB interface, channel 4
''' </summary>
Public Const PCAN_USBBUS4 As TPCANHandle = &H54
''' <summary>
''' PCAN-USB interface, channel 5
''' </summary>
Public Const PCAN_USBBUS5 As TPCANHandle = &H55
''' <summary>
''' PCAN-USB interface, channel 6
''' </summary>
Public Const PCAN_USBBUS6 As TPCANHandle = &H56
''' <summary>
''' PCAN-USB interface, channel 7
''' </summary>
Public Const PCAN_USBBUS7 As TPCANHandle = &H57
''' <summary>
''' PCAN-USB interface, channel 8
''' </summary>
Public Const PCAN_USBBUS8 As TPCANHandle = &H58
''' <summary>
''' PCAN-PC Card interface, channel 1
''' </summary>
Public Const PCAN_PCCBUS1 As TPCANHandle = &H61
''' <summary>
''' PCAN-PC Card interface, channel 2
''' </summary>
Public Const PCAN_PCCBUS2 As TPCANHandle = &H62
#End Region
#Region "Parameter values definition"
''' <summary>
''' The PCAN parameter is not set (inactive)
''' </summary>
Public Const PCAN_PARAMETER_OFF As Integer = 0
''' <summary>
''' The PCAN parameter is set (active)
''' </summary>
Public Const PCAN_PARAMETER_ON As Integer = 1
''' <summary>
''' The PCAN filter is closed. No messages will be received
''' </summary>
Public Const PCAN_FILTER_CLOSE As Integer = 0
''' <summary>
''' The PCAN filter is fully opened. All messages will be received
''' </summary>
Public Const PCAN_FILTER_OPEN As Integer = 1
''' <summary>
''' The PCAN filter is custom configured. Only registered
''' messages will be received
''' </summary>
Public Const PCAN_FILTER_CUSTOM As Integer = 2
''' <summary>
''' The PCAN-Channel handle is illegal, or its associated hadware is not available
''' </summary>
Public Const PCAN_CHANNEL_UNAVAILABLE As Integer = 0
''' <summary>
''' The PCAN-Channel handle is available to be connected (Plug and Play Hardware: it means futhermore that the hardware is plugged-in)
''' </summary>
Public Const PCAN_CHANNEL_AVAILABLE As Integer = 1
''' <summary>
''' The PCAN-Channel handle is valid, and is already being used
''' </summary>
Public Const PCAN_CHANNEL_OCCUPIED As Integer = 2
''' <summary>
''' Logs system exceptions / errors
''' </summary>
Public Const LOG_FUNCTION_DEFAULT As Integer = &H0
''' <summary>
''' Logs the entries to the PCAN-Basic API functions
''' </summary>
Public Const LOG_FUNCTION_ENTRY As Integer = &H1
''' <summary>
''' Logs the parameters passed to the PCAN-Basic API functions
''' </summary>
Public Const LOG_FUNCTION_PARAMETERS As Integer = &H2
''' <summary>
''' Logs the exits from the PCAN-Basic API functions
''' </summary>
Public Const LOG_FUNCTION_LEAVE As Integer = &H4
''' <summary>
''' Logs the CAN messages passed to the CAN_Write function
''' </summary>
Public Const LOG_FUNCTION_WRITE As Integer = &H8
''' <summary>
''' Logs the CAN messages received within the CAN_Read function
''' </summary>
Public Const LOG_FUNCTION_READ As Integer = &H10
''' <summary>
''' Logs all possible information within the PCAN-Basic API functions
''' </summary>
Public Const LOG_FUNCTION_ALL As Integer = &HFFFF
#End Region
#Region "PCANBasic API Implementation"
''' <summary>
''' Initializes a PCAN Channel
''' </summary>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
''' <param name="HwType">NON PLUG AND PLAY: The type of hardware and operation mode</param>
''' <param name="IOPort">NON PLUG AND PLAY: The I/O address for the parallel port</param>
''' <param name="Interrupt">NON PLUG AND PLAY: Interrupt number of the parallel por</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Initialize")> _
Public Shared Function Initialize( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
<MarshalAs(UnmanagedType.U2)> _
ByVal Btr0Btr1 As TPCANBaudrate, _
<MarshalAs(UnmanagedType.U1)> _
ByVal HwType As TPCANType, _
ByVal IOPort As UInt32, _
ByVal Interrupt As UInt16) As TPCANStatus
End Function
''' <summary>
''' Initializes a PCAN Channel
''' </summary>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
''' <returns>A TPCANStatus error code</returns>
Public Shared Function Initialize( _
ByVal Channel As TPCANHandle, _
ByVal Btr0Btr1 As TPCANBaudrate) As TPCANStatus
Return Initialize(Channel, Btr0Btr1, 0, 0, 0)
End Function
''' <summary>
''' Uninitializes one or all PCAN Channels initialized by CAN_Initialize
''' </summary>
''' <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
''' uninitialize all initialized channels</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Uninitialize")> _
Public Shared Function Uninitialize( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle) As TPCANStatus
End Function
''' <summary>
''' Resets the receive and transmit queues of the PCAN Channel
''' </summary>
''' <remarks>A reset of the CAN controller is not performed</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Reset")> _
Public Shared Function Reset( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle) As TPCANStatus
End Function
''' <summary>
''' Gets the current status of a PCAN Channel
''' </summary>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_GetStatus")> _
Public Shared Function GetStatus( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle) As TPCANStatus
End Function
''' <summary>
''' Reads a CAN message from the receive queue of a PCAN Channel
''' </summary>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
''' <param name="TimestampBuffer">A TPCANTimestamp structure buffer to get
''' the reception time of the message</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Read")> _
Public Shared Function Read( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
ByRef MessageBuffer As TPCANMsg, _
ByRef TimestampBuffer As TPCANTimestamp) As TPCANStatus
End Function
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Read")> _
Private Shared Function Read( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
ByRef MessageBuffer As TPCANMsg, _
ByVal BufferPointer As IntPtr) As TPCANStatus
End Function
''' <summary>
''' Reads a CAN message from the receive queue of a PCAN Channel
''' </summary>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
''' <returns>A TPCANStatus error code</returns>
Public Shared Function Read( _
ByVal Channel As TPCANHandle, _
ByRef MessageBuffer As TPCANMsg) As TPCANStatus
Return Read(Channel, MessageBuffer, IntPtr.Zero)
End Function
''' <summary>
''' Transmits a CAN message
''' </summary>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="MessageBuffer">A TPCANMsg buffer with the message to be sent</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Write")> _
Public Shared Function Write( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
ByRef MessageBuffer As TPCANMsg) As TPCANStatus
End Function
''' <summary>
''' Configures the reception filter
''' </summary>
''' <remarks>The message filter will be expanded with every call to
''' this function. If it is desired to reset the filter, please use
''' the 'SetValue' function</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="FromID">The lowest CAN ID to be received</param>
''' <param name="ToID">The highest CAN ID to be received</param>
''' <param name="Mode">Message type, Standard (11-bit identifier) or
''' Extended (29-bit identifier)</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_FilterMessages")> _
Public Shared Function FilterMessages( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
ByVal FromID As UInt32, _
ByVal ToID As UInt32, _
<MarshalAs(UnmanagedType.U1)> _
ByVal Mode As TPCANMode) As TPCANStatus
End Function
''' <summary>
''' Retrieves a PCAN Channel value
''' </summary>
''' <remarks>Parameters can be present or not according with the kind
''' of Hardware (PCAN Channel) being used. If a parameter is not available,
''' a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="Parameter">The TPCANParameter parameter to get</param>
''' <param name="StringBuffer">Buffer for the parameter value</param>
''' <param name="BufferLength">Size in bytes of the buffer</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_GetValue")> _
Public Shared Function GetValue( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
<MarshalAs(UnmanagedType.U1)> _
ByVal Parameter As TPCANParameter, _
ByVal StringBuffer As StringBuilder, _
ByVal BufferLength As UInt32) As TPCANStatus
End Function
''' <summary>
''' Retrieves a PCAN Channel value
''' </summary>
''' <remarks>Parameters can be present or not according with the kind
''' of Hardware (PCAN Channel) being used. If a parameter is not available,
''' a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="Parameter">The TPCANParameter parameter to get</param>
''' <param name="NumericBuffer">Buffer for the parameter value</param>
''' <param name="BufferLength">Size in bytes of the buffer</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_GetValue")> _
Public Shared Function GetValue( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
<MarshalAs(UnmanagedType.U1)> _
ByVal Parameter As TPCANParameter, _
ByRef NumericBuffer As UInt32, _
ByVal BufferLength As UInt32) As TPCANStatus
End Function
''' <summary>
''' Configures or sets a PCAN Channel value
''' </summary>
''' <remarks>Parameters can be present or not according with the kind
''' of Hardware (PCAN Channel) being used. If a parameter is not available,
''' a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="Parameter">The TPCANParameter parameter to set</param>
''' <param name="NumericBuffer">Buffer with the value to be set</param>
''' <param name="BufferLength">Size in bytes of the buffer</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_SetValue")> _
Public Shared Function SetValue( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
<MarshalAs(UnmanagedType.U1)> _
ByVal Parameter As TPCANParameter, _
ByRef NumericBuffer As UInt32, _
ByVal BufferLength As UInt32) As TPCANStatus
End Function
''' <summary>
''' Configures or sets a PCAN Channel value
''' </summary>
''' <remarks>Parameters can be present or not according with the kind
''' of Hardware (PCAN Channel) being used. If a parameter is not available,
''' a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
''' <param name="Channel">The handle of a PCAN Channel</param>
''' <param name="Parameter"></param>
''' <param name="StringBuffer">Buffer with the value to be set</param>
''' <param name="BufferLength">Size in bytes of the buffer</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_SetValue")> _
Public Shared Function SetValue( _
<MarshalAs(UnmanagedType.U1)> _
ByVal Channel As TPCANHandle, _
<MarshalAs(UnmanagedType.U1)> _
ByVal Parameter As TPCANParameter, _
<MarshalAs(UnmanagedType.LPStr, SizeParamIndex:=3)> _
ByVal StringBuffer As String, _
ByVal BufferLength As UInt32) As TPCANStatus
End Function
''' <summary>
''' Returns a descriptive text of a given TPCANStatus error
''' code, in any desired language
''' </summary>
''' <remarks>The current languages available for translation are:
''' Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
''' Italian (0x10) and French (0x0C)</remarks>
''' <param name="anError">A TPCANStatus error code</param>
''' <param name="Language">Indicates a 'Primary language ID'</param>
''' <param name="StringBuffer">Buffer for the text (must be at least 256 in length)</param>
''' <returns>A TPCANStatus error code</returns>
<DllImport("PCANBasic.dll", EntryPoint:="CAN_GetErrorText")> _
Public Shared Function GetErrorText( _
<MarshalAs(UnmanagedType.U4)> _
ByVal anError As TPCANStatus, _
ByVal Language As UInt16, _
ByVal StringBuffer As StringBuilder) As TPCANStatus
End Function
#End Region
End Class
#End Region
End Namespace

852
PCANBasic/PCANBasicCLR.h Normal file
View file

@ -0,0 +1,852 @@
// PCANBasicCLR.h
//
// ~~~~~~~~~~~~
//
// PCAN-Basic API
//
// ~~~~~~~~~~~~
//
// ------------------------------------------------------------------
// Author : Keneth Wagner
// Last change: 08.11.2011 Wagner
//
// Language: C++/CLR
// ------------------------------------------------------------------
//
// Copyright (C) 1999-2012 PEAK-System Technik GmbH, Darmstadt
// more Info at http://www.peak-system.com
//
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;
////////////////////////////////////////////////////////////
// Type definitions
////////////////////////////////////////////////////////////
#define TPCANHandle System::Byte // Represents a PCAN hardware channel handle
namespace Peak
{
namespace Can
{
namespace Basic
{
#pragma region Enumerations
/// <summary>
/// Represents a PCAN status/error code
/// </summary>
[Flags]
public enum class TPCANStatus : UInt32
{
/// <summary>
/// No error
/// </summary>
PCAN_ERROR_OK = 0x00000,
/// <summary>
/// Transmit buffer in CAN controller is full
/// </summary>
PCAN_ERROR_XMTFULL = 0x00001,
/// <summary>
/// CAN controller was read too late
/// </summary>
PCAN_ERROR_OVERRUN = 0x00002,
/// <summary>
/// Bus error: an error counter reached the 'light' limit
/// </summary>
PCAN_ERROR_BUSLIGHT = 0x00004,
/// <summary>
/// Bus error: an error counter reached the 'heavy' limit
/// </summary>
PCAN_ERROR_BUSHEAVY = 0x00008,
/// <summary>
/// Bus error: the CAN controller is in bus-off state
/// </summary>
PCAN_ERROR_BUSOFF = 0x00010,
/// <summary>
/// Mask for all bus errors
/// </summary>
PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF),
/// <summary>
/// Receive queue is empty
/// </summary>
PCAN_ERROR_QRCVEMPTY = 0x00020,
/// <summary>
/// Receive queue was read too late
/// </summary>
PCAN_ERROR_QOVERRUN = 0x00040,
/// <summary>
/// Transmit queue is full
/// </summary>
PCAN_ERROR_QXMTFULL = 0x00080,
/// <summary>
/// Test of the CAN controller hardware registers failed (no hardware found)
/// </summary>
PCAN_ERROR_REGTEST = 0x00100,
/// <summary>
/// Driver not loaded
/// </summary>
PCAN_ERROR_NODRIVER = 0x00200,
/// <summary>
/// Hardware already in use by a Net
/// </summary>
PCAN_ERROR_HWINUSE = 0x00400,
/// <summary>
/// A Client is already connected to the Net
/// </summary>
PCAN_ERROR_NETINUSE = 0x00800,
/// <summary>
/// Hardware handle is invalid
/// </summary>
PCAN_ERROR_ILLHW = 0x01400,
/// <summary>
/// Net handle is invalid
/// </summary>
PCAN_ERROR_ILLNET = 0x01800,
/// <summary>
/// Client handle is invalid
/// </summary>
PCAN_ERROR_ILLCLIENT = 0x01C00,
/// <summary>
/// Mask for all handle errors
/// </summary>
PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT),
/// <summary>
/// Resource (FIFO, Client, timeout) cannot be created
/// </summary>
PCAN_ERROR_RESOURCE = 0x02000,
/// <summary>
/// Invalid parameter
/// </summary>
PCAN_ERROR_ILLPARAMTYPE = 0x04000,
/// <summary>
/// Invalid parameter value
/// </summary>
PCAN_ERROR_ILLPARAMVAL = 0x08000,
/// <summary>
/// Unknow error
/// </summary>
PCAN_ERROR_UNKNOWN = 0x10000,
/// <summary>
/// Invalid data, function, or action.
/// </summary>
PCAN_ERROR_ILLDATA = 0x20000,
/// <summary>
/// Channel is not initialized
/// </summary>
PCAN_ERROR_INITIALIZE = 0x40000,
};
/// <summary>
/// Represents a PCAN device
/// </summary>
public enum class TPCANDevice : Byte
{
/// <summary>
/// Undefined, unknown or not selected PCAN device value
/// </summary>
PCAN_NONE = 0,
/// <summary>
/// PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
/// </summary>
PCAN_PEAKCAN = 1,
/// <summary>
/// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
/// </summary>
PCAN_ISA = 2,
/// <summary>
/// PCAN-Dongle
/// </summary>
PCAN_DNG = 3,
/// <summary>
/// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
/// </summary>
PCAN_PCI = 4,
/// <summary>
/// PCAN-USB and PCAN-USB Pro
/// </summary>
PCAN_USB = 5,
/// <summary>
/// PCAN-PC Card
/// </summary>
PCAN_PCC = 6
};
/// <summary>
/// Represents a PCAN parameter to be read or set
/// </summary>
public enum class TPCANParameter : Byte
{
/// <summary>
/// PCAN-USB device number parameter
/// </summary>
PCAN_DEVICE_NUMBER = 1,
/// <summary>
/// PCAN-PC Card 5-Volt power parameter
/// </summary>
PCAN_5VOLTS_POWER = 2,
/// <summary>
/// PCAN receive event handler parameter
/// </summary>
PCAN_RECEIVE_EVENT = 3,
/// <summary>
/// PCAN message filter parameter
/// </summary>
PCAN_MESSAGE_FILTER = 4,
/// <summary>
/// PCAN-Basic API version parameter
/// </summary>
PCAN_API_VERSION = 5,
/// <summary>
/// PCAN device channel version parameter
/// </summary>
PCAN_CHANNEL_VERSION = 6,
/// <summary>
/// PCAN Reset-On-Busoff parameter
/// </summary>
PCAN_BUSOFF_AUTORESET = 7,
/// <summary>
/// PCAN Listen-Only parameter
/// </summary>
PCAN_LISTEN_ONLY = 8,
/// <summary>
/// Directory path for trace files
/// </summary>
PCAN_LOG_LOCATION = 9,
/// <summary>
/// Debug-Trace activation status
/// </summary>
PCAN_LOG_STATUS = 10,
/// <summary>
/// Configuration of the debugged information (LOG_FUNCTION_***)
/// </summary>
PCAN_LOG_CONFIGURE = 11,
/// <summary>
/// Custom insertion of text into the log file
/// </summary>
PCAN_LOG_TEXT = 12,
/// <summary>
/// Availability status of a PCAN-Channel
/// </summary>
PCAN_CHANNEL_CONDITION = 13,
/// <summary>
/// PCAN hardware name parameter
/// </summary>
PCAN_HARDWARE_NAME = 14,
/// <summary>
/// Message reception status of a PCAN-Channel
/// </summary>
PCAN_RECEIVE_STATUS = 15,
/// <summary>
/// CAN-Controller number of a PCAN-Channel
/// </summary>
PCAN_CONTROLLER_NUMBER = 16,
};
/// <summary>
/// Represents the type of a PCAN message
/// </summary>
[Flags]
public enum class TPCANMessageType : Byte
{
/// <summary>
/// The PCAN message is a CAN Standard Frame (11-bit identifier)
/// </summary>
PCAN_MESSAGE_STANDARD = 0x00,
/// <summary>
/// The PCAN message is a CAN Remote-Transfer-Request Frame
/// </summary>
PCAN_MESSAGE_RTR = 0x01,
/// <summary>
/// The PCAN message is a CAN Extended Frame (29-bit identifier)
/// </summary>
PCAN_MESSAGE_EXTENDED = 0x02,
/// <summary>
/// The PCAN message represents a PCAN status message
/// </summary>
PCAN_MESSAGE_STATUS = 0x80,
};
/// <summary>
/// Represents a PCAN filter mode
/// </summary>
public enum class TPCANMode : Byte
{
/// <summary>
/// Mode is Standard (11-bit identifier)
/// </summary>
PCAN_MODE_STANDARD = TPCANMessageType::PCAN_MESSAGE_STANDARD,
/// <summary>
/// Mode is Extended (29-bit identifier)
/// </summary>
PCAN_MODE_EXTENDED = TPCANMessageType::PCAN_MESSAGE_EXTENDED,
};
/// <summary>
/// Represents a PCAN Baud rate register value
/// </summary>
public enum class TPCANBaudrate : UInt16
{
/// <summary>
/// 1 MBit/s
/// </summary>
PCAN_BAUD_1M = 0x0014,
/// <summary>
/// 800 kBit/s
/// </summary>
PCAN_BAUD_800K = 0x0016,
/// <summary>
/// 500 kBit/s
/// </summary>
PCAN_BAUD_500K = 0x001C,
/// <summary>
/// 250 kBit/s
/// </summary>
PCAN_BAUD_250K = 0x011C,
/// <summary>
/// 125 kBit/s
/// </summary>
PCAN_BAUD_125K = 0x031C,
/// <summary>
/// 100 kBit/s
/// </summary>
PCAN_BAUD_100K = 0x432F,
/// <summary>
/// 95,238 kBit/s
/// </summary>
PCAN_BAUD_95K = 0xC34E,
/// <summary>
/// 83,333 kBit/s
/// </summary>
PCAN_BAUD_83K = 0x4B14,
/// <summary>
/// 50 kBit/s
/// </summary>
PCAN_BAUD_50K = 0x472F,
/// <summary>
/// 47,619 kBit/s
/// </summary>
PCAN_BAUD_47K = 0x1414,
/// <summary>
/// 33,333 kBit/s
/// </summary>
PCAN_BAUD_33K = 0x1D14,
/// <summary>
/// 20 kBit/s
/// </summary>
PCAN_BAUD_20K = 0x532F,
/// <summary>
/// 10 kBit/s
/// </summary>
PCAN_BAUD_10K = 0x672F,
/// <summary>
/// 5 kBit/s
/// </summary>
PCAN_BAUD_5K = 0x7F7F,
};
/// <summary>
/// Represents the type of PCAN (non plug&play) hardware to be initialized
/// </summary>
public enum class TPCANType : Byte
{
/// <summary>
/// PCAN-ISA 82C200
/// </summary>
PCAN_TYPE_ISA = 0x01,
/// <summary>
/// PCAN-ISA SJA1000
/// </summary>
PCAN_TYPE_ISA_SJA = 0x09,
/// <summary>
/// PHYTEC ISA
/// </summary>
PCAN_TYPE_ISA_PHYTEC = 0x04,
/// <summary>
/// PCAN-Dongle 82C200
/// </summary>
PCAN_TYPE_DNG = 0x02,
/// <summary>
/// PCAN-Dongle EPP 82C200
/// </summary>
PCAN_TYPE_DNG_EPP = 0x03,
/// <summary>
/// PCAN-Dongle SJA1000
/// </summary>
PCAN_TYPE_DNG_SJA = 0x05,
/// <summary>
/// PCAN-Dongle EPP SJA1000
/// </summary>
PCAN_TYPE_DNG_SJA_EPP = 0x06,
};
#pragma endregion
#pragma region Strutures
/// <summary>
/// Represents a PCAN message
/// </summary>
public value struct TPCANMsg
{
/// <summary>
/// 11/29-bit message identifier
/// </summary>
UInt32 ID;
/// <summary>
/// Type of the message
/// </summary>
[MarshalAs(UnmanagedType::U1)]
TPCANMessageType MSGTYPE;
/// <summary>
/// Data Length Code of the message (0..8)
/// </summary>
Byte LEN;
/// <summary>
/// Data of the message (DATA[0]..DATA[7])
/// </summary>
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 8)]
array<Byte>^ DATA;
};
/// <summary>
/// Represents a timestamp of a received PCAN message.
/// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
/// </summary>
public value struct TPCANTimestamp
{
/// <summary>
/// Base-value: milliseconds: 0.. 2^32-1
/// </summary>
UInt32 millis;
/// <summary>
/// Roll-arounds of millis
/// </summary>
UInt16 millis_overflow;
/// <summary>
/// Microseconds: 0..999
/// </summary>
UInt16 micros;
};
#pragma endregion
#pragma region PCANBasic class
/// <summary>
/// PCAN-Basic API class implementation
/// </summary>
public ref class PCANBasic abstract sealed
{
public:
#pragma region PCAN-BUS Handles Definition
/// <summary>
/// Undefined/default value for a PCAN bus
/// </summary>
static const TPCANHandle PCAN_NONEBUS = 0x00;
/// <summary>
/// PCAN-ISA interface, channel 1
/// </summary>
static const TPCANHandle PCAN_ISABUS1 = 0x21;
/// <summary>
/// PCAN-ISA interface, channel 2
/// </summary>
static const TPCANHandle PCAN_ISABUS2 = 0x22;
/// <summary>
/// PCAN-ISA interface, channel 3
/// </summary>
static const TPCANHandle PCAN_ISABUS3 = 0x23;
/// <summary>
/// PCAN-ISA interface, channel 4
/// </summary>
static const TPCANHandle PCAN_ISABUS4 = 0x24;
/// <summary>
/// PCAN-ISA interface, channel 5
/// </summary>
static const TPCANHandle PCAN_ISABUS5 = 0x25;
/// <summary>
/// PCAN-ISA interface, channel 6
/// </summary>
static const TPCANHandle PCAN_ISABUS6 = 0x26;
/// <summary>
/// PCAN-ISA interface, channel 7
/// </summary>
static const TPCANHandle PCAN_ISABUS7 = 0x27;
/// <summary>
/// PCAN-ISA interface, channel 8
/// </summary>
static const TPCANHandle PCAN_ISABUS8 = 0x28;
/// <summary>
/// PPCAN-Dongle/LPT interface, channel 1
/// </summary>
static const TPCANHandle PCAN_DNGBUS1 = 0x31;
/// <summary>
/// PCAN-PCI interface, channel 1
/// </summary>
static const TPCANHandle PCAN_PCIBUS1 = 0x41;
/// <summary>
/// PCAN-PCI interface, channel 2
/// </summary>
static const TPCANHandle PCAN_PCIBUS2 = 0x42;
/// <summary>
/// PCAN-PCI interface, channel 3
/// </summary>
static const TPCANHandle PCAN_PCIBUS3 = 0x43;
/// <summary>
/// PCAN-PCI interface, channel 4
/// </summary>
static const TPCANHandle PCAN_PCIBUS4 = 0x44;
/// <summary>
/// PCAN-PCI interface, channel 5
/// </summary>
static const TPCANHandle PCAN_PCIBUS5 = 0x45;
/// <summary>
/// PCAN-PCI interface, channel 6
/// </summary>
static const TPCANHandle PCAN_PCIBUS6 = 0x46;
/// <summary>
/// PCAN-PCI interface, channel 7
/// </summary>
static const TPCANHandle PCAN_PCIBUS7 = 0x47;
/// <summary>
/// PCAN-PCI interface, channel 8
/// </summary>
static const TPCANHandle PCAN_PCIBUS8 = 0x48;
/// <summary>
/// PCAN-USB interface, channel 1
/// </summary>
static const TPCANHandle PCAN_USBBUS1 = 0x51;
/// <summary>
/// PCAN-USB interface, channel 2
/// </summary>
static const TPCANHandle PCAN_USBBUS2 = 0x52;
/// <summary>
/// PCAN-USB interface, channel 3
/// </summary>
static const TPCANHandle PCAN_USBBUS3 = 0x53;
/// <summary>
/// PCAN-USB interface, channel 4
/// </summary>
static const TPCANHandle PCAN_USBBUS4 = 0x54;
/// <summary>
/// PCAN-USB interface, channel 5
/// </summary>
static const TPCANHandle PCAN_USBBUS5 = 0x55;
/// <summary>
/// PCAN-USB interface, channel 6
/// </summary>
static const TPCANHandle PCAN_USBBUS6 = 0x56;
/// <summary>
/// PCAN-USB interface, channel 7
/// </summary>
static const TPCANHandle PCAN_USBBUS7 = 0x57;
/// <summary>
/// PCAN-USB interface, channel 8
/// </summary>
static const TPCANHandle PCAN_USBBUS8 = 0x58;
/// <summary>
/// PCAN-PC Card interface, channel 1
/// </summary>
static const TPCANHandle PCAN_PCCBUS1 = 0x61;
/// <summary>
/// PCAN-PC Card interface, channel 2
/// </summary>
static const TPCANHandle PCAN_PCCBUS2 = 0x62;
#pragma endregion
#pragma region Parameter values definition
/// <summary>
/// The PCAN parameter is not set (inactive)
/// </summary>
static const int PCAN_PARAMETER_OFF = 0;
/// <summary>
/// The PCAN parameter is set (active)
/// </summary>
static const int PCAN_PARAMETER_ON = 1;
/// <summary>
/// The PCAN filter is closed. No messages will be received
/// </summary>
static const int PCAN_FILTER_CLOSE = 0;
/// <summary>
/// The PCAN filter is fully opened. All messages will be received
/// </summary>
static const int PCAN_FILTER_OPEN = 1;
/// <summary>
/// The PCAN filter is custom configured. Only registered
/// messages will be received
/// </summary>
static const int PCAN_FILTER_CUSTOM = 2;
/// <summary>
/// The PCAN-Channel handle is illegal, or its associated hadware is not available
/// </summary>
static const int PCAN_CHANNEL_UNAVAILABLE = 0;
/// <summary>
/// The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
/// </summary>
static const int PCAN_CHANNEL_AVAILABLE = 1;
/// <summary>
/// The PCAN-Channel handle is valid, and is already being used
/// </summary>
static const int PCAN_CHANNEL_OCCUPIED = 2;
/// <summary>
/// Logs system exceptions / errors
/// </summary>
static const int LOG_FUNCTION_DEFAULT = 0x00;
/// <summary>
/// Logs the entries to the PCAN-Basic API functions
/// </summary>
static const int LOG_FUNCTION_ENTRY = 0x01;
/// <summary>
/// Logs the parameters passed to the PCAN-Basic API functions
/// </summary>
static const int LOG_FUNCTION_PARAMETERS = 0x02;
/// <summary>
/// Logs the exits from the PCAN-Basic API functions
/// </summary>
static const int LOG_FUNCTION_LEAVE = 0x04;
/// <summary>
/// Logs the CAN messages passed to the CAN_Write function
/// </summary>
static const int LOG_FUNCTION_WRITE = 0x08;
/// <summary>
/// Logs the CAN messages received within the CAN_Read function
/// </summary>
static const int LOG_FUNCTION_READ = 0x10;
/// <summary>
/// Logs all possible information within the PCAN-Basic API functions
/// </summary>
static const int LOG_FUNCTION_ALL = 0xFFFF;
#pragma endregion
#pragma region PCANBasic API Implementation
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
/// <param name="HwType">NON PLUG&PLAY: The type of hardware and operation mode</param>
/// <param name="IOPort">NON PLUG&PLAY: The I/O address for the parallel port</param>
/// <param name="Interrupt">NON PLUG&PLAY: Interrupt number of the parallel por</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")]
static TPCANStatus Initialize(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType::U2)]
TPCANBaudrate Btr0Btr1,
[MarshalAs(UnmanagedType::U1)]
TPCANType HwType,
UInt32 IOPort,
UInt16 Interrupt);
/// <summary>
/// Initializes a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
/// <returns>A TPCANStatus error code</returns>
static TPCANStatus Initialize(
TPCANHandle Channel,
TPCANBaudrate Btr0Btr1)
{
return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0);
}
/// <summary>
/// Uninitializes one or all PCAN Channels initialized by CAN_Initialize
/// </summary>
/// <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
/// uninitialize all initialized channels</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Uninitialize")]
static TPCANStatus Uninitialize(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel);
/// <summary>
/// Resets the receive and transmit queues of the PCAN Channel
/// </summary>
/// <remarks>A reset of the CAN controller is not performed</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Reset")]
static TPCANStatus Reset(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel);
/// <summary>
/// Gets the current status of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetStatus")]
static TPCANStatus GetStatus(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel);
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
/// <param name="TimestampBuffer">A TPCANTimestamp structure buffer to get
/// the reception time of the message</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
static TPCANStatus Read(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
TPCANMsg %MessageBuffer,
TPCANTimestamp %TimestampBuffer);
/// <summary>
/// Reads a CAN message from the receive queue of a PCAN Channel
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
static TPCANStatus Read(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
TPCANMsg %MessageBuffer);
/// <summary>
/// Transmits a CAN message
/// </summary>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="MessageBuffer">A TPCANMsg buffer with the message to be sent</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")]
static TPCANStatus Write(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
TPCANMsg %MessageBuffer);
/// <summary>
/// Configures the reception filter
/// </summary>
/// <remarks>The message filter will be expanded with every call to
/// this function. If it is desired to reset the filter, please use
/// the 'SetValue' function</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="FromID">The lowest CAN ID to be received</param>
/// <param name="ToID">The highest CAN ID to be received</param>
/// <param name="Mode">Message type, Standard (11-bit identifier) or
/// Extended (29-bit identifier)</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_FilterMessages")]
static TPCANStatus FilterMessages(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
UInt32 FromID,
UInt32 ToID,
[MarshalAs(UnmanagedType::U1)]
TPCANMode Mode);
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to get</param>
/// <param name="StringBuffer">Buffer for the parameter value</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
static TPCANStatus GetValue(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType::U1)]
TPCANParameter Parameter,
StringBuilder^ StringBuffer,
UInt32 BufferLength);
/// <summary>
/// Retrieves a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to get</param>
/// <param name="NumericBuffer">Buffer for the parameter value</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
static TPCANStatus GetValue(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType::U1)]
TPCANParameter Parameter,
UInt32 %NumericBuffer,
UInt32 BufferLength);
/// <summary>
/// Configures a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter">The TPCANParameter parameter to set</param>
/// <param name="NumericBuffer">Buffer with the value to be set</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
static TPCANStatus SetValue(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType::U1)]
TPCANParameter Parameter,
UInt32% NumericBuffer,
UInt32 BufferLength);
/// <summary>
/// Configures a PCAN Channel value
/// </summary>
/// <remarks>Parameters can be present or not according with the kind
/// of Hardware (PCAN Channel) being used. If a parameter is not available,
/// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
/// <param name="Channel">The handle of a PCAN Channel</param>
/// <param name="Parameter"></param>
/// <param name="StringBuffer">Buffer with the value to be set</param>
/// <param name="BufferLength">Size in bytes of the buffer</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
static TPCANStatus SetValue(
[MarshalAs(UnmanagedType::U1)]
TPCANHandle Channel,
[MarshalAs(UnmanagedType::U1)]
TPCANParameter Parameter,
[MarshalAs(UnmanagedType::LPStr,SizeParamIndex=3)]
String^ StringBuffer,
UInt32 BufferLength);
/// <summary>
/// Returns a descriptive text of a given TPCANStatus error
/// code, in any desired language
/// </summary>
/// <remarks>The current languages available for translation are:
/// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
/// Italian (0x10) and French (0x0C)</remarks>
/// <param name="Error">A TPCANStatus error code</param>
/// <param name="Language">Indicates a 'Primary language ID'</param>
/// <param name="StringBuffer">Buffer for the text (must be at least 256 in length)</param>
/// <returns>A TPCANStatus error code</returns>
[DllImport("PCANBasic.dll", EntryPoint = "CAN_GetErrorText")]
static TPCANStatus GetErrorText(
[MarshalAs(UnmanagedType::U4)]
TPCANStatus Error,
UInt16 Language,
StringBuilder^ StringBuffer);
#pragma endregion
};
#pragma endregion
}
}
}