PCANBasicWriteFD Method

Transmits a CAN message using a FD capable PCAN Channel.

Definition

Namespace: Peak.Can.Basic.BackwardCompatibility
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public static TPCANStatus WriteFD(
	ushort Channel,
	ref TPCANMsgFD MessageBuffer
)

Parameters

Channel  UInt16
The handle of a PCAN Channel (see PCAN Handle Definitions).
MessageBuffer  TPCANMsgFD
A buffer containing the CAN message to be sent.

Return Value

TPCANStatus

The return value is a TPCANStatus value. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:

Remarks

  Important

The use of Write and WriteFD is mutually exclusive. The PCAN Channel passed to this method must be initialized using the InitializeFD method. Otherwise the error PCAN_ERROR_ILLOPERATION is returned.

When the hardware status is bus-off, an application cannot communicate anymore. Consider using the PCAN_BUSOFF_AUTORESET parameter, which instructs the API to automatically reset the CAN controller when a bus-off state is detected.

Another way to reset errors like bus-off, bus-warning and bus-passive:

  • Performing an uninitialize / initialize cycle: This causes a hardware reset, but only when no more clients are connected to that channel.
  • Using the parameter PCAN_HARD_RESET_STATUS: It instructs this method to explicitly perform a hardware reset regardless of whether other clients are connected to that channel.

Example

The following example shows the use of WriteFD(UInt16, TPCANMsgFD) method 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.

C#
TPCANStatus result;
System.Text.StringBuilder strMsg;
TPCANMsgFD msg;

strMsg = new System.Text.StringBuilder(256);

// ... channel is initialized ...

// 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);
    Console.WriteLine(strMsg.ToString());
}
else
    Console.WriteLine("Message sent successfully");

See Also