' 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