// PCANBasicCLR.h // // ~~~~~~~~~~~~ // // PCAN-Basic API // // ~~~~~~~~~~~~ // // ------------------------------------------------------------------ // Author : Keneth Wagner // Last change: 2022-07-06 // // Language: C++/CLR // ------------------------------------------------------------------ // // Copyright (C) 1999-2022 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::UInt16 // Represents a PCAN hardware channel handle #define TPCANBitrateFD System::String^ // Represents a bit rate String (Flexible Data rate) #define TPCANTimestampFD System::UInt64 // Represents a timestamp in microseconds namespace Peak { namespace Can { namespace Basic { #pragma region Other Constants /// /// Maximum length of the name of a device: 32 characters + terminator /// static const int MAX_LENGTH_HARDWARE_NAME = 33; /// /// Maximum length of a version string: 255 characters + terminator /// static const int MAX_LENGTH_VERSION_STRING = 256; #pragma endregion #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: an error counter reached the 'warning' limit /// PCAN_ERROR_BUSWARNING = PCAN_ERROR_BUSHEAVY, /// /// Bus error: the CAN controller is error passive /// PCAN_ERROR_BUSPASSIVE = 0x40000, /// /// Bus error: the CAN controller is in bus-off state /// PCAN_ERROR_BUSOFF = 0x00010, /// /// Mask for all bus errors /// PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSWARNING | PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF | PCAN_ERROR_BUSPASSIVE), /// /// 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, /// /// Unknown error /// PCAN_ERROR_UNKNOWN = 0x10000, /// /// Invalid data, function, or action. /// PCAN_ERROR_ILLDATA = 0x20000, /// /// Driver object state is wrong for the attempted operation /// PCAN_ERROR_ILLMODE = 0x80000, /// /// An operation was successfully carried out, however, irregularities were registered /// PCAN_ERROR_CAUTION = 0x2000000, /// /// Channel is not initialized /// Value was changed from 0x40000 to 0x4000000 /// PCAN_ERROR_INITIALIZE = 0x4000000, /// /// Invalid operation /// Value was changed from 0x80000 to 0x8000000 /// PCAN_ERROR_ILLOPERATION = 0x8000000, }; /// /// Represents a PCAN device /// public enum class TPCANDevice : Byte { /// /// Undefined, unknown or not selected PCAN device value /// PCAN_NONE = 0, /// /// PCAN Non-PnP 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, /// /// PCAN Virtual hardware. NOT USED WITHIN PCAN-Basic API /// PCAN_VIRTUAL = 7, /// /// PCAN Gateway devices /// PCAN_LAN = 8 }; /// /// Represents a PCAN parameter to be read or set /// public enum class TPCANParameter : Byte { /// /// Device identifier parameter /// PCAN_DEVICE_ID = 1, /// /// DEPRECATED. Use PCAN_DEVICE_ID instead /// [Obsolete] PCAN_DEVICE_NUMBER = PCAN_DEVICE_ID, /// /// 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 log files /// PCAN_LOG_LOCATION = 9, /// /// Debug-Log 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, /// /// Directory path for PCAN trace files /// PCAN_TRACE_LOCATION = 17, /// /// CAN tracing activation status /// PCAN_TRACE_STATUS = 18, /// /// Configuration of the maximum file size of a CAN trace /// PCAN_TRACE_SIZE = 19, /// /// Configuration of the trace file storing mode (TRACE_FILE_***) /// PCAN_TRACE_CONFIGURE = 20, /// /// Physical identification of a USB based PCAN-Channel by blinking its associated LED /// PCAN_CHANNEL_IDENTIFYING = 21, /// /// Capabilities of a PCAN device (FEATURE_***) /// PCAN_CHANNEL_FEATURES = 22, /// /// Using of an existing bit rate (PCAN-View connected to a channel) /// PCAN_BITRATE_ADAPTING = 23, /// /// Configured bit rate as Btr0Btr1 value /// PCAN_BITRATE_INFO = 24, /// /// Configured bit rate as TPCANBitrateFD string /// PCAN_BITRATE_INFO_FD = 25, /// /// Configured nominal CAN Bus speed as Bits per seconds /// PCAN_BUSSPEED_NOMINAL = 26, /// /// Configured CAN data speed as Bits per seconds /// PCAN_BUSSPEED_DATA = 27, /// /// Remote address of a LAN channel as string in IPv4 format /// PCAN_IP_ADDRESS = 28, /// /// Status of the Virtual PCAN-Gateway Service /// PCAN_LAN_SERVICE_STATUS = 29, /// /// Status messages reception status within a PCAN-Channel /// PCAN_ALLOW_STATUS_FRAMES = 30, /// /// RTR messages reception status within a PCAN-Channel /// PCAN_ALLOW_RTR_FRAMES = 31, /// /// Error messages reception status within a PCAN-Channel /// PCAN_ALLOW_ERROR_FRAMES = 32, /// /// Delay, in microseconds, between sending frames /// PCAN_INTERFRAME_DELAY = 33, /// /// Filter over code and mask patterns for 11-Bit messages /// PCAN_ACCEPTANCE_FILTER_11BIT = 34, /// /// Filter over code and mask patterns for 29-Bit messages /// PCAN_ACCEPTANCE_FILTER_29BIT = 35, /// /// Output mode of 32 digital I/O pin of a PCAN-USB Chip. 1: Output-Active 0 : Output Inactive /// PCAN_IO_DIGITAL_CONFIGURATION = 36, /// /// Value assigned to a 32 digital I/O pins of a PCAN-USB Chip /// PCAN_IO_DIGITAL_VALUE = 37, /// /// Value assigned to a 32 digital I/O pins of a PCAN-USB Chip - Multiple digital I/O pins to 1 = High /// PCAN_IO_DIGITAL_SET = 38, /// /// Clear multiple digital I/O pins to 0 /// PCAN_IO_DIGITAL_CLEAR = 39, /// /// Get value of a single analog input pin /// PCAN_IO_ANALOG_VALUE = 40, /// /// Get the version of the firmware used by the device associated with a PCAN-Channel /// PCAN_FIRMWARE_VERSION = 41, /// /// Get the amount of PCAN channels attached to a system /// PCAN_ATTACHED_CHANNELS_COUNT = 42, /// /// Get information about PCAN channels attached to a system /// PCAN_ATTACHED_CHANNELS = 43, /// /// Echo messages reception status within a PCAN-Channel /// PCAN_ALLOW_ECHO_FRAMES = 44, /// /// Get the part number associated to a device /// PCAN_DEVICE_PART_NUMBER = 45, }; /// 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 FD frame in terms of CiA Specs /// PCAN_MESSAGE_FD = 0x04, /// /// The PCAN message represents a FD bit rate switch (CAN data at a higher bit rate) /// PCAN_MESSAGE_BRS = 0x08, /// /// The PCAN message represents a FD error state indicator(CAN FD transmitter was error active) /// PCAN_MESSAGE_ESI = 0x10, /// /// The PCAN message represents an echo CAN Frame /// PCAN_MESSAGE_ECHO = 0x20, /// /// The PCAN message represents an error frame /// PCAN_MESSAGE_ERRFRAME = 0x40, /// /// 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 = 0x852B, /// /// 50 kBit/s /// PCAN_BAUD_50K = 0x472F, /// /// 47,619 kBit/s /// PCAN_BAUD_47K = 0x1414, /// /// 33,333 kBit/s /// PCAN_BAUD_33K = 0x8B2F, /// /// 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-PnP) 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 + 0x100000000 * 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; }; /// /// Represents a PCAN message from a FD capable hardware /// public value struct TPCANMsgFD { /// /// 11/29-bit message identifier /// UInt32 ID; /// /// Type of the message /// [MarshalAs(UnmanagedType::U1)] TPCANMessageType MSGTYPE; /// /// Data Length Code of the message (0..15) /// Byte DLC; /// /// Data of the message (DATA[0]..DATA[63]) /// [MarshalAs(UnmanagedType::ByValArray, SizeConst = 64)] array^ DATA; }; /// /// Describes an available PCAN channel /// public value struct TPCANChannelInformation { /// /// PCAN channel handle /// [MarshalAs(UnmanagedType::U2)] TPCANHandle channel_handle; /// /// Kind of PCAN device /// [MarshalAs(UnmanagedType::U1)] TPCANDevice device_type; /// /// CAN-Controller number /// Byte controller_number; /// /// Device capabilities flag (see FEATURE_*) /// UInt32 device_features; /// /// Device name /// [MarshalAs(UnmanagedType::ByValTStr, SizeConst = MAX_LENGTH_HARDWARE_NAME)] String^ device_name; /// /// Device number /// UInt32 device_id; /// /// Availability status of a PCAN-Channel /// UInt32 channel_condition; }; #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-PCI interface, channel 9 /// static const TPCANHandle PCAN_PCIBUS9 = 0x409; /// /// PCAN-PCI interface, channel 10 /// static const TPCANHandle PCAN_PCIBUS10 = 0x40A; /// /// PCAN-PCI interface, channel 11 /// static const TPCANHandle PCAN_PCIBUS11 = 0x40B; /// /// PCAN-PCI interface, channel 12 /// static const TPCANHandle PCAN_PCIBUS12 = 0x40C; /// /// PCAN-PCI interface, channel 13 /// static const TPCANHandle PCAN_PCIBUS13 = 0x40D; /// /// PCAN-PCI interface, channel 14 /// static const TPCANHandle PCAN_PCIBUS14 = 0x40E; /// /// PCAN-PCI interface, channel 15 /// static const TPCANHandle PCAN_PCIBUS15 = 0x40F; /// /// PCAN-PCI interface, channel 16 /// static const TPCANHandle PCAN_PCIBUS16 = 0x410; /// /// 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-USB interface, channel 9 /// static const TPCANHandle PCAN_USBBUS9 = 0x509; /// /// PCAN-USB interface, channel 10 /// static const TPCANHandle PCAN_USBBUS10 = 0x50A; /// /// PCAN-USB interface, channel 11 /// static const TPCANHandle PCAN_USBBUS11 = 0x50B; /// /// PCAN-USB interface, channel 12 /// static const TPCANHandle PCAN_USBBUS12 = 0x50C; /// /// PCAN-USB interface, channel 13 /// static const TPCANHandle PCAN_USBBUS13 = 0x50D; /// /// PCAN-USB interface, channel 14 /// static const TPCANHandle PCAN_USBBUS14 = 0x50E; /// /// PCAN-USB interface, channel 15 /// static const TPCANHandle PCAN_USBBUS15 = 0x50F; /// /// PCAN-USB interface, channel 16 /// static const TPCANHandle PCAN_USBBUS16 = 0x510; /// /// PCAN-PC Card interface, channel 1 /// static const TPCANHandle PCAN_PCCBUS1 = 0x61; /// /// PCAN-PC Card interface, channel 2 /// static const TPCANHandle PCAN_PCCBUS2 = 0x62; /// /// PCAN-LAN interface, channel 1 /// static const TPCANHandle PCAN_LANBUS1 = 0x801; /// /// PCAN-LAN interface, channel 2 /// static const TPCANHandle PCAN_LANBUS2 = 0x802; /// /// PCAN-LAN interface, channel 3 /// static const TPCANHandle PCAN_LANBUS3 = 0x803; /// /// PCAN-LAN interface, channel 4 /// static const TPCANHandle PCAN_LANBUS4 = 0x804; /// /// PCAN-LAN interface, channel 5 /// static const TPCANHandle PCAN_LANBUS5 = 0x805; /// /// PCAN-LAN interface, channel 6 /// static const TPCANHandle PCAN_LANBUS6 = 0x806; /// /// PCAN-LAN interface, channel 7 /// static const TPCANHandle PCAN_LANBUS7 = 0x807; /// /// PCAN-LAN interface, channel 8 /// static const TPCANHandle PCAN_LANBUS8 = 0x808; /// /// PCAN-LAN interface, channel 9 /// static const TPCANHandle PCAN_LANBUS9 = 0x809; /// /// PCAN-LAN interface, channel 10 /// static const TPCANHandle PCAN_LANBUS10 = 0x80A; /// /// PCAN-LAN interface, channel 11 /// static const TPCANHandle PCAN_LANBUS11 = 0x80B; /// /// PCAN-LAN interface, channel 12 /// static const TPCANHandle PCAN_LANBUS12 = 0x80C; /// /// PCAN-LAN interface, channel 13 /// static const TPCANHandle PCAN_LANBUS13 = 0x80D; /// /// PCAN-LAN interface, channel 14 /// static const TPCANHandle PCAN_LANBUS14 = 0x80E; /// /// PCAN-LAN interface, channel 15 /// static const TPCANHandle PCAN_LANBUS15 = 0x80F; /// /// PCAN-LAN interface, channel 16 /// static const TPCANHandle PCAN_LANBUS16 = 0x810; #pragma endregion #pragma region FD Bit rate definition /// /// Clock frequency in Herz (80000000, 60000000, 40000000, 30000000, 24000000, 20000000) /// static const String^ PCAN_BR_CLOCK = "f_clock"; /// /// Clock frequency in Megaherz (80, 60, 40, 30, 24, 20) /// static const String^ PCAN_BR_CLOCK_MHZ = "f_clock_mhz"; /// /// Clock prescaler for nominal time quantum /// static const String^ PCAN_BR_NOM_BRP = "nom_brp"; /// /// TSEG1 segment for nominal bit rate in time quanta /// static const String^ PCAN_BR_NOM_TSEG1 = "nom_tseg1"; /// /// TSEG2 segment for nominal bit rate in time quanta /// static const String^ PCAN_BR_NOM_TSEG2 = "nom_tseg2"; /// /// Synchronization Jump Width for nominal bit rate in time quanta /// static const String^ PCAN_BR_NOM_SJW = "nom_sjw"; /// /// Sample point for nominal bit rate /// static const String^ PCAN_BR_NOM_SAMPLE = "nom_sam"; /// /// Clock prescaler for highspeed data time quantum /// static const String^ PCAN_BR_DATA_BRP = "data_brp"; /// /// TSEG1 segment for fast data bit rate in time quanta /// static const String^ PCAN_BR_DATA_TSEG1 = "data_tseg1"; /// /// TSEG2 segment for fast data bit rate in time quanta /// static const String^ PCAN_BR_DATA_TSEG2 = "data_tseg2"; /// /// Synchronization Jump Width for highspeed data bit rate in time quanta /// static const String^ PCAN_BR_DATA_SJW = "data_sjw"; /// /// Secondary sample point delay for highspeed data bit rate in cyles /// static const String^ PCAN_BR_DATA_SAMPLE = "data_ssp_offset"; #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 hardware is not available /// static const int PCAN_CHANNEL_UNAVAILABLE = 0; /// /// The PCAN-Channel handle is available to be connected (PnP Hardware: it means furthermore 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; /// /// The PCAN-Channel handle is already being used by a PCAN-View application, but is available to connect /// static const int PCAN_CHANNEL_PCANVIEW = PCAN_CHANNEL_AVAILABLE | PCAN_CHANNEL_OCCUPIED; /// /// 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; /// /// A single file is written until it size reaches PAN_TRACE_SIZE /// static const int TRACE_FILE_SINGLE = 0x00; /// /// Traced data is distributed in several files with size PAN_TRACE_SIZE /// static const int TRACE_FILE_SEGMENTED = 0x01; /// /// Includes the date into the name of the trace file /// static const int TRACE_FILE_DATE = 0x02; /// /// Includes the start time into the name of the trace file /// static const int TRACE_FILE_TIME = 0x04; /// /// Causes the overwriting of available traces (same name) /// static const int TRACE_FILE_OVERWRITE = 0x80; /// /// Device supports flexible data-rate (CAN-FD) /// static const int FEATURE_FD_CAPABLE = 0x01; /// /// Device supports a delay between sending frames (FPGA based USB devices) /// static const int FEATURE_DELAY_CAPABLE = 0x02; /// /// Device supports I/O functionality for electronic circuits (USB-Chip devices) /// static const int FEATURE_IO_CAPABLE = 0x04; /// The service is not running /// /// static const int SERVICE_STATUS_STOPPED = 0x01; /// /// The service is running /// static const int SERVICE_STATUS_RUNNING = 0x04; #pragma endregion #pragma region Lookup Parameters /// /// Lookup channel by Device type (see PCAN devices e.g. PCAN_USB) /// static const String^ LOOKUP_DEVICE_TYPE = "devicetype"; /// /// Lookup channel by device id /// static const String^ LOOKUP_DEVICE_ID = "deviceid"; /// /// Lookup channel by CAN controller 0-based index /// static const String^ LOOKUP_CONTROLLER_NUMBER = "controllernumber"; /// /// Lookup channel by IP address (LAN channels only) /// static const String^ LOOKUP_IP_ADDRESS = "ipaddress"; #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-PnP: The type of hardware and operation mode /// Non-PnP: The I/O address for the parallel port /// Non-PnP: Interrupt number of the parallel por /// A TPCANStatus error code [DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")] static TPCANStatus Initialize( [MarshalAs(UnmanagedType::U2)] 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); } /// /// Initializes a FD capable PCAN Channel /// /// The handle of a FD capable PCAN Channel /// The speed for the communication (FD bit rate string) /// See PCAN_BR_* values /// Bit rate string must follow the following construction rules: /// * parameter and values must be separated by '=' /// * Couples of Parameter/value must be separated by ',' /// * Following Parameter must be filled out: f_clock, data_brp, data_sjw, data_tseg1, data_tseg2, /// nom_brp, nom_sjw, nom_tseg1, nom_tseg2. /// * Following Parameters are optional (not used yet): data_ssp_offset, nom_sam /// f_clock=80000000,nom_brp=10,nom_tseg1=5,nom_tseg2=2,nom_sjw=1,data_brp=4,data_tseg1=7,data_tseg2=2,data_sjw=1 /// A TPCANStatus error code [DllImport("PCANBasic.dll", EntryPoint = "CAN_InitializeFD")] static TPCANStatus InitializeFD( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, TPCANBitrateFD BitrateFD); /// /// 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::U2)] 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::U2)] 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::U2)] 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::U2)] TPCANHandle Channel, TPCANMsg %MessageBuffer, TPCANTimestamp %TimestampBuffer); private: [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")] static TPCANStatus Read( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, TPCANMsg %MessageBuffer, IntPtr bufferPointer); public: /// /// 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 static TPCANStatus Read( TPCANHandle Channel, TPCANMsg %MessageBuffer) { return Read(Channel, MessageBuffer, IntPtr::Zero); } /// /// Reads a CAN message from the receive queue of a FD capable PCAN Channel /// /// The handle of a FD capable PCAN Channel /// A TPCANMsgFD structure buffer to store the CAN message /// A TPCANTimestampFD buffer to get the /// reception time of the message /// A TPCANStatus error code [DllImport("PCANBasic.dll", EntryPoint = "CAN_ReadFD")] static TPCANStatus ReadFD( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, TPCANMsgFD %MessageBuffer, TPCANTimestampFD %TimestampBuffer); private: [DllImport("PCANBasic.dll", EntryPoint = "CAN_ReadFD")] static TPCANStatus ReadFD( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, TPCANMsgFD %MessageBuffer, IntPtr bufferPointer); public: /// /// Reads a CAN message from the receive queue of a FD capable PCAN Channel /// /// The handle of a FD capable PCAN Channel /// A TPCANMsgFD structure buffer to store the CAN message /// A TPCANTimestampFD buffer to get the /// reception time of the message /// A TPCANStatus error code static TPCANStatus ReadFD( TPCANHandle Channel, TPCANMsgFD %MessageBuffer) { return ReadFD(Channel, 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")] static TPCANStatus Write( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, TPCANMsg %MessageBuffer); /// /// Transmits a CAN message over a FD capable PCAN Channel /// /// The handle of a FD capable PCAN Channel /// A TPCANMsgFD buffer with the message to be sent /// A TPCANStatus error code [DllImport("PCANBasic.dll", EntryPoint = "CAN_WriteFD")] static TPCANStatus WriteFD( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, TPCANMsgFD %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::U2)] 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::U2)] 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::U2)] TPCANHandle Channel, [MarshalAs(UnmanagedType::U1)] TPCANParameter Parameter, UInt32 %NumericBuffer, 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::U2)] TPCANHandle Channel, [MarshalAs(UnmanagedType::U1)] TPCANParameter Parameter, UInt64 %NumericBuffer, UInt32 BufferLength); private: [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")] static TPCANStatus GetValue( [MarshalAs(UnmanagedType::U2)] TPCANHandle Channel, [MarshalAs(UnmanagedType::U1)] TPCANParameter Parameter, [MarshalAs(UnmanagedType::LPArray, SizeParamIndex=3)] [In, Out]array ^ChannelBuffer, UInt32 BufferLength); public: /// /// 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 /// A TPCANStatus error code static TPCANStatus GetValue( TPCANHandle Channel, TPCANParameter Parameter, array ^ChannelsBuffer) { if(ChannelsBuffer == nullptr) return TPCANStatus::PCAN_ERROR_ILLPARAMVAL; return GetValue(Channel, Parameter, ChannelsBuffer, ChannelsBuffer->Length * Marshal::SizeOf(TPCANChannelInformation::typeid)); } /// /// 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::U2)] 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::U2)] TPCANHandle Channel, [MarshalAs(UnmanagedType::U1)] TPCANParameter Parameter, UInt64% 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::U2)] 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); /// /// Finds a PCAN-Basic channel that matches with the given parameters /// /// A comma separated string contained pairs of /// parameter-name/value to be matched within a PCAN-Basic channel /// Buffer for returning the PCAN-Basic channel, /// when found /// A TPCANStatus error code [DllImport("PCANBasic.dll", EntryPoint = "CAN_LookUpChannel")] static TPCANStatus LookUpChannel( String^ Parameters, TPCANHandle% FoundChannel); #pragma endregion }; #pragma endregion } } }