commit b4631bbd9a4e651a32811d4a9885ce7c4d7d6b50 Author: Christian Sültrop Date: Fri Jul 6 14:52:16 2012 +0200 A Python library for sending and receiving CAN messages via the PEAK PCan adapter. Initial commit. diff --git a/CANLibrary/CanMessage.py b/CANLibrary/CanMessage.py new file mode 100644 index 0000000..97dc56f --- /dev/null +++ b/CANLibrary/CanMessage.py @@ -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 + + + diff --git a/CANLibrary/CanMessage.pyc b/CANLibrary/CanMessage.pyc new file mode 100644 index 0000000..ec4132b Binary files /dev/null and b/CANLibrary/CanMessage.pyc differ diff --git a/CANLibrary/CanSignal.py b/CANLibrary/CanSignal.py new file mode 100644 index 0000000..1ef1fbb --- /dev/null +++ b/CANLibrary/CanSignal.py @@ -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 \ No newline at end of file diff --git a/CANLibrary/CanSignal.pyc b/CANLibrary/CanSignal.pyc new file mode 100644 index 0000000..e559fcb Binary files /dev/null and b/CANLibrary/CanSignal.pyc differ diff --git a/CANLibrary/PCan.py b/CANLibrary/PCan.py new file mode 100644 index 0000000..322a423 --- /dev/null +++ b/CANLibrary/PCan.py @@ -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') \ No newline at end of file diff --git a/CANLibrary/PCan.pyc b/CANLibrary/PCan.pyc new file mode 100644 index 0000000..0e6c088 Binary files /dev/null and b/CANLibrary/PCan.pyc differ diff --git a/CANLibrary/__init__.py b/CANLibrary/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CANLibrary/__init__.pyc b/CANLibrary/__init__.pyc new file mode 100644 index 0000000..eacedfe Binary files /dev/null and b/CANLibrary/__init__.pyc differ diff --git a/CANLibrary/example.py b/CANLibrary/example.py new file mode 100644 index 0000000..25cff04 --- /dev/null +++ b/CANLibrary/example.py @@ -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' + + + + + + diff --git a/PCANBasic/PCANBasic.bas b/PCANBasic/PCANBasic.bas new file mode 100644 index 0000000..992cb6f --- /dev/null +++ b/PCANBasic/PCANBasic.bas @@ -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 + + +''' +''' Initializes a PCAN Channel +''' +''' "The handle of a PCAN Channel" +''' "The speed for the communication (BTR0BTR1 code)" +''' "NON PLUG&PLAY: The type of hardware and operation mode" +''' "NON PLUG&PLAY: The I/O address for the parallel port" +''' "NON PLUG&PLAY: Interrupt number of the parallel port" +''' "A TPCANStatus error code" +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 + + +''' +''' Uninitializes one or all PCAN Channels initialized by CAN_Initialize +''' +''' Giving the TPCANHandle value "PCAN_NONEBUS", +''' uninitialize all initialized channels +''' "The handle of a PCAN Channel" +''' "A TPCANStatus error code" +Public Declare Function CAN_Uninitialize Lib "PCANBasic.DLL" _ + (ByVal Channel As Byte) As Long + + +''' +''' Resets the receive and transmit queues of the PCAN Channel +''' +''' +''' A reset of the CAN controller is not performed. +''' +''' "The handle of a PCAN Channel" +''' "A TPCANStatus error code" +Public Declare Function CAN_Reset Lib "PCANBasic.DLL" _ + (ByVal Channel As Byte) As Long + + +''' +''' Gets the current status of a PCAN Channel +''' +''' "The handle of a PCAN Channel" +''' "A TPCANStatus error code" +Public Declare Function CAN_GetStatus Lib "PCANBasic.DLL" _ + (ByVal Channel As Byte) As Long + + +''' +''' Reads a CAN message from the receive queue of a PCAN Channel +''' +''' "The handle of a PCAN Channel" +''' "A TPCANMsg structure buffer to store the CAN message" +''' "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" +''' "A TPCANStatus error code" +Public Declare Function CAN_Read Lib "PCANBasic.DLL" _ + (ByVal Channel As Byte, _ + ByRef MessageBuffer As TPCANMsg, _ + ByRef TimestampBuffer As TPCANTimestamp) As Long + + +''' +''' Transmits a CAN message +''' +''' "The handle of a PCAN Channel" +''' "A TPCANMsg buffer with the message to be sent" +''' "A TPCANStatus error code" +Public Declare Function CAN_Write Lib "PCANBasic.DLL" _ + (ByVal Channel As Byte, _ + ByRef MessageBuffer As TPCANMsg) As Long + + +''' +''' Configures the reception filter. +''' +''' 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 +''' "The handle of a PCAN Channel" +''' "The lowest CAN ID to be received" +''' "The highest CAN ID to be received" +''' "Message type, Standard (11-bit identifier) or +''' Extended (29-bit identifier)" +''' "A TPCANStatus error code" +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 + + +''' +''' Retrieves a PCAN Channel value +''' +''' 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 handle of a PCAN Channel" +''' "The TPCANParameter parameter to get" +''' "Buffer for the parameter value" +''' "Size in bytes of the buffer" +''' "A TPCANStatus error code" +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 + + +''' +''' Configures or sets a PCAN Channel value +''' +''' 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 handle of a PCAN Channel" +''' "The TPCANParameter parameter to set" +''' "Buffer with the value to be set" +''' "Size in bytes of the buffer" +''' "A TPCANStatus error code" +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 + + +' +' Returns a descriptive text of a given TPCANStatus error +' code, in any desired language +' +' The current languages available for translation are: +' Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A), +' Italian (0x10) and French (0x0C) +' "A TPCANStatus error code" +' "Indicates a 'Primary language ID'" +' "Buffer for a null terminated char array" +' "A TPCANStatus error code" +Public Declare Function CAN_GetErrorText Lib "PCANBasic.DLL" _ + (ByVal ErrorCode As Long, _ + ByVal Language As Integer, _ + ByVal Buffer As String) As Long \ No newline at end of file diff --git a/PCANBasic/PCANBasic.cs b/PCANBasic/PCANBasic.cs new file mode 100644 index 0000000..b5fe43f --- /dev/null +++ b/PCANBasic/PCANBasic.cs @@ -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 + /// + /// Represents a PCAN status/error code + /// + [Flags] + public enum TPCANStatus : uint + { + /// + /// No error + /// + PCAN_ERROR_OK = 0x00000, + /// + /// Transmit buffer in CAN controller is full + /// + PCAN_ERROR_XMTFULL = 0x00001, + /// + /// CAN controller was read too late + /// + PCAN_ERROR_OVERRUN = 0x00002, + /// + /// Bus error: an error counter reached the 'light' limit + /// + PCAN_ERROR_BUSLIGHT = 0x00004, + /// + /// Bus error: an error counter reached the 'heavy' limit + /// + PCAN_ERROR_BUSHEAVY = 0x00008, + /// + /// Bus error: the CAN controller is in bus-off state + /// + PCAN_ERROR_BUSOFF = 0x00010, + /// + /// Mask for all bus errors + /// + PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF), + /// + /// Receive queue is empty + /// + PCAN_ERROR_QRCVEMPTY = 0x00020, + /// + /// Receive queue was read too late + /// + PCAN_ERROR_QOVERRUN = 0x00040, + /// + /// Transmit queue is full + /// + PCAN_ERROR_QXMTFULL = 0x00080, + /// + /// Test of the CAN controller hardware registers failed (no hardware found) + /// + PCAN_ERROR_REGTEST = 0x00100, + /// + /// Driver not loaded + /// + PCAN_ERROR_NODRIVER = 0x00200, + /// + /// Hardware already in use by a Net + /// + PCAN_ERROR_HWINUSE = 0x00400, + /// + /// A Client is already connected to the Net + /// + PCAN_ERROR_NETINUSE = 0x00800, + /// + /// Hardware handle is invalid + /// + PCAN_ERROR_ILLHW = 0x01400, + /// + /// Net handle is invalid + /// + PCAN_ERROR_ILLNET = 0x01800, + /// + /// Client handle is invalid + /// + PCAN_ERROR_ILLCLIENT = 0x01C00, + /// + /// Mask for all handle errors + /// + PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT), + /// + /// Resource (FIFO, Client, timeout) cannot be created + /// + PCAN_ERROR_RESOURCE = 0x02000, + /// + /// Invalid parameter + /// + PCAN_ERROR_ILLPARAMTYPE = 0x04000, + /// + /// Invalid parameter value + /// + PCAN_ERROR_ILLPARAMVAL = 0x08000, + /// + /// Unknow error + /// + PCAN_ERROR_UNKNOWN = 0x10000, + /// + /// Invalid data, function, or action. + /// + PCAN_ERROR_ILLDATA = 0x20000, + /// + /// Channel is not initialized + /// + PCAN_ERROR_INITIALIZE = 0x40000, + } + + /// + /// Represents a PCAN device + /// + public enum TPCANDevice : byte + { + /// + /// Undefined, unknown or not selected PCAN device value + /// + PCAN_NONE = 0, + /// + /// PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API + /// + PCAN_PEAKCAN = 1, + /// + /// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus + /// + PCAN_ISA = 2, + /// + /// PCAN-Dongle + /// + PCAN_DNG = 3, + /// + /// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express + /// + PCAN_PCI = 4, + /// + /// PCAN-USB and PCAN-USB Pro + /// + PCAN_USB = 5, + /// + /// PCAN-PC Card + /// + PCAN_PCC = 6 + } + + /// + /// Represents a PCAN parameter to be read or set + /// + public enum TPCANParameter : byte + { + /// + /// PCAN-USB device number parameter + /// + PCAN_DEVICE_NUMBER = 1, + /// + /// PCAN-PC Card 5-Volt power parameter + /// + PCAN_5VOLTS_POWER = 2, + /// + /// PCAN receive event handler parameter + /// + PCAN_RECEIVE_EVENT = 3, + /// + /// PCAN message filter parameter + /// + PCAN_MESSAGE_FILTER = 4, + /// + /// PCAN-Basic API version parameter + /// + PCAN_API_VERSION = 5, + /// + /// PCAN device channel version parameter + /// + PCAN_CHANNEL_VERSION = 6, + /// + /// PCAN Reset-On-Busoff parameter + /// + PCAN_BUSOFF_AUTORESET = 7, + /// + /// PCAN Listen-Only parameter + /// + PCAN_LISTEN_ONLY = 8, + /// + /// Directory path for trace files + /// + PCAN_LOG_LOCATION = 9, + /// + /// Debug-Trace activation status + /// + PCAN_LOG_STATUS = 10, + /// + /// Configuration of the debugged information (LOG_FUNCTION_***) + /// + PCAN_LOG_CONFIGURE = 11, + /// + /// Custom insertion of text into the log file + /// + PCAN_LOG_TEXT = 12, + /// + /// Availability status of a PCAN-Channel + /// + PCAN_CHANNEL_CONDITION = 13, + /// + /// PCAN hardware name parameter + /// + PCAN_HARDWARE_NAME = 14, + /// + /// Message reception status of a PCAN-Channel + /// + PCAN_RECEIVE_STATUS = 15, + /// + /// CAN-Controller number of a PCAN-Channel + /// + PCAN_CONTROLLER_NUMBER = 16, + } + + /// + /// Represents the type of a PCAN message + /// + [Flags] + public enum TPCANMessageType : byte + { + /// + /// The PCAN message is a CAN Standard Frame (11-bit identifier) + /// + PCAN_MESSAGE_STANDARD = 0x00, + /// + /// The PCAN message is a CAN Remote-Transfer-Request Frame + /// + PCAN_MESSAGE_RTR = 0x01, + /// + /// The PCAN message is a CAN Extended Frame (29-bit identifier) + /// + PCAN_MESSAGE_EXTENDED = 0x02, + /// + /// The PCAN message represents a PCAN status message + /// + PCAN_MESSAGE_STATUS = 0x80, + } + + /// + /// Represents a PCAN filter mode + /// + public enum TPCANMode : byte + { + /// + /// Mode is Standard (11-bit identifier) + /// + PCAN_MODE_STANDARD = TPCANMessageType.PCAN_MESSAGE_STANDARD, + /// + /// Mode is Extended (29-bit identifier) + /// + PCAN_MODE_EXTENDED = TPCANMessageType.PCAN_MESSAGE_EXTENDED, + } + + /// + /// Represents a PCAN Baud rate register value + /// + public enum TPCANBaudrate : ushort + { + /// + /// 1 MBit/s + /// + PCAN_BAUD_1M = 0x0014, + /// + /// 800 KBit/s + /// + PCAN_BAUD_800K = 0x0016, + /// + /// 500 kBit/s + /// + PCAN_BAUD_500K = 0x001C, + /// + /// 250 kBit/s + /// + PCAN_BAUD_250K = 0x011C, + /// + /// 125 kBit/s + /// + PCAN_BAUD_125K = 0x031C, + /// + /// 100 kBit/s + /// + PCAN_BAUD_100K = 0x432F, + /// + /// 95,238 KBit/s + /// + PCAN_BAUD_95K = 0xC34E, + /// + /// 83,333 KBit/s + /// + PCAN_BAUD_83K = 0x4B14, + /// + /// 50 kBit/s + /// + PCAN_BAUD_50K = 0x472F, + /// + /// 47,619 KBit/s + /// + PCAN_BAUD_47K = 0x1414, + /// + /// 33,333 KBit/s + /// + PCAN_BAUD_33K = 0x1D14, + /// + /// 20 kBit/s + /// + PCAN_BAUD_20K = 0x532F, + /// + /// 10 kBit/s + /// + PCAN_BAUD_10K = 0x672F, + /// + /// 5 kBit/s + /// + PCAN_BAUD_5K = 0x7F7F, + } + + /// + /// Represents the type of PCAN (non plug&play) hardware to be initialized + /// + public enum TPCANType : byte + { + /// + /// PCAN-ISA 82C200 + /// + PCAN_TYPE_ISA = 0x01, + /// + /// PCAN-ISA SJA1000 + /// + PCAN_TYPE_ISA_SJA = 0x09, + /// + /// PHYTEC ISA + /// + PCAN_TYPE_ISA_PHYTEC = 0x04, + /// + /// PCAN-Dongle 82C200 + /// + PCAN_TYPE_DNG = 0x02, + /// + /// PCAN-Dongle EPP 82C200 + /// + PCAN_TYPE_DNG_EPP = 0x03, + /// + /// PCAN-Dongle SJA1000 + /// + PCAN_TYPE_DNG_SJA = 0x05, + /// + /// PCAN-Dongle EPP SJA1000 + /// + PCAN_TYPE_DNG_SJA_EPP = 0x06, + } + #endregion + + #region Structures + /// + /// Represents a PCAN message + /// + public struct TPCANMsg + { + /// + /// 11/29-bit message identifier + /// + public uint ID; + /// + /// Type of the message + /// + [MarshalAs(UnmanagedType.U1)] + public TPCANMessageType MSGTYPE; + /// + /// Data Length Code of the message (0..8) + /// + public byte LEN; + /// + /// Data of the message (DATA[0]..DATA[7]) + /// + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + public byte[] DATA; + } + + /// + /// Represents a timestamp of a received PCAN message. + /// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow + /// + public struct TPCANTimestamp + { + /// + /// Base-value: milliseconds: 0.. 2^32-1 + /// + public uint millis; + /// + /// Roll-arounds of millis + /// + public ushort millis_overflow; + /// + /// Microseconds: 0..999 + /// + public ushort micros; + } + #endregion + + #region PCANBasic class + /// + /// PCAN-Basic API class implementation + /// + public static class PCANBasic + { + #region PCAN-BUS Handles Definition + /// + /// Undefined/default value for a PCAN bus + /// + public const TPCANHandle PCAN_NONEBUS = 0x00; + + /// + /// PCAN-ISA interface, channel 1 + /// + public const TPCANHandle PCAN_ISABUS1 = 0x21; + /// + /// PCAN-ISA interface, channel 2 + /// + public const TPCANHandle PCAN_ISABUS2 = 0x22; + /// + /// PCAN-ISA interface, channel 3 + /// + public const TPCANHandle PCAN_ISABUS3 = 0x23; + /// + /// PCAN-ISA interface, channel 4 + /// + public const TPCANHandle PCAN_ISABUS4 = 0x24; + /// + /// PCAN-ISA interface, channel 5 + /// + public const TPCANHandle PCAN_ISABUS5 = 0x25; + /// + /// PCAN-ISA interface, channel 6 + /// + public const TPCANHandle PCAN_ISABUS6 = 0x26; + /// + /// PCAN-ISA interface, channel 7 + /// + public const TPCANHandle PCAN_ISABUS7 = 0x27; + /// + /// PCAN-ISA interface, channel 8 + /// + public const TPCANHandle PCAN_ISABUS8 = 0x28; + + /// + /// PPCAN-Dongle/LPT interface, channel 1 + /// + public const TPCANHandle PCAN_DNGBUS1 = 0x31; + + /// + /// PCAN-PCI interface, channel 1 + /// + public const TPCANHandle PCAN_PCIBUS1 = 0x41; + /// + /// PCAN-PCI interface, channel 2 + /// + public const TPCANHandle PCAN_PCIBUS2 = 0x42; + /// + /// PCAN-PCI interface, channel 3 + /// + public const TPCANHandle PCAN_PCIBUS3 = 0x43; + /// + /// PCAN-PCI interface, channel 4 + /// + public const TPCANHandle PCAN_PCIBUS4 = 0x44; + /// + /// PCAN-PCI interface, channel 5 + /// + public const TPCANHandle PCAN_PCIBUS5 = 0x45; + /// + /// PCAN-PCI interface, channel 6 + /// + public const TPCANHandle PCAN_PCIBUS6 = 0x46; + /// + /// PCAN-PCI interface, channel 7 + /// + public const TPCANHandle PCAN_PCIBUS7 = 0x47; + /// + /// PCAN-PCI interface, channel 8 + /// + public const TPCANHandle PCAN_PCIBUS8 = 0x48; + + /// + /// PCAN-USB interface, channel 1 + /// + public const TPCANHandle PCAN_USBBUS1 = 0x51; + /// + /// PCAN-USB interface, channel 2 + /// + public const TPCANHandle PCAN_USBBUS2 = 0x52; + /// + /// PCAN-USB interface, channel 3 + /// + public const TPCANHandle PCAN_USBBUS3 = 0x53; + /// + /// PCAN-USB interface, channel 4 + /// + public const TPCANHandle PCAN_USBBUS4 = 0x54; + /// + /// PCAN-USB interface, channel 5 + /// + public const TPCANHandle PCAN_USBBUS5 = 0x55; + /// + /// PCAN-USB interface, channel 6 + /// + public const TPCANHandle PCAN_USBBUS6 = 0x56; + /// + /// PCAN-USB interface, channel 7 + /// + public const TPCANHandle PCAN_USBBUS7 = 0x57; + /// + /// PCAN-USB interface, channel 8 + /// + public const TPCANHandle PCAN_USBBUS8 = 0x58; + + /// + /// PCAN-PC Card interface, channel 1 + /// + public const TPCANHandle PCAN_PCCBUS1 = 0x61; + /// + /// PCAN-PC Card interface, channel 2 + /// + public const TPCANHandle PCAN_PCCBUS2 = 0x62; + #endregion + + #region Parameter values definition + /// + /// The PCAN parameter is not set (inactive) + /// + public const int PCAN_PARAMETER_OFF = 0; + /// + /// The PCAN parameter is set (active) + /// + public const int PCAN_PARAMETER_ON = 1; + /// + /// The PCAN filter is closed. No messages will be received + /// + public const int PCAN_FILTER_CLOSE = 0; + /// + /// The PCAN filter is fully opened. All messages will be received + /// + public const int PCAN_FILTER_OPEN = 1; + /// + /// The PCAN filter is custom configured. Only registered + /// messages will be received + /// + public const int PCAN_FILTER_CUSTOM = 2; + /// + /// The PCAN-Channel handle is illegal, or its associated hadware is not available + /// + public const int PCAN_CHANNEL_UNAVAILABLE = 0; + /// + /// The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in) + /// + public const int PCAN_CHANNEL_AVAILABLE = 1; + /// + /// The PCAN-Channel handle is valid, and is already being used + /// + public const int PCAN_CHANNEL_OCCUPIED = 2; + + /// + /// Logs system exceptions / errors + /// + public const int LOG_FUNCTION_DEFAULT = 0x00; + /// + /// Logs the entries to the PCAN-Basic API functions + /// + public const int LOG_FUNCTION_ENTRY = 0x01; + /// + /// Logs the parameters passed to the PCAN-Basic API functions + /// + public const int LOG_FUNCTION_PARAMETERS = 0x02; + /// + /// Logs the exits from the PCAN-Basic API functions + /// + public const int LOG_FUNCTION_LEAVE = 0x04; + /// + /// Logs the CAN messages passed to the CAN_Write function + /// + public const int LOG_FUNCTION_WRITE = 0x08; + /// + /// Logs the CAN messages received within the CAN_Read function + /// + public const int LOG_FUNCTION_READ = 0x10; + /// + /// Logs all possible information within the PCAN-Basic API functions + /// + public const int LOG_FUNCTION_ALL = 0xFFFF; + #endregion + + #region PCANBasic API Implementation + /// + /// Initializes a PCAN Channel + /// + /// The handle of a PCAN Channel + /// The speed for the communication (BTR0BTR1 code) + /// NON PLUG&PLAY: The type of hardware and operation mode + /// NON PLUG&PLAY: The I/O address for the parallel port + /// NON PLUG&PLAY: Interrupt number of the parallel por + /// A TPCANStatus error code + [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); + + /// + /// Initializes a PCAN Channel + /// + /// The handle of a PCAN Channel + /// The speed for the communication (BTR0BTR1 code) + /// A TPCANStatus error code + public static TPCANStatus Initialize( + TPCANHandle Channel, + TPCANBaudrate Btr0Btr1) + { + return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0); + } + + /// + /// Uninitializes one or all PCAN Channels initialized by CAN_Initialize + /// + /// Giving the TPCANHandle value "PCAN_NONEBUS", + /// uninitialize all initialized channels + /// The handle of a PCAN Channel + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Uninitialize")] + public static extern TPCANStatus Uninitialize( + [MarshalAs(UnmanagedType.U1)] + TPCANHandle Channel); + + /// + /// Resets the receive and transmit queues of the PCAN Channel + /// + /// A reset of the CAN controller is not performed + /// The handle of a PCAN Channel + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Reset")] + public static extern TPCANStatus Reset( + [MarshalAs(UnmanagedType.U1)] + TPCANHandle Channel); + + /// + /// Gets the current status of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetStatus")] + public static extern TPCANStatus GetStatus( + [MarshalAs(UnmanagedType.U1)] + TPCANHandle Channel); + + /// + /// Reads a CAN message from the receive queue of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANMsg structure buffer to store the CAN message + /// A TPCANTimestamp structure buffer to get + /// the reception time of the message + /// A TPCANStatus error code + [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); + + /// + /// Reads a CAN message from the receive queue of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANMsg structure buffer to store the CAN message + /// A TPCANStatus error code + public static TPCANStatus Read( + TPCANHandle Channel, + out TPCANMsg MessageBuffer) + { + return Read(Channel, out MessageBuffer, IntPtr.Zero); + } + + /// + /// Transmits a CAN message + /// + /// The handle of a PCAN Channel + /// A TPCANMsg buffer with the message to be sent + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")] + public static extern TPCANStatus Write( + [MarshalAs(UnmanagedType.U1)] + TPCANHandle Channel, + ref TPCANMsg MessageBuffer); + + /// + /// Configures the reception filter + /// + /// 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 + /// The handle of a PCAN Channel + /// The lowest CAN ID to be received + /// The highest CAN ID to be received + /// Message type, Standard (11-bit identifier) or + /// Extended (29-bit identifier) + /// A TPCANStatus error code + [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); + + /// + /// Retrieves a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to get + /// Buffer for the parameter value + /// Size in bytes of the buffer + /// A TPCANStatus error code + [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); + + /// + /// Retrieves a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to get + /// Buffer for the parameter value + /// Size in bytes of the buffer + /// A TPCANStatus error code + [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); + + /// + /// Configures or sets a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to set + /// Buffer with the value to be set + /// Size in bytes of the buffer + /// A TPCANStatus error code + [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); + + /// + /// Configures or sets a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// + /// Buffer with the value to be set + /// Size in bytes of the buffer + /// A TPCANStatus error code + [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); + + /// + /// Returns a descriptive text of a given TPCANStatus error + /// code, in any desired language + /// + /// The current languages available for translation are: + /// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A), + /// Italian (0x10) and French (0x0C) + /// A TPCANStatus error code + /// Indicates a 'Primary language ID' + /// Buffer for the text (must be at least 256 in length) + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetErrorText")] + public static extern TPCANStatus GetErrorText( + [MarshalAs(UnmanagedType.U4)] + TPCANStatus Error, + UInt16 Language, + StringBuilder StringBuffer); + #endregion + } + #endregion +} diff --git a/PCANBasic/PCANBasic.dll b/PCANBasic/PCANBasic.dll new file mode 100644 index 0000000..ba26b73 Binary files /dev/null and b/PCANBasic/PCANBasic.dll differ diff --git a/PCANBasic/PCANBasic.h b/PCANBasic/PCANBasic.h new file mode 100644 index 0000000..801ee11 --- /dev/null +++ b/PCANBasic/PCANBasic.h @@ -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 +//////////////////////////////////////////////////////////// + + +/// +/// Initializes a PCAN Channel +/// +/// "The handle of a PCAN Channel" +/// "The speed for the communication (BTR0BTR1 code)" +/// "NON PLUG&PLAY: The type of hardware and operation mode" +/// "NON PLUG&PLAY: The I/O address for the parallel port" +/// "NON PLUG&PLAY: Interrupt number of the parallel port" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_Initialize( + TPCANHandle Channel, + TPCANBaudrate Btr0Btr1, + TPCANType HwType = 0, + DWORD IOPort = 0, + WORD Interrupt = 0); + + +/// +/// Uninitializes one or all PCAN Channels initialized by CAN_Initialize +/// +/// Giving the TPCANHandle value "PCAN_NONEBUS", +/// uninitialize all initialized channels +/// "The handle of a PCAN Channel" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_Uninitialize( + TPCANHandle Channel); + + +/// +/// Resets the receive and transmit queues of the PCAN Channel +/// +/// +/// A reset of the CAN controller is not performed. +/// +/// "The handle of a PCAN Channel" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_Reset( + TPCANHandle Channel); + + +/// +/// Gets the current status of a PCAN Channel +/// +/// "The handle of a PCAN Channel" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_GetStatus( + TPCANHandle Channel); + + +/// +/// Reads a CAN message from the receive queue of a PCAN Channel +/// +/// "The handle of a PCAN Channel" +/// "A TPCANMsg structure buffer to store the CAN message" +/// "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" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_Read( + TPCANHandle Channel, + TPCANMsg* MessageBuffer, + TPCANTimestamp* TimestampBuffer); + + +/// +/// Transmits a CAN message +/// +/// "The handle of a PCAN Channel" +/// "A TPCANMsg buffer with the message to be sent" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_Write( + TPCANHandle Channel, + TPCANMsg* MessageBuffer); + + +/// +/// Configures the reception filter. +/// +/// 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 +/// "The handle of a PCAN Channel" +/// "The lowest CAN ID to be received" +/// "The highest CAN ID to be received" +/// "Message type, Standard (11-bit identifier) or +/// Extended (29-bit identifier)" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_FilterMessages( + TPCANHandle Channel, + DWORD FromID, + DWORD ToID, + TPCANMode Mode); + + +/// +/// Retrieves a PCAN Channel value +/// +/// 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 handle of a PCAN Channel" +/// "The TPCANParameter parameter to get" +/// "Buffer for the parameter value" +/// "Size in bytes of the buffer" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_GetValue( + TPCANHandle Channel, + TPCANParameter Parameter, + void* Buffer, + DWORD BufferLength); + + +/// +/// Configures or sets a PCAN Channel value +/// +/// 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 handle of a PCAN Channel" +/// "The TPCANParameter parameter to set" +/// "Buffer with the value to be set" +/// "Size in bytes of the buffer" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_SetValue( + TPCANHandle Channel, + TPCANParameter Parameter, + void* Buffer, + DWORD BufferLength); + + +/// +/// Returns a descriptive text of a given TPCANStatus error +/// code, in any desired language +/// +/// The current languages available for translation are: +/// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A), +/// Italian (0x10) and French (0x0C) +/// "A TPCANStatus error code" +/// "Indicates a 'Primary language ID'" +/// "Buffer for a null terminated char array" +/// "A TPCANStatus error code" +TPCANStatus __stdcall CAN_GetErrorText( + TPCANStatus Error, + WORD Language, + LPSTR Buffer); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/PCANBasic/PCANBasic.pas b/PCANBasic/PCANBasic.pas new file mode 100644 index 0000000..7890074 --- /dev/null +++ b/PCANBasic/PCANBasic.pas @@ -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} + /// + /// Represents a PCAN status/error code + /// + TPCANStatus = ( + /// + /// No error + /// + PCAN_ERROR_OK = $00000, + /// + /// Transmit buffer in CAN controller is full + /// + PCAN_ERROR_XMTFULL = $00001, + /// + /// CAN controller was read too late + /// + PCAN_ERROR_OVERRUN = $00002, + /// + /// Bus error: an error counter reached the 'light' limit + /// + PCAN_ERROR_BUSLIGHT = $00004, + /// + /// Bus error: an error counter reached the 'heavy' limit + /// + PCAN_ERROR_BUSHEAVY = $00008, + /// + /// Bus error: the CAN controller is in bus-off state + /// + PCAN_ERROR_BUSOFF = $00010, + /// + /// Mask for all bus errors + /// + PCAN_ERROR_ANYBUSERR = Byte(PCAN_ERROR_BUSLIGHT) Or Byte(PCAN_ERROR_BUSHEAVY) Or Byte(PCAN_ERROR_BUSOFF), + /// + /// Receive queue is empty + /// + PCAN_ERROR_QRCVEMPTY = $00020, + /// + /// Receive queue was read too late + /// + PCAN_ERROR_QOVERRUN = $00040, + /// + /// Transmit queue is full + /// + PCAN_ERROR_QXMTFULL = $00080, + /// + /// Test of the CAN controller hardware registers failed (no hardware found) + /// + PCAN_ERROR_REGTEST = $00100, + /// + /// Driver not loaded + /// + PCAN_ERROR_NODRIVER = $00200, + /// + /// Hardware already in use by a Net + /// + PCAN_ERROR_HWINUSE = $00400, + /// + /// A Client is already connected to the Net + /// + PCAN_ERROR_NETINUSE = $00800, + /// + /// Hardware handle is invalid + /// + PCAN_ERROR_ILLHW = $01400, + /// + /// Net handle is invalid + /// + PCAN_ERROR_ILLNET = $01800, + /// + /// Client handle is invalid + /// + PCAN_ERROR_ILLCLIENT = $01C00, + /// + /// Mask for all handle errors + /// + PCAN_ERROR_ILLHANDLE = Byte(PCAN_ERROR_ILLHW) Or Byte(PCAN_ERROR_ILLNET) Or Byte(PCAN_ERROR_ILLCLIENT), + /// + /// Resource (FIFO, Client, timeout) cannot be created + /// + PCAN_ERROR_RESOURCE = $02000, + /// + /// Invalid parameter + /// + PCAN_ERROR_ILLPARAMTYPE = $04000, + /// + /// Invalid parameter value + /// + PCAN_ERROR_ILLPARAMVAL = $08000, + /// + /// Unknow error + /// + PCAN_ERROR_UNKNOWN = $10000, + /// + /// Invalid data, function, or action + /// + PCAN_ERROR_ILLDATA = $20000, + /// + /// Channel is not initialized + /// + PCAN_ERROR_INITIALIZE = $40000 + ); + +{$Z1} + /// + /// Represents a PCAN device + /// + TPCANDevice = ( + /// + /// Undefined, unknown or not selected PCAN device value + /// + PCAN_NONE = 0, + /// + /// PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API + /// + PCAN_PEAKCAN = 1, + /// + /// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus + /// + PCAN_ISA = 2, + /// + /// PCAN-Dongle + /// + PCAN_DNG = 3, + /// + /// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express + /// + PCAN_PCI = 4, + /// + /// PCAN-USB and PCAN-USB Pro + /// + PCAN_USB = 5, + /// + /// PCAN-PC Card + /// + PCAN_PCC = 6 + ); + + /// + /// Represents a PCAN parameter to be read or set + /// + TPCANParameter = ( + /// + /// PCAN-USB device number parameter + /// + PCAN_DEVICE_NUMBER = 1, + /// + /// PCAN-PC Card 5-Volt power parameter + /// + PCAN_5VOLTS_POWER = 2, + /// + /// PCAN receive event handler parameter + /// + PCAN_RECEIVE_EVENT = 3, + /// + /// PCAN message filter parameter + /// + PCAN_MESSAGE_FILTER = 4, + /// + /// PCAN-Basic API version parameter + /// + PCAN_API_VERSION = 5, + /// + /// PCAN device channel version parameter + /// + PCAN_CHANNEL_VERSION = 6, + /// + /// PCAN Reset-On-Busoff parameter + /// + PCAN_BUSOFF_AUTORESET = 7, + /// + /// PCAN Listen-Only parameter + /// + PCAN_LISTEN_ONLY = 8, + /// + /// Directory path for trace files + /// + PCAN_LOG_LOCATION = 9, + /// + /// Debug-Trace activation status + /// + PCAN_LOG_STATUS = 10, + /// + /// Configuration of the debugged information (LOG_FUNCTION_***) + /// + PCAN_LOG_CONFIGURE = 11, + /// + /// Custom insertion of text into the log file + /// + PCAN_LOG_TEXT = 12, + /// + /// Availability status of a PCAN-Channel + /// + PCAN_CHANNEL_CONDITION = 13, + /// + /// PCAN hardware name parameter + /// + PCAN_HARDWARE_NAME = 14, + /// + /// Message reception status of a PCAN-Channel + /// + PCAN_RECEIVE_STATUS = 15, + /// + /// CAN-Controller number of a PCAN-Channel + /// + PCAN_CONTROLLER_NUMBER = 16 + ); + + /// + /// Represents the type of a PCAN message + /// + TPCANMessageType = ( + /// + /// The PCAN message is a CAN Standard Frame (11-bit identifier) + /// + PCAN_MESSAGE_STANDARD = $00, + /// + /// The PCAN message is a CAN Remote-Transfer-Request Frame + /// + PCAN_MESSAGE_RTR = $01, + /// + /// The PCAN message is a CAN Extended Frame (29-bit identifier) + /// + PCAN_MESSAGE_EXTENDED = $02, + /// + /// The PCAN message represents a PCAN status message + /// + PCAN_MESSAGE_STATUS = $80 + ); + + /// + /// Represents a PCAN filter mode + /// + TPCANMode = ( + /// + /// Mode is Standard (11-bit identifier) + /// + PCAN_MODE_STANDARD = Byte(PCAN_MESSAGE_STANDARD), + /// + /// Mode is Extended (29-bit identifier) + /// + PCAN_MODE_EXTENDED = Byte(PCAN_MESSAGE_EXTENDED) + ); + +{$Z2} + /// + /// Represents a PCAN Baud rate register value + /// + TPCANBaudrate = ( + /// + /// 1 MBit/s + /// + PCAN_BAUD_1M = $0014, + /// + /// 800 kBit/s + /// + PCAN_BAUD_800K = $0016, + /// + /// 500 kBit/s + /// + PCAN_BAUD_500K = $001C, + /// + /// 250 kBit/s + /// + PCAN_BAUD_250K = $011C, + /// + /// 125 kBit/s + /// + PCAN_BAUD_125K = $031C, + /// + /// 100 kBit/s + /// + PCAN_BAUD_100K = $432F, + /// + /// 95,238 kBit/s + /// + PCAN_BAUD_95K = $C34E, + /// + /// 83,333 kBit/s + /// + PCAN_BAUD_83K = $4B14, + /// + /// 50 kBit/s + /// + PCAN_BAUD_50K = $472F, + /// + /// 47,619 kBit/s + /// + PCAN_BAUD_47K = $1414, + /// + /// 33,333 kBit/s + /// + PCAN_BAUD_33K = $1D14, + /// + /// 20 kBit/s + /// + PCAN_BAUD_20K = $532F, + /// + /// 10 kBit/s + /// + PCAN_BAUD_10K = $672F, + /// + /// 5 kBit/s + /// + PCAN_BAUD_5K = $7F7F + ); + +{$Z1} + /// + /// Represents the type of PCAN (non plug&play) hardware to be initialized + /// + TPCANType = ( + /// + /// PCAN-ISA 82C200 + /// + PCAN_TYPE_ISA = $01, + /// + /// PCAN-ISA SJA1000 + /// + PCAN_TYPE_ISA_SJA = $09, + /// + /// PHYTEC ISA + /// + PCAN_TYPE_ISA_PHYTEC = $04, + /// + /// PCAN-Dongle 82C200 + /// + PCAN_TYPE_DNG = $02, + /// + /// PCAN-Dongle EPP 82C200 + /// + PCAN_TYPE_DNG_EPP = $03, + /// + /// PCAN-Dongle SJA1000 + /// + PCAN_TYPE_DNG_SJA = $05, + /// + /// PCAN-Dongle EPP SJA1000 + /// + PCAN_TYPE_DNG_SJA_EPP = $06 + ); + + /// + /// Represents a PCAN message + /// + TPCANMsg = record + /// + /// 11/29-bit message identifier + /// + ID: Longword; + /// + /// Type of the message + /// + MSGTYPE: TPCANMessageType; + /// + /// Data Length Code of the message (0..8) + /// + LEN: Byte; + /// + /// Data of the message (DATA[0]..DATA[7]) + /// + DATA: array[0..7] of Byte; + end; + + /// + /// Represents a timestamp of a received PCAN message. + /// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow + /// + TPCANTimestamp = record + /// + /// Base-value: milliseconds: 0.. 2^32-1 + /// + millis: Longword; + /// + /// Roll-arounds of millis + /// + millis_overflow: Word; + /// + /// Microseconds: 0..999 + /// + micros: Word; + end; + PTPCANTimestamp = ^TPCANTimestamp; + + /// + /// PCAN-Basic API class implementation + /// + TPCANBasic = class + public + class var + /// + /// Undefined/default value for a PCAN bus + /// + const PCAN_NONEBUS: TPCANHandle = $00; + + /// + /// PCAN-ISA interface, channel 1 + /// + const PCAN_ISABUS1: TPCANHandle = $21; + /// + /// PCAN-ISA interface, channel 2 + /// + const PCAN_ISABUS2: TPCANHandle = $22; + /// + /// PCAN-ISA interface, channel 3 + /// + const PCAN_ISABUS3: TPCANHandle = $23; + /// + /// PCAN-ISA interface, channel 4 + /// + const PCAN_ISABUS4: TPCANHandle = $24; + /// + /// PCAN-ISA interface, channel 5 + /// + const PCAN_ISABUS5: TPCANHandle = $25; + /// + /// PCAN-ISA interface, channel 6 + /// + const PCAN_ISABUS6: TPCANHandle = $26; + /// + /// PCAN-ISA interface, channel 7 + /// + const PCAN_ISABUS7: TPCANHandle = $27; + /// + /// PCAN-ISA interface, channel 8 + /// + const PCAN_ISABUS8: TPCANHandle = $28; + + /// + /// PPCAN-Dongle/LPT interface, channel 1 + /// + const PCAN_DNGBUS1: TPCANHandle = $31; + + /// + /// PCAN-PCI interface, channel 1 + /// + const PCAN_PCIBUS1: TPCANHandle = $41; + /// + /// PCAN-PCI interface, channel 2 + /// + const PCAN_PCIBUS2: TPCANHandle = $42; + /// + /// PCAN-PCI interface, channel 3 + /// + const PCAN_PCIBUS3: TPCANHandle = $43; + /// + /// PCAN-PCI interface, channel 4 + /// + const PCAN_PCIBUS4: TPCANHandle = $44; + /// + /// PCAN-PCI interface, channel 5 + /// + const PCAN_PCIBUS5: TPCANHandle = $45; + /// + /// PCAN-PCI interface, channel 6 + /// + const PCAN_PCIBUS6: TPCANHandle = $46; + /// + /// PCAN-PCI interface, channel 7 + /// + const PCAN_PCIBUS7: TPCANHandle = $47; + /// + /// PCAN-PCI interface, channel 8 + /// + const PCAN_PCIBUS8: TPCANHandle = $48; + + /// + /// PCAN-USB interface, channel 1 + /// + const PCAN_USBBUS1: TPCANHandle = $51; + /// + /// PCAN-USB interface, channel 2 + /// + const PCAN_USBBUS2: TPCANHandle = $52; + /// + /// PCAN-USB interface, channel 3 + /// + const PCAN_USBBUS3: TPCANHandle = $53; + /// + /// PCAN-USB interface, channel 4 + /// + const PCAN_USBBUS4: TPCANHandle = $54; + /// + /// PCAN-USB interface, channel 5 + /// + const PCAN_USBBUS5: TPCANHandle = $55; + /// + /// PCAN-USB interface, channel 6 + /// + const PCAN_USBBUS6: TPCANHandle = $56; + /// + /// PCAN-USB interface, channel 7 + /// + const PCAN_USBBUS7: TPCANHandle = $57; + /// + /// PCAN-USB interface, channel 8 + /// + const PCAN_USBBUS8: TPCANHandle = $58; + + /// + /// PCAN-PC Card interface, channel 1 + /// + const PCAN_PCCBUS1: TPCANHandle = $61; + /// + /// PCAN-PC Card interface, channel 2 + /// + const PCAN_PCCBUS2: TPCANHandle = $62; + + /// + /// The PCAN parameter is not set (inactive) + /// + const PCAN_PARAMETER_OFF: Integer = 0; + /// + /// The PCAN parameter is set (active) + /// + const PCAN_PARAMETER_ON: Integer = 1; + /// + /// The PCAN filter is closed. No messages will be received + /// + const PCAN_FILTER_CLOSE: Integer = 0; + /// + /// The PCAN filter is fully opened. All messages will be received + /// + const PCAN_FILTER_OPEN: Integer = 1; + /// + /// The PCAN filter is custom configured. Only registered + /// messages will be received + /// + const PCAN_FILTER_CUSTOM: Integer = 2; + /// + /// The PCAN-Channel handle is illegal, or its associated hadware is not available + /// + const PCAN_CHANNEL_UNAVAILABLE: Integer = 0; + /// + /// The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in) + /// + const PCAN_CHANNEL_AVAILABLE: Integer = 1; + /// + /// The PCAN-Channel handle is valid, and is already being used + /// + const PCAN_CHANNEL_OCCUPIED: Integer = 2; + + /// + /// Logs system exceptions / errors + /// + const LOG_FUNCTION_DEFAULT: Integer = $00; + /// + /// Logs the entries to the PCAN-Basic API functions + /// + const LOG_FUNCTION_ENTRY: Integer = $01; + /// + /// Logs the parameters passed to the PCAN-Basic API functions + /// + const LOG_FUNCTION_PARAMETERS: Integer = $02; + /// + /// Logs the exits from the PCAN-Basic API functions + /// + const LOG_FUNCTION_LEAVE: Integer = $04; + /// + /// Logs the CAN messages passed to the CAN_Write function + /// + const LOG_FUNCTION_WRITE: Integer = $08; + /// + /// Logs the CAN messages received within the CAN_Read function + /// + const LOG_FUNCTION_READ: Integer = $10; + /// + /// Logs all possible information within the PCAN-Basic API functions + /// + const LOG_FUNCTION_ALL: Integer = $FFFF; + + /// + /// Initializes a PCAN Channel + /// + /// The handle of a PCAN Channel + /// The speed for the communication (BTR0BTR1 code) + /// NON PLUG&PLAY: The type of hardware and operation mode + /// NON PLUG&PLAY: The I/O address for the parallel port + /// NON PLUG&PLAY: Interrupt number of the parallel port + /// A TPCANStatus error code + class function Initialize( + Channel: TPCANHandle; + Btr0Btr1: TPCANBaudrate; + HwType: TPCANType; + IOPort: LongWord; + Interrupt: Word + ): TPCANStatus; overload; + + /// + /// Initializes a PCAN Channel + /// + /// The handle of a PCAN Channel + /// The speed for the communication (BTR0BTR1 code) + /// A TPCANStatus error code + class function Initialize( + Channel: TPCANHandle; + Btr0Btr1: TPCANBaudrate + ): TPCANStatus; overload; + + /// + /// Uninitializes one or all PCAN Channels initialized by CAN_Initialize + /// + /// Giving the TPCANHandle value "PCAN_NONEBUS", + /// uninitialize all initialized channels + /// The handle of a PCAN Channel + /// A TPCANStatus error code + class function Uninitialize( + Channel: TPCANHandle + ): TPCANStatus; + + /// + /// Resets the receive and transmit queues of the PCAN Channel + /// + /// A reset of the CAN controller is not performed + /// The handle of a PCAN Channel + /// A TPCANStatus error code + class function Reset( + Channel: TPCANHandle + ): TPCANStatus; + + /// + /// Gets the current status of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANStatus error code + class function GetStatus( + Channel: TPCANHandle + ): TPCANStatus; + + /// + /// Reads a CAN message from the receive queue of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANMsg structure buffer to store the CAN message + /// A TPCANTimestamp structure buffer to get + /// the reception time of the message + /// A TPCANStatus error code + class function Read( + Channel: TPCANHandle; + var MessageBuffer: TPCANMsg; + var TimestampBuffer: TPCANTimestamp + ):TPCANStatus; overload; + + /// + /// Reads a CAN message from the receive queue of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANMsg structure buffer to store the CAN message + /// A TPCANStatus error code + class function Read( + Channel: TPCANHandle; + var MessageBuffer: TPCANMsg + ): TPCANStatus; overload; + + /// + /// Transmits a CAN message + /// + /// The handle of a PCAN Channel + /// A TPCANMsg buffer with the message to be sent + /// A TPCANStatus error code + class function Write( + Channel: TPCANHandle; + var MessageBuffer: TPCANMsg + ): TPCANStatus; + + /// + /// Configures the reception filter + /// + /// 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 + /// The handle of a PCAN Channel + /// The lowest CAN ID to be received + /// The highest CAN ID to be received + /// Message type, Standard (11-bit identifier) or + /// Extended (29-bit identifier) + /// A TPCANStatus error code + class function FilterMessages( + Channel: TPCANHandle; + FromID: LongWord; + ToID: LongWord; + Mode: TPCANMode + ): TPCANStatus; + + /// + /// Retrieves a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to get + /// Buffer for the parameter value + /// Size in bytes of the buffer + /// A TPCANStatus error code + class function GetValue( + Channel: TPCANHandle; + Parameter: TPCANParameter; + NumericBuffer: PLongWord; + BufferLength: LongWord + ): TPCANStatus; overload; + + + /// + /// Retrieves a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to get + /// Buffer for the parameter value + /// Size in bytes of the buffer + /// A TPCANStatus error code + class function GetValue( + Channel: TPCANHandle; + Parameter: TPCANParameter; + StringBuffer: PAnsiChar; + BufferLength: LongWord + ): TPCANStatus; overload; + + /// + /// Configures or sets a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to set + /// Buffer with the value to be set + /// Size in bytes of the buffer + /// A TPCANStatus error code + class function SetValue( + Channel: TPCANHandle; + Parameter: TPCANParameter; + NumericBuffer: PLongWord; + BufferLength: LongWord + ): TPCANStatus; overload; + + /// + /// Configures or sets a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to set + /// Buffer with the value to be set + /// Size in bytes of the buffer + /// A TPCANStatus error code + class function SetValue( + Channel: TPCANHandle; + Parameter: TPCANParameter; + StringBuffer: PAnsiChar; + BufferLength: LongWord + ): TPCANStatus; overload; + + /// + /// Returns a descriptive text of a given TPCANStatus error + /// code, in any desired language + /// + /// The current languages available for translation are: + /// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A), + /// Italian (0x10) and French (0x0C) + /// A TPCANStatus error code + /// Indicates a 'Primary language ID' + /// Buffer for the text (must be at least 256 in length) + /// A TPCANStatus error code + 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. diff --git a/PCANBasic/PCANBasic.py b/PCANBasic/PCANBasic.py new file mode 100644 index 0000000..3952fa1 --- /dev/null +++ b/PCANBasic/PCANBasic.py @@ -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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PCANBasic/PCANBasic.pyc b/PCANBasic/PCANBasic.pyc new file mode 100644 index 0000000..246ed6c Binary files /dev/null and b/PCANBasic/PCANBasic.pyc differ diff --git a/PCANBasic/PCANBasic.vb b/PCANBasic/PCANBasic.vb new file mode 100644 index 0000000..a7d31d5 --- /dev/null +++ b/PCANBasic/PCANBasic.vb @@ -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" + ''' + ''' Represents a PCAN status/error code + ''' + _ + Public Enum TPCANStatus As UInt32 + ''' + ''' No error + ''' + PCAN_ERROR_OK = &H0 + ''' + ''' Transmit buffer in CAN controller is full + ''' + PCAN_ERROR_XMTFULL = &H1 + ''' + ''' CAN controller was read too late + ''' + PCAN_ERROR_OVERRUN = &H2 + ''' + ''' Bus error: an error counter reached the 'light' limit + ''' + PCAN_ERROR_BUSLIGHT = &H4 + ''' + ''' Bus error: an error counter reached the 'heavy' limit + ''' + PCAN_ERROR_BUSHEAVY = &H8 + ''' + ''' Bus error: the CAN controller is in bus-off state + ''' + PCAN_ERROR_BUSOFF = &H10 + ''' + ''' Mask for all bus errors + ''' + PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT Or PCAN_ERROR_BUSHEAVY Or PCAN_ERROR_BUSOFF) + ''' + ''' Receive queue is empty + ''' + PCAN_ERROR_QRCVEMPTY = &H20 + ''' + ''' Receive queue was read too late + ''' + PCAN_ERROR_QOVERRUN = &H40 + ''' + ''' Transmit queue is full + ''' + PCAN_ERROR_QXMTFULL = &H80 + ''' + ''' Test of the CAN controller hardware registers failed (no hardware found) + ''' + PCAN_ERROR_REGTEST = &H100 + ''' + ''' Driver not loaded + ''' + PCAN_ERROR_NODRIVER = &H200 + ''' + ''' Hardware already in use by a Net + ''' + PCAN_ERROR_HWINUSE = &H400 + ''' + ''' A Client is already connected to the Net + ''' + PCAN_ERROR_NETINUSE = &H800 + ''' + ''' Hardware handle is invalid + ''' + PCAN_ERROR_ILLHW = &H1400 + ''' + ''' Net handle is invalid + ''' + PCAN_ERROR_ILLNET = &H1800 + ''' + ''' Client handle is invalid + ''' + PCAN_ERROR_ILLCLIENT = &H1C00 + ''' + ''' Mask for all handle errors + ''' + PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW Or PCAN_ERROR_ILLNET Or PCAN_ERROR_ILLCLIENT) + ''' + ''' Resource (FIFO, Client, timeout) cannot be created + ''' + PCAN_ERROR_RESOURCE = &H2000 + ''' + ''' Invalid parameter + ''' + PCAN_ERROR_ILLPARAMTYPE = &H4000 + ''' + ''' Invalid parameter value + ''' + PCAN_ERROR_ILLPARAMVAL = &H8000 + ''' + ''' Unknow error + ''' + PCAN_ERROR_UNKNOWN = &H10000 + ''' + ''' Invalid data, function, or action. + ''' + PCAN_ERROR_ILLDATA = &H20000 + ''' + ''' Channel is not initialized + ''' + PCAN_ERROR_INITIALIZE = &H40000 + End Enum + + ''' + ''' Represents a PCAN device + ''' + Public Enum TPCANDevice As Byte + ''' + ''' Undefined, unknown or not selected PCAN device value + ''' + PCAN_NONE = 0 + ''' + ''' PCAN Non-Plug And Play devices. NOT USED WITHIN PCAN-Basic API + ''' + PCAN_PEAKCAN = 1 + ''' + ''' PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus + ''' + PCAN_ISA = 2 + ''' + ''' PCAN-Dongle + ''' + PCAN_DNG = 3 + ''' + ''' PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express + ''' + PCAN_PCI = 4 + ''' + ''' PCAN-USB and PCAN-USB Pro + ''' + PCAN_USB = 5 + ''' + ''' PCAN-PC Card + ''' + PCAN_PCC = 6 + End Enum + + ''' + ''' Represents a PCAN parameter to be read or set + ''' + Public Enum TPCANParameter As Byte + ''' + ''' PCAN-USB device number parameter + ''' + PCAN_DEVICE_NUMBER = 1 + ''' + ''' PCAN-PC Card 5-Volt power parameter + ''' + PCAN_5VOLTS_POWER = 2 + ''' + ''' PCAN receive event handler parameter + ''' + PCAN_RECEIVE_EVENT = 3 + ''' + ''' PCAN message filter parameter + ''' + PCAN_MESSAGE_FILTER = 4 + ''' + ''' PCAN-Basic API version parameter + ''' + PCAN_API_VERSION = 5 + ''' + ''' PCAN device channel version parameter + ''' + PCAN_CHANNEL_VERSION = 6 + ''' + ''' PCAN Reset-On-Busoff parameter + ''' + PCAN_BUSOFF_AUTORESET = 7 + ''' + ''' PCAN Listen-Only parameter + ''' + PCAN_LISTEN_ONLY = 8 + ''' + ''' Directory path for trace files + ''' + PCAN_LOG_LOCATION = 9 + ''' + ''' Debug-Trace activation status + ''' + PCAN_LOG_STATUS = 10 + ''' + ''' Configuration of the debugged information (LOG_FUNCTION_***) + ''' + PCAN_LOG_CONFIGURE = 11 + ''' + ''' Custom insertion of text into the log file + ''' + PCAN_LOG_TEXT = 12 + ''' + ''' Availability status of a PCAN-Channel + ''' + PCAN_CHANNEL_CONDITION = 13 + ''' + ''' PCAN hardware name parameter + ''' + PCAN_HARDWARE_NAME = 14 + ''' + ''' Message reception status of a PCAN-Channel + ''' + PCAN_RECEIVE_STATUS = 15 + ''' + ''' CAN-Controller number of a PCAN-Channel + ''' + PCAN_CONTROLLER_NUMBER = 16 + End Enum + + ''' + ''' Represents the type of a PCAN message + ''' + _ + Public Enum TPCANMessageType As Byte + ''' + ''' The PCAN message is a CAN Standard Frame (11-bit identifier) + ''' + PCAN_MESSAGE_STANDARD = &H0 + ''' + ''' The PCAN message is a CAN Remote-Transfer-Request Frame + ''' + PCAN_MESSAGE_RTR = &H1 + ''' + ''' The PCAN message is a CAN Extended Frame (29-bit identifier) + ''' + PCAN_MESSAGE_EXTENDED = &H2 + ''' + ''' The PCAN message represents a PCAN status message + ''' + PCAN_MESSAGE_STATUS = &H80 + End Enum + + ''' + ''' Represents a PCAN filter mode + ''' + Public Enum TPCANMode As Byte + ''' + ''' Mode is Standard (11-bit identifier) + ''' + PCAN_MODE_STANDARD = TPCANMessageType.PCAN_MESSAGE_STANDARD + ''' + ''' Mode is Extended (29-bit identifier) + ''' + PCAN_MODE_EXTENDED = TPCANMessageType.PCAN_MESSAGE_EXTENDED + End Enum + + ''' + ''' Represents a PCAN Baud rate register value + ''' + Public Enum TPCANBaudrate As UInt16 + ''' + ''' 1 MBit/s + ''' + PCAN_BAUD_1M = &H14 + ''' + ''' 800 kBit/s + ''' + PCAN_BAUD_800K = &H16 + ''' + ''' 500 kBit/s + ''' + PCAN_BAUD_500K = &H1C + ''' + ''' 250 kBit/s + ''' + PCAN_BAUD_250K = &H11C + ''' + ''' 125 kBit/s + ''' + PCAN_BAUD_125K = &H31C + ''' + ''' 100 kBit/s + ''' + PCAN_BAUD_100K = &H432F + ''' + ''' 95,238 kBit/s + ''' + PCAN_BAUD_95K = &HC34E + ''' + ''' 83,333 kBit/s + ''' + PCAN_BAUD_83K = &H4B14 + ''' + ''' 50 kBit/s + ''' + PCAN_BAUD_50K = &H472F + ''' + ''' 47,619 kBit/s + ''' + PCAN_BAUD_47K = &H1414 + ''' + ''' 33,333 kBit/s + ''' + PCAN_BAUD_33K = &H1D14 + ''' + ''' 20 kBit/s + ''' + PCAN_BAUD_20K = &H532F + ''' + ''' 10 kBit/s + ''' + PCAN_BAUD_10K = &H672F + ''' + ''' 5 kBit/s + ''' + PCAN_BAUD_5K = &H7F7F + End Enum + + ''' + ''' Represents the type of PCAN (non plug and play) hardware to be initialized + ''' + Public Enum TPCANType As Byte + ''' + ''' PCAN-ISA 82C200 + ''' + PCAN_TYPE_ISA = &H1 + ''' + ''' PCAN-ISA SJA1000 + ''' + PCAN_TYPE_ISA_SJA = &H9 + ''' + ''' PHYTEC ISA + ''' + PCAN_TYPE_ISA_PHYTEC = &H4 + ''' + ''' PCAN-Dongle 82C200 + ''' + PCAN_TYPE_DNG = &H2 + ''' + ''' PCAN-Dongle EPP 82C200 + ''' + PCAN_TYPE_DNG_EPP = &H3 + ''' + ''' PCAN-Dongle SJA1000 + ''' + PCAN_TYPE_DNG_SJA = &H5 + ''' + ''' PCAN-Dongle EPP SJA1000 + ''' + PCAN_TYPE_DNG_SJA_EPP = &H6 + End Enum +#End Region + +#Region "Structures" + ''' + ''' Represents a PCAN message + ''' + Public Structure TPCANMsg + ''' + ''' 11/29-bit message identifier + ''' + Public ID As UInt32 + ''' + ''' Type of the message + ''' + _ + Public MSGTYPE As TPCANMessageType + ''' + ''' Data Length Code of the message (0..8) + ''' + Public LEN As Byte + ''' + ''' Data of the message (DATA[0]..DATA[7]) + ''' + _ + Public DATA As Byte() + End Structure + + ''' + ''' Represents a timestamp of a received PCAN message. + ''' Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow + ''' + Public Structure TPCANTimestamp + ''' + ''' Base-value: milliseconds: 0.. 2^32-1 + ''' + Public millis As UInt32 + ''' + ''' Roll-arounds of millis + ''' + Public millis_overflow As UInt16 + ''' + ''' Microseconds: 0..999 + ''' + Public micros As UInt16 + End Structure +#End Region + +#Region "PCANBasic class" + ''' + ''' PCAN-Basic API class implementation + ''' + Public NotInheritable Class PCANBasic +#Region "PCAN-BUS Handles Definition" + ''' + ''' Undefined/default value for a PCAN bus + ''' + Public Const PCAN_NONEBUS As TPCANHandle = &H0 + + ''' + ''' PCAN-ISA interface, channel 1 + ''' + Public Const PCAN_ISABUS1 As TPCANHandle = &H21 + ''' + ''' PCAN-ISA interface, channel 2 + ''' + Public Const PCAN_ISABUS2 As TPCANHandle = &H22 + ''' + ''' PCAN-ISA interface, channel 3 + ''' + Public Const PCAN_ISABUS3 As TPCANHandle = &H23 + ''' + ''' PCAN-ISA interface, channel 4 + ''' + Public Const PCAN_ISABUS4 As TPCANHandle = &H24 + ''' + ''' PCAN-ISA interface, channel 5 + ''' + Public Const PCAN_ISABUS5 As TPCANHandle = &H25 + ''' + ''' PCAN-ISA interface, channel 6 + ''' + Public Const PCAN_ISABUS6 As TPCANHandle = &H26 + ''' + ''' PCAN-ISA interface, channel 7 + ''' + Public Const PCAN_ISABUS7 As TPCANHandle = &H27 + ''' + ''' PCAN-ISA interface, channel 8 + ''' + Public Const PCAN_ISABUS8 As TPCANHandle = &H28 + + ''' + ''' PPCAN-Dongle/LPT interface, channel 1 + ''' + Public Const PCAN_DNGBUS1 As TPCANHandle = &H31 + + ''' + ''' PCAN-PCI interface, channel 1 + ''' + Public Const PCAN_PCIBUS1 As TPCANHandle = &H41 + ''' + ''' PCAN-PCI interface, channel 2 + ''' + Public Const PCAN_PCIBUS2 As TPCANHandle = &H42 + ''' + ''' PCAN-PCI interface, channel 3 + ''' + Public Const PCAN_PCIBUS3 As TPCANHandle = &H43 + ''' + ''' PCAN-PCI interface, channel 4 + ''' + Public Const PCAN_PCIBUS4 As TPCANHandle = &H44 + ''' + ''' PCAN-PCI interface, channel 5 + ''' + Public Const PCAN_PCIBUS5 As TPCANHandle = &H45 + ''' + ''' PCAN-PCI interface, channel 6 + ''' + Public Const PCAN_PCIBUS6 As TPCANHandle = &H46 + ''' + ''' PCAN-PCI interface, channel 7 + ''' + Public Const PCAN_PCIBUS7 As TPCANHandle = &H47 + ''' + ''' PCAN-PCI interface, channel 8 + ''' + Public Const PCAN_PCIBUS8 As TPCANHandle = &H48 + + ''' + ''' PCAN-USB interface, channel 1 + ''' + Public Const PCAN_USBBUS1 As TPCANHandle = &H51 + ''' + ''' PCAN-USB interface, channel 2 + ''' + Public Const PCAN_USBBUS2 As TPCANHandle = &H52 + ''' + ''' PCAN-USB interface, channel 3 + ''' + Public Const PCAN_USBBUS3 As TPCANHandle = &H53 + ''' + ''' PCAN-USB interface, channel 4 + ''' + Public Const PCAN_USBBUS4 As TPCANHandle = &H54 + ''' + ''' PCAN-USB interface, channel 5 + ''' + Public Const PCAN_USBBUS5 As TPCANHandle = &H55 + ''' + ''' PCAN-USB interface, channel 6 + ''' + Public Const PCAN_USBBUS6 As TPCANHandle = &H56 + ''' + ''' PCAN-USB interface, channel 7 + ''' + Public Const PCAN_USBBUS7 As TPCANHandle = &H57 + ''' + ''' PCAN-USB interface, channel 8 + ''' + Public Const PCAN_USBBUS8 As TPCANHandle = &H58 + + ''' + ''' PCAN-PC Card interface, channel 1 + ''' + Public Const PCAN_PCCBUS1 As TPCANHandle = &H61 + ''' + ''' PCAN-PC Card interface, channel 2 + ''' + Public Const PCAN_PCCBUS2 As TPCANHandle = &H62 +#End Region + +#Region "Parameter values definition" + ''' + ''' The PCAN parameter is not set (inactive) + ''' + Public Const PCAN_PARAMETER_OFF As Integer = 0 + ''' + ''' The PCAN parameter is set (active) + ''' + Public Const PCAN_PARAMETER_ON As Integer = 1 + ''' + ''' The PCAN filter is closed. No messages will be received + ''' + Public Const PCAN_FILTER_CLOSE As Integer = 0 + ''' + ''' The PCAN filter is fully opened. All messages will be received + ''' + Public Const PCAN_FILTER_OPEN As Integer = 1 + ''' + ''' The PCAN filter is custom configured. Only registered + ''' messages will be received + ''' + Public Const PCAN_FILTER_CUSTOM As Integer = 2 + ''' + ''' The PCAN-Channel handle is illegal, or its associated hadware is not available + ''' + Public Const PCAN_CHANNEL_UNAVAILABLE As Integer = 0 + ''' + ''' The PCAN-Channel handle is available to be connected (Plug and Play Hardware: it means futhermore that the hardware is plugged-in) + ''' + Public Const PCAN_CHANNEL_AVAILABLE As Integer = 1 + ''' + ''' The PCAN-Channel handle is valid, and is already being used + ''' + Public Const PCAN_CHANNEL_OCCUPIED As Integer = 2 + + ''' + ''' Logs system exceptions / errors + ''' + Public Const LOG_FUNCTION_DEFAULT As Integer = &H0 + ''' + ''' Logs the entries to the PCAN-Basic API functions + ''' + Public Const LOG_FUNCTION_ENTRY As Integer = &H1 + ''' + ''' Logs the parameters passed to the PCAN-Basic API functions + ''' + Public Const LOG_FUNCTION_PARAMETERS As Integer = &H2 + ''' + ''' Logs the exits from the PCAN-Basic API functions + ''' + Public Const LOG_FUNCTION_LEAVE As Integer = &H4 + ''' + ''' Logs the CAN messages passed to the CAN_Write function + ''' + Public Const LOG_FUNCTION_WRITE As Integer = &H8 + ''' + ''' Logs the CAN messages received within the CAN_Read function + ''' + Public Const LOG_FUNCTION_READ As Integer = &H10 + ''' + ''' Logs all possible information within the PCAN-Basic API functions + ''' + Public Const LOG_FUNCTION_ALL As Integer = &HFFFF +#End Region + +#Region "PCANBasic API Implementation" + ''' + ''' Initializes a PCAN Channel + ''' + ''' The handle of a PCAN Channel + ''' The speed for the communication (BTR0BTR1 code) + ''' NON PLUG AND PLAY: The type of hardware and operation mode + ''' NON PLUG AND PLAY: The I/O address for the parallel port + ''' NON PLUG AND PLAY: Interrupt number of the parallel por + ''' A TPCANStatus error code + _ + Public Shared Function Initialize( _ + _ + ByVal Channel As TPCANHandle, _ + _ + ByVal Btr0Btr1 As TPCANBaudrate, _ + _ + ByVal HwType As TPCANType, _ + ByVal IOPort As UInt32, _ + ByVal Interrupt As UInt16) As TPCANStatus + End Function + + ''' + ''' Initializes a PCAN Channel + ''' + ''' The handle of a PCAN Channel + ''' The speed for the communication (BTR0BTR1 code) + ''' A TPCANStatus error code + Public Shared Function Initialize( _ + ByVal Channel As TPCANHandle, _ + ByVal Btr0Btr1 As TPCANBaudrate) As TPCANStatus + Return Initialize(Channel, Btr0Btr1, 0, 0, 0) + End Function + + ''' + ''' Uninitializes one or all PCAN Channels initialized by CAN_Initialize + ''' + ''' Giving the TPCANHandle value "PCAN_NONEBUS", + ''' uninitialize all initialized channels + ''' The handle of a PCAN Channel + ''' A TPCANStatus error code + _ + Public Shared Function Uninitialize( _ + _ + ByVal Channel As TPCANHandle) As TPCANStatus + End Function + + ''' + ''' Resets the receive and transmit queues of the PCAN Channel + ''' + ''' A reset of the CAN controller is not performed + ''' The handle of a PCAN Channel + ''' A TPCANStatus error code + _ + Public Shared Function Reset( _ + _ + ByVal Channel As TPCANHandle) As TPCANStatus + End Function + + ''' + ''' Gets the current status of a PCAN Channel + ''' + ''' The handle of a PCAN Channel + ''' A TPCANStatus error code + _ + Public Shared Function GetStatus( _ + _ + ByVal Channel As TPCANHandle) As TPCANStatus + End Function + + ''' + ''' Reads a CAN message from the receive queue of a PCAN Channel + ''' + ''' The handle of a PCAN Channel + ''' A TPCANMsg structure buffer to store the CAN message + ''' A TPCANTimestamp structure buffer to get + ''' the reception time of the message + ''' A TPCANStatus error code + _ + Public Shared Function Read( _ + _ + ByVal Channel As TPCANHandle, _ + ByRef MessageBuffer As TPCANMsg, _ + ByRef TimestampBuffer As TPCANTimestamp) As TPCANStatus + End Function + + _ + Private Shared Function Read( _ + _ + ByVal Channel As TPCANHandle, _ + ByRef MessageBuffer As TPCANMsg, _ + ByVal BufferPointer As IntPtr) As TPCANStatus + End Function + + ''' + ''' Reads a CAN message from the receive queue of a PCAN Channel + ''' + ''' The handle of a PCAN Channel + ''' A TPCANMsg structure buffer to store the CAN message + ''' A TPCANStatus error code + Public Shared Function Read( _ + ByVal Channel As TPCANHandle, _ + ByRef MessageBuffer As TPCANMsg) As TPCANStatus + Return Read(Channel, MessageBuffer, IntPtr.Zero) + End Function + + ''' + ''' Transmits a CAN message + ''' + ''' The handle of a PCAN Channel + ''' A TPCANMsg buffer with the message to be sent + ''' A TPCANStatus error code + _ + Public Shared Function Write( _ + _ + ByVal Channel As TPCANHandle, _ + ByRef MessageBuffer As TPCANMsg) As TPCANStatus + End Function + + ''' + ''' Configures the reception filter + ''' + ''' 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 + ''' The handle of a PCAN Channel + ''' The lowest CAN ID to be received + ''' The highest CAN ID to be received + ''' Message type, Standard (11-bit identifier) or + ''' Extended (29-bit identifier) + ''' A TPCANStatus error code + _ + Public Shared Function FilterMessages( _ + _ + ByVal Channel As TPCANHandle, _ + ByVal FromID As UInt32, _ + ByVal ToID As UInt32, _ + _ + ByVal Mode As TPCANMode) As TPCANStatus + End Function + + ''' + ''' Retrieves a PCAN Channel value + ''' + ''' 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 handle of a PCAN Channel + ''' The TPCANParameter parameter to get + ''' Buffer for the parameter value + ''' Size in bytes of the buffer + ''' A TPCANStatus error code + _ + Public Shared Function GetValue( _ + _ + ByVal Channel As TPCANHandle, _ + _ + ByVal Parameter As TPCANParameter, _ + ByVal StringBuffer As StringBuilder, _ + ByVal BufferLength As UInt32) As TPCANStatus + End Function + + ''' + ''' Retrieves a PCAN Channel value + ''' + ''' 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 handle of a PCAN Channel + ''' The TPCANParameter parameter to get + ''' Buffer for the parameter value + ''' Size in bytes of the buffer + ''' A TPCANStatus error code + _ + Public Shared Function GetValue( _ + _ + ByVal Channel As TPCANHandle, _ + _ + ByVal Parameter As TPCANParameter, _ + ByRef NumericBuffer As UInt32, _ + ByVal BufferLength As UInt32) As TPCANStatus + End Function + + ''' + ''' Configures or sets a PCAN Channel value + ''' + ''' 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 handle of a PCAN Channel + ''' The TPCANParameter parameter to set + ''' Buffer with the value to be set + ''' Size in bytes of the buffer + ''' A TPCANStatus error code + _ + Public Shared Function SetValue( _ + _ + ByVal Channel As TPCANHandle, _ + _ + ByVal Parameter As TPCANParameter, _ + ByRef NumericBuffer As UInt32, _ + ByVal BufferLength As UInt32) As TPCANStatus + End Function + + ''' + ''' Configures or sets a PCAN Channel value + ''' + ''' 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 handle of a PCAN Channel + ''' + ''' Buffer with the value to be set + ''' Size in bytes of the buffer + ''' A TPCANStatus error code + _ + Public Shared Function SetValue( _ + _ + ByVal Channel As TPCANHandle, _ + _ + ByVal Parameter As TPCANParameter, _ + _ + ByVal StringBuffer As String, _ + ByVal BufferLength As UInt32) As TPCANStatus + End Function + + ''' + ''' Returns a descriptive text of a given TPCANStatus error + ''' code, in any desired language + ''' + ''' The current languages available for translation are: + ''' Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A), + ''' Italian (0x10) and French (0x0C) + ''' A TPCANStatus error code + ''' Indicates a 'Primary language ID' + ''' Buffer for the text (must be at least 256 in length) + ''' A TPCANStatus error code + _ + Public Shared Function GetErrorText( _ + _ + ByVal anError As TPCANStatus, _ + ByVal Language As UInt16, _ + ByVal StringBuffer As StringBuilder) As TPCANStatus + End Function +#End Region + End Class +#End Region +End Namespace + + diff --git a/PCANBasic/PCANBasicCLR.h b/PCANBasic/PCANBasicCLR.h new file mode 100644 index 0000000..ea6f6e3 --- /dev/null +++ b/PCANBasic/PCANBasicCLR.h @@ -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 + /// + /// Represents a PCAN status/error code + /// + [Flags] + public enum class TPCANStatus : UInt32 + { + /// + /// No error + /// + PCAN_ERROR_OK = 0x00000, + /// + /// Transmit buffer in CAN controller is full + /// + PCAN_ERROR_XMTFULL = 0x00001, + /// + /// CAN controller was read too late + /// + PCAN_ERROR_OVERRUN = 0x00002, + /// + /// Bus error: an error counter reached the 'light' limit + /// + PCAN_ERROR_BUSLIGHT = 0x00004, + /// + /// Bus error: an error counter reached the 'heavy' limit + /// + PCAN_ERROR_BUSHEAVY = 0x00008, + /// + /// Bus error: the CAN controller is in bus-off state + /// + PCAN_ERROR_BUSOFF = 0x00010, + /// + /// Mask for all bus errors + /// + PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF), + /// + /// Receive queue is empty + /// + PCAN_ERROR_QRCVEMPTY = 0x00020, + /// + /// Receive queue was read too late + /// + PCAN_ERROR_QOVERRUN = 0x00040, + /// + /// Transmit queue is full + /// + PCAN_ERROR_QXMTFULL = 0x00080, + /// + /// Test of the CAN controller hardware registers failed (no hardware found) + /// + PCAN_ERROR_REGTEST = 0x00100, + /// + /// Driver not loaded + /// + PCAN_ERROR_NODRIVER = 0x00200, + /// + /// Hardware already in use by a Net + /// + PCAN_ERROR_HWINUSE = 0x00400, + /// + /// A Client is already connected to the Net + /// + PCAN_ERROR_NETINUSE = 0x00800, + /// + /// Hardware handle is invalid + /// + PCAN_ERROR_ILLHW = 0x01400, + /// + /// Net handle is invalid + /// + PCAN_ERROR_ILLNET = 0x01800, + /// + /// Client handle is invalid + /// + PCAN_ERROR_ILLCLIENT = 0x01C00, + /// + /// Mask for all handle errors + /// + PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT), + /// + /// Resource (FIFO, Client, timeout) cannot be created + /// + PCAN_ERROR_RESOURCE = 0x02000, + /// + /// Invalid parameter + /// + PCAN_ERROR_ILLPARAMTYPE = 0x04000, + /// + /// Invalid parameter value + /// + PCAN_ERROR_ILLPARAMVAL = 0x08000, + /// + /// Unknow error + /// + PCAN_ERROR_UNKNOWN = 0x10000, + /// + /// Invalid data, function, or action. + /// + PCAN_ERROR_ILLDATA = 0x20000, + /// + /// Channel is not initialized + /// + PCAN_ERROR_INITIALIZE = 0x40000, + }; + + /// + /// Represents a PCAN device + /// + public enum class TPCANDevice : Byte + { + /// + /// Undefined, unknown or not selected PCAN device value + /// + PCAN_NONE = 0, + /// + /// PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API + /// + PCAN_PEAKCAN = 1, + /// + /// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus + /// + PCAN_ISA = 2, + /// + /// PCAN-Dongle + /// + PCAN_DNG = 3, + /// + /// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express + /// + PCAN_PCI = 4, + /// + /// PCAN-USB and PCAN-USB Pro + /// + PCAN_USB = 5, + /// + /// PCAN-PC Card + /// + PCAN_PCC = 6 + }; + + /// + /// Represents a PCAN parameter to be read or set + /// + public enum class TPCANParameter : Byte + { + /// + /// PCAN-USB device number parameter + /// + PCAN_DEVICE_NUMBER = 1, + /// + /// PCAN-PC Card 5-Volt power parameter + /// + PCAN_5VOLTS_POWER = 2, + /// + /// PCAN receive event handler parameter + /// + PCAN_RECEIVE_EVENT = 3, + /// + /// PCAN message filter parameter + /// + PCAN_MESSAGE_FILTER = 4, + /// + /// PCAN-Basic API version parameter + /// + PCAN_API_VERSION = 5, + /// + /// PCAN device channel version parameter + /// + PCAN_CHANNEL_VERSION = 6, + /// + /// PCAN Reset-On-Busoff parameter + /// + PCAN_BUSOFF_AUTORESET = 7, + /// + /// PCAN Listen-Only parameter + /// + PCAN_LISTEN_ONLY = 8, + /// + /// Directory path for trace files + /// + PCAN_LOG_LOCATION = 9, + /// + /// Debug-Trace activation status + /// + PCAN_LOG_STATUS = 10, + /// + /// Configuration of the debugged information (LOG_FUNCTION_***) + /// + PCAN_LOG_CONFIGURE = 11, + /// + /// Custom insertion of text into the log file + /// + PCAN_LOG_TEXT = 12, + /// + /// Availability status of a PCAN-Channel + /// + PCAN_CHANNEL_CONDITION = 13, + /// + /// PCAN hardware name parameter + /// + PCAN_HARDWARE_NAME = 14, + /// + /// Message reception status of a PCAN-Channel + /// + PCAN_RECEIVE_STATUS = 15, + /// + /// CAN-Controller number of a PCAN-Channel + /// + PCAN_CONTROLLER_NUMBER = 16, + }; + + /// + /// Represents the type of a PCAN message + /// + [Flags] + public enum class TPCANMessageType : Byte + { + /// + /// The PCAN message is a CAN Standard Frame (11-bit identifier) + /// + PCAN_MESSAGE_STANDARD = 0x00, + /// + /// The PCAN message is a CAN Remote-Transfer-Request Frame + /// + PCAN_MESSAGE_RTR = 0x01, + /// + /// The PCAN message is a CAN Extended Frame (29-bit identifier) + /// + PCAN_MESSAGE_EXTENDED = 0x02, + /// + /// The PCAN message represents a PCAN status message + /// + PCAN_MESSAGE_STATUS = 0x80, + }; + + /// + /// Represents a PCAN filter mode + /// + public enum class TPCANMode : Byte + { + /// + /// Mode is Standard (11-bit identifier) + /// + PCAN_MODE_STANDARD = TPCANMessageType::PCAN_MESSAGE_STANDARD, + /// + /// Mode is Extended (29-bit identifier) + /// + PCAN_MODE_EXTENDED = TPCANMessageType::PCAN_MESSAGE_EXTENDED, + }; + + /// + /// Represents a PCAN Baud rate register value + /// + public enum class TPCANBaudrate : UInt16 + { + /// + /// 1 MBit/s + /// + PCAN_BAUD_1M = 0x0014, + /// + /// 800 kBit/s + /// + PCAN_BAUD_800K = 0x0016, + /// + /// 500 kBit/s + /// + PCAN_BAUD_500K = 0x001C, + /// + /// 250 kBit/s + /// + PCAN_BAUD_250K = 0x011C, + /// + /// 125 kBit/s + /// + PCAN_BAUD_125K = 0x031C, + /// + /// 100 kBit/s + /// + PCAN_BAUD_100K = 0x432F, + /// + /// 95,238 kBit/s + /// + PCAN_BAUD_95K = 0xC34E, + /// + /// 83,333 kBit/s + /// + PCAN_BAUD_83K = 0x4B14, + /// + /// 50 kBit/s + /// + PCAN_BAUD_50K = 0x472F, + /// + /// 47,619 kBit/s + /// + PCAN_BAUD_47K = 0x1414, + /// + /// 33,333 kBit/s + /// + PCAN_BAUD_33K = 0x1D14, + /// + /// 20 kBit/s + /// + PCAN_BAUD_20K = 0x532F, + /// + /// 10 kBit/s + /// + PCAN_BAUD_10K = 0x672F, + /// + /// 5 kBit/s + /// + PCAN_BAUD_5K = 0x7F7F, + }; + + /// + /// Represents the type of PCAN (non plug&play) hardware to be initialized + /// + public enum class TPCANType : Byte + { + /// + /// PCAN-ISA 82C200 + /// + PCAN_TYPE_ISA = 0x01, + /// + /// PCAN-ISA SJA1000 + /// + PCAN_TYPE_ISA_SJA = 0x09, + /// + /// PHYTEC ISA + /// + PCAN_TYPE_ISA_PHYTEC = 0x04, + /// + /// PCAN-Dongle 82C200 + /// + PCAN_TYPE_DNG = 0x02, + /// + /// PCAN-Dongle EPP 82C200 + /// + PCAN_TYPE_DNG_EPP = 0x03, + /// + /// PCAN-Dongle SJA1000 + /// + PCAN_TYPE_DNG_SJA = 0x05, + /// + /// PCAN-Dongle EPP SJA1000 + /// + PCAN_TYPE_DNG_SJA_EPP = 0x06, + }; + #pragma endregion + + #pragma region Strutures + /// + /// Represents a PCAN message + /// + public value struct TPCANMsg + { + /// + /// 11/29-bit message identifier + /// + UInt32 ID; + /// + /// Type of the message + /// + [MarshalAs(UnmanagedType::U1)] + TPCANMessageType MSGTYPE; + /// + /// Data Length Code of the message (0..8) + /// + Byte LEN; + /// + /// Data of the message (DATA[0]..DATA[7]) + /// + [MarshalAs(UnmanagedType::ByValArray, SizeConst = 8)] + array^ DATA; + }; + + /// + /// Represents a timestamp of a received PCAN message. + /// Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow + /// + public value struct TPCANTimestamp + { + /// + /// Base-value: milliseconds: 0.. 2^32-1 + /// + UInt32 millis; + /// + /// Roll-arounds of millis + /// + UInt16 millis_overflow; + /// + /// Microseconds: 0..999 + /// + UInt16 micros; + }; + #pragma endregion + + #pragma region PCANBasic class + /// + /// PCAN-Basic API class implementation + /// + public ref class PCANBasic abstract sealed + { + public: + #pragma region PCAN-BUS Handles Definition + /// + /// Undefined/default value for a PCAN bus + /// + static const TPCANHandle PCAN_NONEBUS = 0x00; + + /// + /// PCAN-ISA interface, channel 1 + /// + static const TPCANHandle PCAN_ISABUS1 = 0x21; + /// + /// PCAN-ISA interface, channel 2 + /// + static const TPCANHandle PCAN_ISABUS2 = 0x22; + /// + /// PCAN-ISA interface, channel 3 + /// + static const TPCANHandle PCAN_ISABUS3 = 0x23; + /// + /// PCAN-ISA interface, channel 4 + /// + static const TPCANHandle PCAN_ISABUS4 = 0x24; + /// + /// PCAN-ISA interface, channel 5 + /// + static const TPCANHandle PCAN_ISABUS5 = 0x25; + /// + /// PCAN-ISA interface, channel 6 + /// + static const TPCANHandle PCAN_ISABUS6 = 0x26; + /// + /// PCAN-ISA interface, channel 7 + /// + static const TPCANHandle PCAN_ISABUS7 = 0x27; + /// + /// PCAN-ISA interface, channel 8 + /// + static const TPCANHandle PCAN_ISABUS8 = 0x28; + + /// + /// PPCAN-Dongle/LPT interface, channel 1 + /// + static const TPCANHandle PCAN_DNGBUS1 = 0x31; + + /// + /// PCAN-PCI interface, channel 1 + /// + static const TPCANHandle PCAN_PCIBUS1 = 0x41; + /// + /// PCAN-PCI interface, channel 2 + /// + static const TPCANHandle PCAN_PCIBUS2 = 0x42; + /// + /// PCAN-PCI interface, channel 3 + /// + static const TPCANHandle PCAN_PCIBUS3 = 0x43; + /// + /// PCAN-PCI interface, channel 4 + /// + static const TPCANHandle PCAN_PCIBUS4 = 0x44; + /// + /// PCAN-PCI interface, channel 5 + /// + static const TPCANHandle PCAN_PCIBUS5 = 0x45; + /// + /// PCAN-PCI interface, channel 6 + /// + static const TPCANHandle PCAN_PCIBUS6 = 0x46; + /// + /// PCAN-PCI interface, channel 7 + /// + static const TPCANHandle PCAN_PCIBUS7 = 0x47; + /// + /// PCAN-PCI interface, channel 8 + /// + static const TPCANHandle PCAN_PCIBUS8 = 0x48; + + /// + /// PCAN-USB interface, channel 1 + /// + static const TPCANHandle PCAN_USBBUS1 = 0x51; + /// + /// PCAN-USB interface, channel 2 + /// + static const TPCANHandle PCAN_USBBUS2 = 0x52; + /// + /// PCAN-USB interface, channel 3 + /// + static const TPCANHandle PCAN_USBBUS3 = 0x53; + /// + /// PCAN-USB interface, channel 4 + /// + static const TPCANHandle PCAN_USBBUS4 = 0x54; + /// + /// PCAN-USB interface, channel 5 + /// + static const TPCANHandle PCAN_USBBUS5 = 0x55; + /// + /// PCAN-USB interface, channel 6 + /// + static const TPCANHandle PCAN_USBBUS6 = 0x56; + /// + /// PCAN-USB interface, channel 7 + /// + static const TPCANHandle PCAN_USBBUS7 = 0x57; + /// + /// PCAN-USB interface, channel 8 + /// + static const TPCANHandle PCAN_USBBUS8 = 0x58; + + /// + /// PCAN-PC Card interface, channel 1 + /// + static const TPCANHandle PCAN_PCCBUS1 = 0x61; + /// + /// PCAN-PC Card interface, channel 2 + /// + static const TPCANHandle PCAN_PCCBUS2 = 0x62; + #pragma endregion + + #pragma region Parameter values definition + /// + /// The PCAN parameter is not set (inactive) + /// + static const int PCAN_PARAMETER_OFF = 0; + /// + /// The PCAN parameter is set (active) + /// + static const int PCAN_PARAMETER_ON = 1; + /// + /// The PCAN filter is closed. No messages will be received + /// + static const int PCAN_FILTER_CLOSE = 0; + /// + /// The PCAN filter is fully opened. All messages will be received + /// + static const int PCAN_FILTER_OPEN = 1; + /// + /// The PCAN filter is custom configured. Only registered + /// messages will be received + /// + static const int PCAN_FILTER_CUSTOM = 2; + /// + /// The PCAN-Channel handle is illegal, or its associated hadware is not available + /// + static const int PCAN_CHANNEL_UNAVAILABLE = 0; + /// + /// The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in) + /// + static const int PCAN_CHANNEL_AVAILABLE = 1; + /// + /// The PCAN-Channel handle is valid, and is already being used + /// + static const int PCAN_CHANNEL_OCCUPIED = 2; + + /// + /// Logs system exceptions / errors + /// + static const int LOG_FUNCTION_DEFAULT = 0x00; + /// + /// Logs the entries to the PCAN-Basic API functions + /// + static const int LOG_FUNCTION_ENTRY = 0x01; + /// + /// Logs the parameters passed to the PCAN-Basic API functions + /// + static const int LOG_FUNCTION_PARAMETERS = 0x02; + /// + /// Logs the exits from the PCAN-Basic API functions + /// + static const int LOG_FUNCTION_LEAVE = 0x04; + /// + /// Logs the CAN messages passed to the CAN_Write function + /// + static const int LOG_FUNCTION_WRITE = 0x08; + /// + /// Logs the CAN messages received within the CAN_Read function + /// + static const int LOG_FUNCTION_READ = 0x10; + /// + /// Logs all possible information within the PCAN-Basic API functions + /// + static const int LOG_FUNCTION_ALL = 0xFFFF; + #pragma endregion + + #pragma region PCANBasic API Implementation + /// + /// Initializes a PCAN Channel + /// + /// The handle of a PCAN Channel + /// The speed for the communication (BTR0BTR1 code) + /// NON PLUG&PLAY: The type of hardware and operation mode + /// NON PLUG&PLAY: The I/O address for the parallel port + /// NON PLUG&PLAY: Interrupt number of the parallel por + /// A TPCANStatus error code + [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); + + /// + /// Initializes a PCAN Channel + /// + /// The handle of a PCAN Channel + /// The speed for the communication (BTR0BTR1 code) + /// A TPCANStatus error code + static TPCANStatus Initialize( + TPCANHandle Channel, + TPCANBaudrate Btr0Btr1) + { + return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0); + } + + /// + /// Uninitializes one or all PCAN Channels initialized by CAN_Initialize + /// + /// Giving the TPCANHandle value "PCAN_NONEBUS", + /// uninitialize all initialized channels + /// The handle of a PCAN Channel + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Uninitialize")] + static TPCANStatus Uninitialize( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel); + + /// + /// Resets the receive and transmit queues of the PCAN Channel + /// + /// A reset of the CAN controller is not performed + /// The handle of a PCAN Channel + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Reset")] + static TPCANStatus Reset( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel); + + /// + /// Gets the current status of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetStatus")] + static TPCANStatus GetStatus( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel); + + /// + /// Reads a CAN message from the receive queue of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANMsg structure buffer to store the CAN message + /// A TPCANTimestamp structure buffer to get + /// the reception time of the message + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")] + static TPCANStatus Read( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + TPCANMsg %MessageBuffer, + TPCANTimestamp %TimestampBuffer); + + /// + /// Reads a CAN message from the receive queue of a PCAN Channel + /// + /// The handle of a PCAN Channel + /// A TPCANMsg structure buffer to store the CAN message + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")] + static TPCANStatus Read( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + TPCANMsg %MessageBuffer); + + /// + /// Transmits a CAN message + /// + /// The handle of a PCAN Channel + /// A TPCANMsg buffer with the message to be sent + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")] + static TPCANStatus Write( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + TPCANMsg %MessageBuffer); + + /// + /// Configures the reception filter + /// + /// 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 + /// The handle of a PCAN Channel + /// The lowest CAN ID to be received + /// The highest CAN ID to be received + /// Message type, Standard (11-bit identifier) or + /// Extended (29-bit identifier) + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_FilterMessages")] + static TPCANStatus FilterMessages( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + UInt32 FromID, + UInt32 ToID, + [MarshalAs(UnmanagedType::U1)] + TPCANMode Mode); + + /// + /// Retrieves a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to get + /// Buffer for the parameter value + /// Size in bytes of the buffer + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")] + static TPCANStatus GetValue( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + [MarshalAs(UnmanagedType::U1)] + TPCANParameter Parameter, + StringBuilder^ StringBuffer, + UInt32 BufferLength); + + /// + /// Retrieves a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to get + /// Buffer for the parameter value + /// Size in bytes of the buffer + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")] + static TPCANStatus GetValue( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + [MarshalAs(UnmanagedType::U1)] + TPCANParameter Parameter, + UInt32 %NumericBuffer, + UInt32 BufferLength); + + /// + /// Configures a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// The TPCANParameter parameter to set + /// Buffer with the value to be set + /// Size in bytes of the buffer + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")] + static TPCANStatus SetValue( + [MarshalAs(UnmanagedType::U1)] + TPCANHandle Channel, + [MarshalAs(UnmanagedType::U1)] + TPCANParameter Parameter, + UInt32% NumericBuffer, + UInt32 BufferLength); + + /// + /// Configures a PCAN Channel value + /// + /// 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 handle of a PCAN Channel + /// + /// Buffer with the value to be set + /// Size in bytes of the buffer + /// A TPCANStatus error code + [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); + + /// + /// Returns a descriptive text of a given TPCANStatus error + /// code, in any desired language + /// + /// The current languages available for translation are: + /// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A), + /// Italian (0x10) and French (0x0C) + /// A TPCANStatus error code + /// Indicates a 'Primary language ID' + /// Buffer for the text (must be at least 256 in length) + /// A TPCANStatus error code + [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetErrorText")] + static TPCANStatus GetErrorText( + [MarshalAs(UnmanagedType::U4)] + TPCANStatus Error, + UInt16 Language, + StringBuilder^ StringBuffer); + #pragma endregion + }; + #pragma endregion + } + } +} \ No newline at end of file