PCANBasicWrite Method
Transmits a CAN message.
Namespace: Peak.Can.Basic.BackwardCompatibilityAssembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
public static TPCANStatus Write(
ushort Channel,
ref TPCANMsg MessageBuffer
)
Public Shared Function Write (
Channel As UShort,
ByRef MessageBuffer As TPCANMsg
) As TPCANStatus
public:
static TPCANStatus Write(
unsigned short Channel,
TPCANMsg% MessageBuffer
)
- Channel UInt16
- The handle of a PCAN Channel (see PCAN Handle Definitions).
- MessageBuffer TPCANMsg
- 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
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.
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.
It is assumed that the channel was already initialized.
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");
Dim result As TPCANStatus
Dim strMsg As System.Text.StringBuilder
Dim msg As TPCANMsg
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 = CType(Array.CreateInstance(GetType(Byte), 8), Byte())
' A CAN message is configured
'
msg.ID = &H100
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, 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;
TPCANMsg^ msg;
strMsg = gcnew 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 = gcnew TPCANMsg();
msg->DATA = gcnew array<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, *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");