PCANBasicWrite Method

Transmits a CAN message.

Definition

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

Parameters

Channel  UInt16
The handle of a PCAN Channel (see PCAN Handle Definitions).
MessageBuffer  TPCANMsg
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 Initialize 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-heavy and bus-light:

  • 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 Write(UInt16, TPCANMsg) 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;
TPCANMsg msg;

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

// ... channel is initialized ...

// A CAN message is created. The Data field (8 bytes)
// of the message must also be created
//
msg = new TPCANMsg();
msg.DATA = new Byte[8];

// A CAN message is configured
//
msg.ID = 0x100;
msg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
msg.LEN = 3;
msg.DATA[0] = 1;
msg.DATA[1] = 2;
msg.DATA[2] = 3;

// The message is sent using the PCAN-USB Channel 1
//
result = PCANBasic.Write(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