Transmits a CAN message using a FD capable PCAN Channel.
class function WriteFD( Channel: TPCANHandle; var MessageBuffer: TPCANMsgFD ): TPCANStatus;
[DllImport("PCANBasic.dll", EntryPoint = "CAN_WriteFD")] public static extern TPCANStatus WriteFD( [MarshalAs(UnmanagedType.U1)] TPCANHandle Channel, ref TPCANMsgFD MessageBuffer);
[DllImport("PCANBasic.dll", EntryPoint = "CAN_WriteFD")] static TPCANStatus WriteFD( [MarshalAs(UnmanagedType::U1)] TPCANHandle Channel, TPCANMsgFD %MessageBuffer);
<DllImport("PCANBasic.dll", EntryPoint:="CAN_WriteFD")> _ Public Shared Function WriteFD( _ <MarshalAs(UnmanagedType.U1)> _ ByVal Channel As TPCANHandle, _ ByRef MessageBuffer As TPCANMsgFD) As TPCANStatus End Function
def WriteFD( self, Channel, MessageBuffer)
Parameters |
Description |
Channel |
The handle of a FD capable PCAN Channel (see TPCANHandle). |
MessageBuffer |
A TPCANMsgFD buffer containing the CAN message to be sent. |
The return value is a TPCANStatus code. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:
PCAN_ERROR_ILLPARAMVAL: |
Indicates that the parameters passed to the method are invalid. Check the value of the MessageBuffer; it should point to a TPCANMsgFD structure. |
PCAN_ERROR_ILLOPERATION: |
Indicates that the PCAN Channel passed to the method was not initialized using InitializeFD (plain function: CAN_InitializeFD). |
PCAN_ERROR_INITIALIZE: |
Indicates that the given PCAN channel was not found in the list of initialized channels of the calling application. |
PCAN_ERROR_BUSOFF: |
Indicates a bus error within the given PCAN Channel. The hardware is in bus-off status. |
PCAN_ERROR_QXMTFULL: |
Indicates that the transmit queue of the Channel is full. |
The use of Write and WriteFD are mutually exclusive. The PCAN Channel passed to this method must be initialized using InitializeFD (plain function: CAN_InitializeFD). Otherwise the error PCAN_ERROR_ILLOPERATION is returned.
If a bus-off error occur, an application cannot use the channel to communicate anymore, until the CAN controller is reset. With PCAN-Basic it is not possible to reset the CAN controller through a method directly. Consider using the PCAN-Basic property PCAN_BUSOFF_AUTORESET which instructs the API to automatically reset the CAN controller when a bus-off state is detected.
Another way to reset errors like BUSOFF, BUSWARNING, and BUSPASSIVE, is to uninitialize and initialise again the channel used. This causes a hardware reset.
Python Notes
- Class-Method: Unlike the .NET Framework, under Python a variable has to be instantiated with an object of type PCANBasic in order to use the API functionality.
- Python's first argument convention: Under Python, 'self' is a parameter that is automatically included within the call of this method, within a PCANBasic object and hasn't to be indicated in a method call. This parameter represents the calling object itself.
The following example shows the use of the method WriteFD on the channel PCAN_USBBUS1. In case of failure, the returned code will be translated to a text (according with the operating system language) in English, German, Italian, French or Spanish, and it will be shown to the user.
Note: It is assumed that the channel was already initialized using the method InitializeFD.
C#:
TPCANStatus result; StringBuilder strMsg; TPCANMsgFD msg; strMsg = new StringBuilder(256); // A CAN message is created. The Data field (64 bytes) // of the message must also be created // msg = new TPCANMsgFD(); msg.DATA = new Byte[64]; // A CAN message is configured // msg.ID = 0x100; msg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD | TPCANMessageType.PCAN_MESSAGE_FD; // DLC 9 means 12 data bytes // msg.DLC = 9; for(byte i=0; i < 12; i++) msg.DATA[i] = i; // The message is sent using the PCAN-USB Channel 1 // result = PCANBasic.WriteFD(PCANBasic.PCAN_USBBUS1, ref msg); if (result != TPCANStatus.PCAN_ERROR_OK) { // An error occurred, get a text describing the error and show it // PCANBasic.GetErrorText(result, 0, strMsg); MessageBox.Show(strMsg.ToString()); } else MessageBox.Show("Message sent successfully");
C++/CLR:
TPCANStatus result; StringBuilder^ strMsg; TPCANMsgFD^ msg; strMsg = gcnew StringBuilder(256); // A CAN message is created. The Data field (64 bytes) // of the message must also be created // msg = gcnew TPCANMsgFD(); msg->DATA = gcnew array<Byte>(64); // A CAN message is configured // msg->ID = 0x100; msg->MSGTYPE = TPCANMessageType::PCAN_MESSAGE_STANDARD | TPCANMessageType::PCAN_MESSAGE_FD; // DLC 9 means 12 data bytes // msg->DLC = 9; for(Byte i=0; i < 12; i++) msg->DATA[i] = i; // The message is sent using the PCAN-USB Channel 1 // result = PCANBasic::WriteFD(PCANBasic::PCAN_USBBUS1, *msg); if (result != TPCANStatus::PCAN_ERROR_OK) { // An error occurred, get a text describing the error and show it // PCANBasic::GetErrorText(result, 0, strMsg); MessageBox::Show(strMsg->ToString()); } else MessageBox::Show("Message sent successfully");
Visual Basic:
Dim result As TPCANStatus Dim strMsg As StringBuilder Dim msg As TPCANMsgFD strMsg = New StringBuilder(256) ' A CAN message is created. The Data field (64 bytes) ' of the message must also be created ' msg = New TPCANMsgFD msg.DATA = CType(Array.CreateInstance(GetType(Byte), 64), Byte()) ' A CAN message is configured ' msg.ID = &H100 msg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD Or TPCANMessageType.PCAN_MESSAGE_FD ' DLC 9 means 12 data bytes ' msg.DLC = 9 For i As Byte = 0 To 11 msg.DATA(i) = i Next ' The message is sent using the PCAN-USB Channel 1 ' result = PCANBasic.WriteFD(PCANBasic.PCAN_USBBUS1, msg) If result <> TPCANStatus.PCAN_ERROR_OK Then ' An error occurred, get a text describing the error and show it ' PCANBasic.GetErrorText(result, 0, strMsg) MessageBox.Show(strMsg.ToString) Else MessageBox.Show("Message sent successfully") End If
Pascal OO:
var result : TPCANStatus; strMsg: array [0..256] of Char; msg: TPCANMsgFD; I: Integer; begin // A CAN message is configured // msg.ID := $100; msg.MSGTYPE := TPCANMessageType(Byte(PCAN_MESSAGE_STANDARD) OR Byte(PCAN_MESSAGE_FD)); // DLC 9 means 12 data bytes // msg.DLC := 9; for I:=0 To 11 do msg.DATA[I] := I; // The message is sent using the PCAN-USB Channel 1 // result := TPCANBasic.WriteFD(TPCANBasic.PCAN_USBBUS1,msg); If (result <> PCAN_ERROR_OK) Then begin // An error occurred, get a text describing the error and show it // TPCANBasic.GetErrorText(result, 0, strMsg); MessageBox(0, strMsg, 'Error',MB_OK); end else MessageBox(0,'Message sent successfully','Success', MB_OK);
Python:
# A CAN message is configured # msg = TPCANMsgFD() msg.ID = 0x100 msg.MSGTYPE = PCAN_MESSAGE_STANDARD.value | PCAN_MESSAGE_FD.value # DLC 9 means 12 data bytes # msg.DLC = 9 for i in range(12): msg.DATA[i] = i # The message is sent using the PCAN-USB Channel 1 # result = objPCAN.WriteFD(PCAN_USBBUS1,msg) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result else: print "Message sent successfully"
Copyright © 2017. PEAK-System Technik GmbH. All rights reserved.
|
Send feedback to this documentation
|