// PCAN-ISO-TP_2004.cs
//
// ~~~~~~~~~~~~
//
// PCAN-ISO-TP API
//
// ~~~~~~~~~~~~
//
// ------------------------------------------------------------------
// Author : Fabrice Vergnaud
// Last changed by: $Author: Fabrice $
// Last changed date: $Date: 2020-02-10 16:57:41 +0100 (Mon, 10 Feb 2020) $
//
// Language: C#
// ------------------------------------------------------------------
//
// Copyright (C) 2015 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.IsoTp
{
////////////////////////////////////////////////////////////
// Inclusion of other needed stuff
////////////////////////////////////////////////////////////
using TPCANTPHandle = System.UInt16;
using TPCANTPBitrateFD = System.String;
#region Enumerations
///
/// Represents a PCAN Baud rate register value
///
public enum TPCANTPBaudrate : ushort
{
///
/// 1 MBit/s
///
PCANTP_BAUD_1M = 0x0014,
///
/// 800 kBit/s
///
PCANTP_BAUD_800K = 0x0016,
///
/// 500 kBit/s
///
PCANTP_BAUD_500K = 0x001C,
///
/// 250 kBit/s
///
PCANTP_BAUD_250K = 0x011C,
///
/// 125 kBit/s
///
PCANTP_BAUD_125K = 0x031C,
///
/// 100 kBit/s
///
PCANTP_BAUD_100K = 0x432F,
///
/// 95,238 kBit/s
///
PCANTP_BAUD_95K = 0xC34E,
///
/// 83,333 kBit/s
///
PCANTP_BAUD_83K = 0x852B,
///
/// 50 kBit/s
///
PCANTP_BAUD_50K = 0x472F,
///
/// 47,619 kBit/s
///
PCANTP_BAUD_47K = 0x1414,
///
/// 33,333 kBit/s
///
PCANTP_BAUD_33K = 0x8B2F,
///
/// 20 kBit/s
///
PCANTP_BAUD_20K = 0x532F,
///
/// 10 kBit/s
///
PCANTP_BAUD_10K = 0x672F,
///
/// 5 kBit/s
///
PCANTP_BAUD_5K = 0x7F7F,
}
///
/// Represents the different Not Plug-And-Play PCAN Hardware types
///
public enum TPCANTPHWType : byte
{
///
/// PCAN-ISA 82C200
///
PCANTP_TYPE_ISA = 0x01,
///
/// PCAN-ISA SJA1000
///
PCANTP_TYPE_ISA_SJA = 0x09,
///
/// PHYTEC ISA
///
PCANTP_TYPE_ISA_PHYTEC = 0x04,
///
/// PCAN-Dongle 82C200
///
PCANTP_TYPE_DNG = 0x02,
///
/// PCAN-Dongle EPP 82C200
///
PCANTP_TYPE_DNG_EPP = 0x03,
///
/// PCAN-Dongle SJA1000
///
PCANTP_TYPE_DNG_SJA = 0x05,
///
/// PCAN-Dongle EPP SJA1000
///
PCANTP_TYPE_DNG_SJA_EPP = 0x06,
}
///
/// Represent the PCANTP error and status codes
///
public enum TPCANTPStatus : uint
{
///
/// No error
///
PCANTP_ERROR_OK = 0x00000,
///
/// Not Initialized
///
PCANTP_ERROR_NOT_INITIALIZED = 0x00001,
///
/// Already Initialized
///
PCANTP_ERROR_ALREADY_INITIALIZED = 0x00002,
///
/// Could not obtain memory
///
PCANTP_ERROR_NO_MEMORY = 0x00003,
///
/// Input buffer overflow
///
PCANTP_ERROR_OVERFLOW = 0x00004,
///
/// No Message available
///
PCANTP_ERROR_NO_MESSAGE = 0x00007,
///
/// Wrong message parameters
///
PCANTP_ERROR_WRONG_PARAM = 0x00008,
///
/// PCANTP Channel is in BUS-LIGHT error state
///
PCANTP_ERROR_BUSLIGHT = 0x00009,
///
/// PCANTP Channel is in BUS-HEAVY error state
///
PCANTP_ERROR_BUSHEAVY = 0x0000A,
///
/// PCANTP Channel is in BUS-OFF error state
///
PCANTP_ERROR_BUSOFF = 0x0000B,
///
/// Global CAN error, status code for composition of PCANBasic Errors.
/// Remove this value to get a PCAN-Basic TPCANStatus error code.
///
PCANTP_ERROR_CAN_ERROR = 0x80000000,
}
///
/// Represent message request confirmation values defined in ISO 15765-2
///
public enum TPCANTPConfirmation : byte
{
///
/// No network error
///
PCANTP_N_OK = 0x00,
///
/// timeout occured between 2 frames transmission (sender and receiver side)
///
PCANTP_N_TIMEOUT_A = 0x01,
///
/// sender side timeout while waiting for flow control frame
///
PCANTP_N_TIMEOUT_BS = 0x02,
///
/// receiver side timeout while waiting for consecutive frame
///
PCANTP_N_TIMEOUT_CR = 0x03,
///
/// unexpected sequence number
///
PCANTP_N_WRONG_SN = 0x04,
///
/// invalid or unknown FlowStatus
///
PCANTP_N_INVALID_FS = 0x05,
///
/// unexpected protocol data unit
///
PCANTP_N_UNEXP_PDU = 0x06,
///
/// reception of flow control WAIT frame that exceeds the maximum counter defined by PCANTP_PARAM_WFT_MAX
///
PCANTP_N_WFT_OVRN = 0x07,
///
/// buffer on the receiver side cannot store the data length (server side only)
///
PCANTP_N_BUFFER_OVFLW = 0x08,
///
/// general error
///
PCANTP_N_ERROR = 0x09,
///
/// message was invalid and ignored
///
PCANTP_N_IGNORED = 0x0A,
}
///
/// PCANTP parameters
///
public enum TPCANTPParameter : byte
{
///
/// 1 BYTE data describing the block size parameter (BS)
///
PCANTP_PARAM_BLOCK_SIZE = 0xE1,
///
/// 1 BYTE data describing the separation time parameter (STmin)
///
PCANTP_PARAM_SEPARATION_TIME = 0xE2,
///
/// 1 BYTE data describing the debug mode
///
PCANTP_PARAM_DEBUG = 0xE3,
///
/// 1 Byte data describing the condition of a channel
///
PCANTP_PARAM_CHANNEL_CONDITION = 0xE4,
///
/// Integer data describing the Wait Frame Transmissions parameter.
///
PCANTP_PARAM_WFT_MAX = 0xE5,
///
/// 1 BYTE data stating if pending messages are displayed/hidden
///
PCANTP_PARAM_MSG_PENDING = 0xE6,
///
/// PCAN-ISO-TP API version parameter
///
PCANTP_PARAM_API_VERSION = 0xE7,
///
/// 1 BYTE data stating if CAN frame DLC uses padding or not
///
PCANTP_PARAM_CAN_DATA_PADDING = 0xE8,
///
/// 1 BYTE data stating if unsegmented (NON-ISO-TP) CAN frames can be received
///
PCANTP_PARAM_CAN_UNSEGMENTED = 0xE9,
///
/// PCAN-ISO-TP receive event handler parameter
///
PCANTP_PARAM_RECEIVE_EVENT = 0xEA,
///
/// 1 BYTE data stating the value used for CAN data padding
///
PCANTP_PARAM_PADDING_VALUE = 0xED,
///
/// 1 BYTE data stating the default priority value for normal fixed, mixed and enhanced addressing (default=6)
///
PCANTP_PARAM_J1939_PRIORITY = 0xEE,
///
/// 1 BYTE data stating the default DLC to use when transmitting messages with CAN FD
///
PCANTP_PARAM_CAN_TX_DL = 0xEF,
///
/// 1 BYTE data stating how to optimize Minimum SeparationTime latency (enabled=1 by default).
/// Set value to 0 to ensure STMin is always strictly respected (but consequently lose some speed in communication process).
///
PCANTP_PARAM_SEPARATION_TIME_OPTIMIZATION = 0xF0
}
///
/// PCANTP message types
///
public enum TPCANTPMessageType : byte
{
///
/// Unknown (non-ISO-TP) message
///
PCANTP_MESSAGE_UNKNOWN = 0x00,
///
/// Diagnostic Request/Confirmation
///
PCANTP_MESSAGE_DIAGNOSTIC = 0x01,
///
/// Remote Dignostic Request/Confirmation (uses RA address)
///
PCANTP_MESSAGE_REMOTE_DIAGNOSTIC = 0x02,
///
/// Confirms that a message has been sent successfully/ not successfully
///
PCANTP_MESSAGE_REQUEST_CONFIRMATION = 0x03,
///
/// Multi-Frame Message is being received
///
PCANTP_MESSAGE_INDICATION = 0x04,
///
/// Multi-Frame Message is being transmitted
///
PCANTP_MESSAGE_INDICATION_TX = 0x05,
}
///
/// PCANTP message types
///
[Flags]
public enum TPCANTPIdType : byte
{
///
/// 11 bits CAN ID (CAN Standard Frame)
///
PCANTP_ID_CAN_11BIT = 0x01,
///
/// 29 bits CAN ID (CAN Extended Frame)
///
PCANTP_ID_CAN_29BIT = 0x02,
///
/// CAN FD flag
///
PCANTP_ID_CAN_FD = 0x04,
///
/// Bitrate Switch flag (only if CAN FD)
///
PCANTP_ID_CAN_BRS = 0x08,
}
///
/// PCANTP message types utility function to handle priority J1939 compatible ISO-TP messages
///
public static class TPCANTPIdTypePriority
{
///
/// Get the 29bits CAN ID type with a specific J1939 priority
///
public static TPCANTPIdType PCANTP_ID_CAN_GET_29B(byte j1939_priority)
{
return (TPCANTPIdType)((byte)((j1939_priority << 5) | CanTpApi.PCANTP_ID_CAN_IS_PRIORITY_MASK | ((byte)TPCANTPIdType.PCANTP_ID_CAN_29BIT & CanTpApi.PCANTP_ID_CAN_MASK)));
}
///
/// Retrieves the priority field from a CAN ID type
///
public static byte PCANTP_ID_CAN_GET_PRIORIY(byte id_type)
{
return (byte)(id_type >> 5);
}
///
/// States if the CAN ID Type is 29bits
///
public static bool PCANTP_ID_CAN_IS_EXTENDED(byte id_type)
{
return ((id_type & (byte)TPCANTPIdType.PCANTP_ID_CAN_29BIT) == (int)TPCANTPIdType.PCANTP_ID_CAN_29BIT);
}
///
/// States if the id_type contains a J1939 priority field
///
public static bool PCANTP_ID_CAN_HAS_PRIORITY(byte id_type)
{
return (PCANTP_ID_CAN_IS_EXTENDED(id_type) && ((id_type & CanTpApi.PCANTP_ID_CAN_IS_PRIORITY_MASK) == CanTpApi.PCANTP_ID_CAN_IS_PRIORITY_MASK));
}
}
///
/// PCANTP addressing type
///
public enum TPCANTPFormatType : byte
{
///
/// unknown adressing format
///
PCANTP_FORMAT_UNKNOWN = 0xFF,
///
/// unsegmented CAN frame
///
PCANTP_FORMAT_NONE = 0x00,
///
/// normal adressing format from ISO 15765-2
///
PCANTP_FORMAT_NORMAL = 0x01,
///
/// fixed normal adressing format from ISO 15765-2
///
PCANTP_FORMAT_FIXED_NORMAL = 0x02,
///
/// extended adressing format from ISO 15765-2
///
PCANTP_FORMAT_EXTENDED = 0x03,
///
/// mixed adressing format from ISO 15765-2
///
PCANTP_FORMAT_MIXED = 0x04,
///
/// enhanced adressing format from ISO 15765-3
///
PCANTP_FORMAT_ENHANCED = 0x05,
}
///
/// PCANTP addressing type
///
public enum TPCANTPAddressingType : byte
{
///
/// Unknown (non-ISO-TP) message
///
PCANTP_ADDRESSING_UNKNOWN = 0x00,
///
/// Physical addressing
///
PCANTP_ADDRESSING_PHYSICAL = 0x01,
///
/// Functional addressing
///
PCANTP_ADDRESSING_FUNCTIONAL = 0x02,
}
#endregion
#region Structures
///
/// A CAN-TP Message
///
[StructLayout(LayoutKind.Sequential)]
public struct TPCANTPMsg
{
///
/// Represents the origin of this message (address from
/// where this message was or will be sent)
///
public byte SA;
///
/// Represents the destination of this message (address to
/// where this message was or will be sent)
///
public byte TA;
///
/// Represents the kind of addressing being used for communication
///
[MarshalAs(UnmanagedType.U1)]
public TPCANTPAddressingType TA_TYPE;
///
/// Represents the destination of this message in a remote network
///
public byte RA;
///
/// Identifies the kind of CAN Identifier
///
[MarshalAs(UnmanagedType.U1)]
public TPCANTPIdType IDTYPE;
///
/// Identifies the kind of data represented by this message
///
[MarshalAs(UnmanagedType.U1)]
public TPCANTPMessageType MSGTYPE;
///
/// Represents the kind of format represented by this message
///
[MarshalAs(UnmanagedType.U1)]
public TPCANTPFormatType FORMAT;
///
/// Represents the buffer containing the data of this message
///
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4095)]
public byte[] DATA;
///
/// Indicates the length of the useful data within the DATA buffer
///
public ushort LEN;
///
/// Represent message request confirmation
///
[MarshalAs(UnmanagedType.U1)]
public TPCANTPConfirmation RESULT;
}
///
/// Represents a timestamp of a received PCAN message.
/// Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
///
public struct TPCANTPTimestamp
{
///
/// 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 PCANTP Api
/* Summary
The PCAN-ISO-TP API class
*/
public static partial class CanTpApi
{
#region PCAN-BUS Handles Definition
///
/// Undefined/default value for a PCAN bus
///
public const TPCANTPHandle PCANTP_NONEBUS = 0x00;
///
/// PCAN-ISA interface, channel 1
///
public const TPCANTPHandle PCANTP_ISABUS1 = 0x21;
///
/// PCAN-ISA interface, channel 2
///
public const TPCANTPHandle PCANTP_ISABUS2 = 0x22;
///
/// PCAN-ISA interface, channel 3
///
public const TPCANTPHandle PCANTP_ISABUS3 = 0x23;
///
/// PCAN-ISA interface, channel 4
///
public const TPCANTPHandle PCANTP_ISABUS4 = 0x24;
///
/// PCAN-ISA interface, channel 5
///
public const TPCANTPHandle PCANTP_ISABUS5 = 0x25;
///
/// PCAN-ISA interface, channel 6
///
public const TPCANTPHandle PCANTP_ISABUS6 = 0x26;
///
/// PCAN-ISA interface, channel 7
///
public const TPCANTPHandle PCANTP_ISABUS7 = 0x27;
///
/// PCAN-ISA interface, channel 8
///
public const TPCANTPHandle PCANTP_ISABUS8 = 0x28;
///
/// PPCAN-Dongle/LPT interface, channel 1
///
public const TPCANTPHandle PCANTP_DNGBUS1 = 0x31;
///
/// PCAN-PCI interface, channel 1
///
public const TPCANTPHandle PCANTP_PCIBUS1 = 0x41;
///
/// PCAN-PCI interface, channel 2
///
public const TPCANTPHandle PCANTP_PCIBUS2 = 0x42;
///
/// PCAN-PCI interface, channel 3
///
public const TPCANTPHandle PCANTP_PCIBUS3 = 0x43;
///
/// PCAN-PCI interface, channel 4
///
public const TPCANTPHandle PCANTP_PCIBUS4 = 0x44;
///
/// PCAN-PCI interface, channel 5
///
public const TPCANTPHandle PCANTP_PCIBUS5 = 0x45;
///
/// PCAN-PCI interface, channel 6
///
public const TPCANTPHandle PCANTP_PCIBUS6 = 0x46;
///
/// PCAN-PCI interface, channel 7
///
public const TPCANTPHandle PCANTP_PCIBUS7 = 0x47;
///
/// PCAN-PCI interface, channel 8
///
public const TPCANTPHandle PCANTP_PCIBUS8 = 0x48;
///
/// PCAN-PCI interface, channel 9
///
public const TPCANTPHandle PCANTP_PCIBUS9 = 0x409;
///
/// PCAN-PCI interface, channel 10
///
public const TPCANTPHandle PCANTP_PCIBUS10 = 0x40A;
///
/// PCAN-PCI interface, channel 11
///
public const TPCANTPHandle PCANTP_PCIBUS11 = 0x40B;
///
/// PCAN-PCI interface, channel 12
///
public const TPCANTPHandle PCANTP_PCIBUS12 = 0x40C;
///
/// PCAN-PCI interface, channel 13
///
public const TPCANTPHandle PCANTP_PCIBUS13 = 0x40D;
///
/// PCAN-PCI interface, channel 14
///
public const TPCANTPHandle PCANTP_PCIBUS14 = 0x40E;
///
/// PCAN-PCI interface, channel 15
///
public const TPCANTPHandle PCANTP_PCIBUS15 = 0x40F;
///
/// PCAN-PCI interface, channel 16
///
public const TPCANTPHandle PCANTP_PCIBUS16 = 0x410;
///
/// PCAN-USB interface, channel 1
///
public const TPCANTPHandle PCANTP_USBBUS1 = 0x51;
///
/// PCAN-USB interface, channel 2
///
public const TPCANTPHandle PCANTP_USBBUS2 = 0x52;
///
/// PCAN-USB interface, channel 3
///
public const TPCANTPHandle PCANTP_USBBUS3 = 0x53;
///
/// PCAN-USB interface, channel 4
///
public const TPCANTPHandle PCANTP_USBBUS4 = 0x54;
///
/// PCAN-USB interface, channel 5
///
public const TPCANTPHandle PCANTP_USBBUS5 = 0x55;
///
/// PCAN-USB interface, channel 6
///
public const TPCANTPHandle PCANTP_USBBUS6 = 0x56;
///
/// PCAN-USB interface, channel 7
///
public const TPCANTPHandle PCANTP_USBBUS7 = 0x57;
///
/// PCAN-USB interface, channel 8
///
public const TPCANTPHandle PCANTP_USBBUS8 = 0x58;
///
/// PCAN-USB interface, channel 9
///
public const TPCANTPHandle PCANTP_USBBUS9 = 0x509;
///
/// PCAN-USB interface, channel 10
///
public const TPCANTPHandle PCANTP_USBBUS10 = 0x50A;
///
/// PCAN-USB interface, channel 11
///
public const TPCANTPHandle PCANTP_USBBUS11 = 0x50B;
///
/// PCAN-USB interface, channel 12
///
public const TPCANTPHandle PCANTP_USBBUS12 = 0x50C;
///
/// PCAN-USB interface, channel 13
///
public const TPCANTPHandle PCANTP_USBBUS13 = 0x50D;
///
/// PCAN-USB interface, channel 14
///
public const TPCANTPHandle PCANTP_USBBUS14 = 0x50E;
///
/// PCAN-USB interface, channel 15
///
public const TPCANTPHandle PCANTP_USBBUS15 = 0x50F;
///
/// PCAN-USB interface, channel 16
///
public const TPCANTPHandle PCANTP_USBBUS16 = 0x510;
///
/// PCAN-PC Card interface, channel 1
///
public const TPCANTPHandle PCANTP_PCCBUS1 = 0x61;
///
/// PCAN-PC Card interface, channel 2
///
public const TPCANTPHandle PCANTP_PCCBUS2 = 0x62;
///
/// PCAN-LAN interface, channel 1
///
public const TPCANTPHandle PCANTP_LANBUS1 = 0x801;
///
/// PCAN-LAN interface, channel 2
///
public const TPCANTPHandle PCANTP_LANBUS2 = 0x802;
///
/// PCAN-LAN interface, channel 3
///
public const TPCANTPHandle PCANTP_LANBUS3 = 0x803;
///
/// PCAN-LAN interface, channel 4
///
public const TPCANTPHandle PCANTP_LANBUS4 = 0x804;
///
/// PCAN-LAN interface, channel 5
///
public const TPCANTPHandle PCANTP_LANBUS5 = 0x805;
///
/// PCAN-LAN interface, channel 6
///
public const TPCANTPHandle PCANTP_LANBUS6 = 0x806;
///
/// PCAN-LAN interface, channel 7
///
public const TPCANTPHandle PCANTP_LANBUS7 = 0x807;
///
/// PCAN-LAN interface, channel 8
///
public const TPCANTPHandle PCANTP_LANBUS8 = 0x808;
///
/// PCAN-LAN interface, channel 9
///
public const TPCANTPHandle PCANTP_LANBUS9 = 0x809;
///
/// PCAN-LAN interface, channel 10
///
public const TPCANTPHandle PCANTP_LANBUS10 = 0x80A;
///
/// PCAN-LAN interface, channel 11
///
public const TPCANTPHandle PCANTP_LANBUS11 = 0x80B;
///
/// PCAN-LAN interface, channel 12
///
public const TPCANTPHandle PCANTP_LANBUS12 = 0x80C;
///
/// PCAN-LAN interface, channel 13
///
public const TPCANTPHandle PCANTP_LANBUS13 = 0x80D;
///
/// PCAN-LAN interface, channel 14
///
public const TPCANTPHandle PCANTP_LANBUS14 = 0x80E;
///
/// PCAN-LAN interface, channel 15
///
public const TPCANTPHandle PCANTP_LANBUS15 = 0x80F;
///
/// PCAN-LAN interface, channel 16
///
public const TPCANTPHandle PCANTP_LANBUS16 = 0x810;
#endregion
#region FD Bit rate parameters
///
/// Clock frequency in Herz (80000000, 60000000, 40000000, 30000000, 24000000, 20000000)
///
public const string PCANTP_BR_CLOCK = "f_clock";
///
/// Clock frequency in Megaherz (80, 60, 40, 30, 24, 20)
///
public const string PCANTP_BR_CLOCK_MHZ = "f_clock_mhz";
///
/// Clock prescaler for nominal time quantum
///
public const string PCANTP_BR_NOM_BRP = "nom_brp";
///
/// TSEG1 segment for nominal bit rate in time quanta
///
public const string PCANTP_BR_NOM_TSEG1 = "nom_tseg1";
///
/// TSEG2 segment for nominal bit rate in time quanta
///
public const string PCANTP_BR_NOM_TSEG2 = "nom_tseg2";
///
/// Synchronization Jump Width for nominal bit rate in time quanta
///
public const string PCANTP_BR_NOM_SJW = "nom_sjw";
///
/// Sample point for nominal bit rate
///
public const string PCANTP_BR_NOM_SAMPLE = "nom_sam";
///
/// Clock prescaler for highspeed data time quantum
///
public const string PCANTP_BR_DATA_BRP = "data_brp";
///
/// TSEG1 segment for fast data bit rate in time quanta
///
public const string PCANTP_BR_DATA_TSEG1 = "data_tseg1";
///
/// TSEG2 segment for fast data bit rate in time quanta
///
public const string PCANTP_BR_DATA_TSEG2 = "data_tseg2";
///
/// Synchronization Jump Width for highspeed data bit rate in time quanta
///
public const string PCANTP_BR_DATA_SJW = "data_sjw";
///
/// Secondary sample point delay for highspeed data bit rate in cyles
///
public const string PCANTP_BR_DATA_SAMPLE = "data_ssp_offset";
#endregion
#region Parameter values definition
///
/// No debug messages
///
public const byte PCANTP_DEBUG_NONE = 0;
///
/// Puts CAN debug messages to stdout
///
public const byte PCANTP_DEBUG_CAN = 1;
///
/// The Channel is illegal or not available
///
public const byte PCANTP_CHANNEL_UNAVAILABLE = 0;
///
/// The Channel is available
///
public const byte PCANTP_CHANNEL_AVAILABLE = 1;
///
/// The Channel is valid, and is being used
///
public const byte PCANTP_CHANNEL_OCCUPIED = 2;
///
/// if set Flow Control frame shall not use the WT flow status value
///
public const byte PCANTP_WFT_MAX_UNLIMITED = 0x00;
///
/// An integer describing the Wait Frame Transmissions parameter.
///
public const byte PCANTP_WFT_MAX_DEFAULT = 0x10;
///
/// Hide messages with type PCANTP_MESSAGE_INDICATION from CANTP_Read function
///
public const byte PCANTP_MSG_PENDING_HIDE = 0x00;
///
/// Show messages with type PCANTP_MESSAGE_INDICATION from CANTP_Read function
///
public const byte PCANTP_MSG_PENDING_SHOW = 0x01;
///
/// Uses CAN frame data optimization
///
public const byte PCANTP_CAN_DATA_PADDING_NONE = 0x00;
///
/// Uses CAN frame data padding (default, i.e. CAN DLC = 8)
///
public const byte PCANTP_CAN_DATA_PADDING_ON = 0x01;
///
/// Default value used if CAN data padding is enabled
///
public const byte PCANTP_CAN_DATA_PADDING_VALUE = 0x55;
///
/// Disable reception of unformatted (NON-ISO-TP) CAN frames (default):
/// only ISO 15765 messages will be received
///
public const byte PCANTP_CAN_UNSEGMENTED_OFF = 0x00;
///
/// Enable reception of unformatted (NON-ISO-TP) CAN frames:
/// received messages will be treated as either ISO 15765 or as an unformatted CAN frame
///
public const byte PCANTP_CAN_UNSEGMENTED_ON = 0x01;
///
/// Enable reception of unformatted (NON-ISO-TP) CAN frames:
/// received messages will be treated as ISO 15765, unformatted CAN frame, or both (user will able to read fragmented CAN frames
///
public const byte PCANTP_CAN_UNSEGMENTED_ALL_FRAMES = 0x02;
///
/// Default priority for ISO-TP messages (only available for normal fixed, mixed and enhanced addressing)
///
public const byte PCANTP_J1939_PRIORITY_DEFAULT = 0x06;
///
/// Mask to retrieve the CAN ID type
///
public const byte PCANTP_ID_CAN_MASK = 0x0F;
///
/// Mask to check if the priority field is set
///
public const byte PCANTP_ID_CAN_IS_PRIORITY_MASK = 0x10;
///
/// Mask to retrieve the J1939 priority
///
public const byte PCANTP_ID_CAN_PRIORITY_MASK = 0xE0;
///
/// Constant defining that no CAN ID mapping exists
///
public const uint CAN_ID_NO_MAPPING = 0xFFFFFFFF;
///
/// max data length prior ISO 15765-2:2016 update
///
public const uint PCANTP_MESSAGE_MAX_LENGTH = 0xfff;
#endregion
#region PCAN ISO-TP API Implementation
///
/// Initializes a PCANTP-Client based on a CANTP Channel
///
/// Only one PCANTP-Client can be initialized per CAN-Channel
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The CAN Hardware speed
/// NON PLUG-n-PLAY: The type of hardware and operation mode
/// NON PLUG-n-PLAY: The I/O address for the parallel port
/// NON PLUG-n-PLAY: Interrupt number of the parallel port
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_Initialize")]
public static extern TPCANTPStatus Initialize(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U2)]
TPCANTPBaudrate Baudrate,
[MarshalAs(UnmanagedType.U1)]
TPCANTPHWType HwType,
UInt32 IOPort,
UInt16 Interrupt);
///
/// Initializes a PCANTP-Client based on a CANTP Channel
///
/// Only one PCANTP-Client can be initialized per CAN-Channel
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
public static TPCANTPStatus Initialize(
TPCANTPHandle CanChannel,
TPCANTPBaudrate Baudrate)
{
return Initialize(CanChannel, Baudrate, (TPCANTPHWType)0, 0, 0);
}
///
/// Initializes a PCANTP-Client based on a CANTP Channel with FD support
///
/// Only one PCANTP-Client can be initialized per CAN-Channel
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The speed for the communication (FD bit rate string)
/// See PCANTP_BR_* values
/// Bit rate string must follow the following construction rules:
/// * parameters 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_samp
/// f_clock_mhz=80, nom_brp=1, nom_tset1=63, nom_tseg2=16, nom_sjw=7, data_brp=4, data_tset1=12, data_tseg2=7, data_sjw=1
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_InitializeFD")]
public static extern TPCANTPStatus InitializeFD(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
TPCANTPBitrateFD BitrateFD);
///
/// Uninitializes a PCANTP-Client initialized before
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_Uninitialize")]
public static extern TPCANTPStatus Uninitialize(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel);
///
/// Resets the receive and transmit queues of a PCANTP-Client
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_Reset")]
public static extern TPCANTPStatus Reset(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel);
///
/// Gets information about the internal BUS status of a PCANTP-Channel.
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_GetStatus")]
public static extern TPCANTPStatus GetStatus(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel);
///
/// Reads a PCANTP message from the receive queue of a PCANTP-Client
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPMsg structure buffer to store the PUDS message
/// "A TPCANTPTimestamp structure buffer to get
/// the reception time of the message. If this value is not desired, this parameter
/// should be passed as NULL"
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_Read")]
public static extern TPCANTPStatus Read(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
out TPCANTPMsg MessageBuffer,
out TPCANTPTimestamp TimestampBuffer);
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_Read")]
private static extern TPCANTPStatus Read(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
out TPCANTPMsg MessageBuffer,
IntPtr TimestampBuffer);
///
/// Reads a PCANTP message from the receive queue of a PCANTP-Client
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPMsg structure buffer to store the PUDS message
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
public static TPCANTPStatus Read(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
out TPCANTPMsg MessageBuffer)
{
return Read(CanChannel, out MessageBuffer, IntPtr.Zero);
}
///
/// Transmits a PCANTP message
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// A TPCANTPMsg buffer with the message to be sent
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_Write")]
public static extern TPCANTPStatus Write(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
ref TPCANTPMsg MessageBuffer);
///
/// Retrieves a PCANTP-Client value
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The TPCANTPParameter parameter to get
/// Buffer for the parameter value
/// Size in bytes of the buffer
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_GetValue")]
public static extern TPCANTPStatus GetValue(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPCANTPParameter Parameter,
StringBuilder StringBuffer,
UInt32 BufferLength);
///
/// Retrieves a PCANTP-Client value
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The TPCANTPParameter parameter to get
/// Buffer for the parameter value
/// Size in bytes of the buffer
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_GetValue")]
public static extern TPCANTPStatus GetValue(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPCANTPParameter Parameter,
out UInt32 NumericBuffer,
UInt32 BufferLength);
///
/// Retrieves a PCANTP-Client value
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The TPCANTPParameter parameter to get
/// Buffer for the parameter value
/// Size in bytes of the buffer
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_GetValue")]
public static extern TPCANTPStatus GetValue(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPCANTPParameter Parameter,
[MarshalAs(UnmanagedType.LPArray)]
[Out] Byte[] Buffer,
UInt32 BufferLength);
///
/// Configures or sets a PCANTP-Client value
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The TPCANTPParameter parameter to set
/// Buffer with the value to be set
/// Size in bytes of the buffer
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_SetValue")]
public static extern TPCANTPStatus SetValue(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPCANTPParameter Parameter,
ref UInt32 NumericBuffer,
UInt32 BufferLength);
///
/// Configures or sets a PCANTP-Client value
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The TPCANTPParameter parameter to set
/// Buffer with the value to be set
/// Size in bytes of the buffer
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_SetValue")]
public static extern TPCANTPStatus SetValue(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPCANTPParameter Parameter,
[MarshalAs(UnmanagedType.LPStr, SizeParamIndex = 3)]
string StringBuffer,
UInt32 BufferLength);
///
/// Configures or sets a PCANTP-Client value
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The TPCANTPParameter parameter to set
/// Buffer with the value to be set
/// Size in bytes of the buffer
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_SetValue")]
public static extern TPCANTPStatus SetValue(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPCANTPParameter Parameter,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]
Byte[] Buffer,
UInt32 BufferLength);
///
/// Adds a user-defined PCAN-TP mapping between CAN ID and Network Address Information
///
///
/// Defining a mapping enables ISO-TP communication with 11BITS CAN ID or
/// with opened Addressing Formats (like PCANTP_FORMAT_NORMAL or PCANTP_FORMAT_EXTENDED).
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The CAN ID to map
/// The CAN ID mapped response
/// CAN ID Type (11 or 29 bits, see PCANTP_ID_CAN_XXX)
/// Addressing format (see PCANTP_FORMAT_XXX)
/// Message type (remote or diagnostic, see PCANTP_MESSAGE_XXX)
/// Source Address
/// Target Address
/// Target Addressing Type (physical or functional, see PCANTP_ADDRESSING_XXX)
/// Address extension or Remote address
/// A TPCANTPStatus code : PCANTP_ERROR_OK is returned on success,
/// PCANTP_ERROR_WRONG_PARAM states invalid Network Address Information parameters.
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_AddMapping")]
public static extern TPCANTPStatus AddMapping(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
uint canID,
uint canIDResponse,
[MarshalAs(UnmanagedType.U1)]
TPCANTPIdType canIdType,
[MarshalAs(UnmanagedType.U1)]
TPCANTPFormatType formatType,
[MarshalAs(UnmanagedType.U1)]
TPCANTPMessageType msgType,
byte sourceAddr,
byte targetAddr,
[MarshalAs(UnmanagedType.U1)]
TPCANTPAddressingType targetType,
byte remoteAddr);
///
/// Removes a user-defined PCAN-TP mapping between CAN ID and Network Address Information
///
/// A PCANTP Channel Handle representing a PCANTP-Client
/// The mapped CAN ID to remove
/// A TPCANTPStatus code. PCANTP_ERROR_OK is returned on success
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_RemoveMapping")]
public static extern TPCANTPStatus RemoveMapping(
[MarshalAs(UnmanagedType.U2)]
TPCANTPHandle CanChannel,
uint canID);
///
/// Returns a descriptive text of a given TPCANTPStatus 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 TPCANTPStatus error code
/// Indicates a 'Primary language ID'
/// Buffer for the text (must be at least 256 in length)
/// A TPCANTPStatus error code
[DllImport("PCAN-ISO-TP.dll", EntryPoint = "CANTP_GetErrorText")]
public static extern TPCANTPStatus GetErrorText(
[MarshalAs(UnmanagedType.U4)]
TPCANTPStatus Error,
UInt16 Language,
StringBuilder StringBuffer);
#endregion
}
#endregion
}