PCANBasicWriteFD Method
Transmits a CAN message using a FD capable PCAN Channel.
Namespace: Peak.Can.Basic.BackwardCompatibilityAssembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
public static TPCANStatus WriteFD(
ushort Channel,
ref TPCANMsgFD MessageBuffer
)
Public Shared Function WriteFD (
Channel As UShort,
ByRef MessageBuffer As TPCANMsgFD
) As TPCANStatus
public:
static TPCANStatus WriteFD(
unsigned short Channel,
TPCANMsgFD% MessageBuffer
)
- Channel UInt16
- The handle of a PCAN Channel (see PCAN Handle Definitions).
- MessageBuffer TPCANMsgFD
- A buffer containing the CAN message to be sent.
TPCANStatus
The return value is a TPCANStatus value. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:
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.
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.
It is assumed that the channel was already initialized.
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");
Dim result As TPCANStatus
Dim strMsg As System.Text.StringBuilder
Dim msg As TPCANMsgFD
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 = 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)
Console.WriteLine(strMsg.ToString)
Else
Console.WriteLine("Message sent successfully")
End If
TPCANStatus result;
System::Text::StringBuilder^ strMsg;
TPCANMsgFD^ msg;
strMsg = gcnew 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 = 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);
Console::WriteLine(strMsg->ToString());
}
else
Console::WriteLine("Message sent successfully");