Write

PCAN-Basic

PCAN-Basic Documentation
Home
PreviousUpNext
Write

Transmits a CAN message.

Syntax
class function Write(
    Channel: TPCANHandle;
    var MessageBuffer: TPCANMsg
    ): TPCANStatus;
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")]
public static extern TPCANStatus Write(
    [MarshalAs(UnmanagedType.U1)]
    TPCANHandle Channel,
    ref TPCANMsg MessageBuffer);
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")]
static TPCANStatus Write(
    [MarshalAs(UnmanagedType::U1)]
    TPCANHandle Channel,
    TPCANMsg %MessageBuffer);
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Write")> _
Public Shared Function Write( _
    <MarshalAs(UnmanagedType.U1)> _
    ByVal Channel As TPCANHandle, _
    ByVal MessageBuffer As TPCANMsg) As TPCANStatus
End Function
def Write(
    self,
    Channel,
    MessageBuffer)
Parameters 
Description 
Channel 
The handle of a PCAN Channel (see TPCANHandle). 
MessageBuffer 
A TPCANMsg 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 TPCANMsg structure. 
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. 

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, BUSHEAVY, and BUSLIGTH, 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 Write 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;
StringBuilder strMsg;
TPCANMsg msg;

strMsg = new StringBuilder(256);

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

C++/CLR:

TPCANStatus result;
StringBuilder^ strMsg;
TPCANMsg^ msg;

strMsg = gcnew StringBuilder(256);

// 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);
    MessageBox::Show(strMsg->ToString());
}
else
    MessageBox::Show("Message sent successfully");

Visual Basic:

Dim result As TPCANStatus
Dim strMsg As StringBuilder
Dim msg As TPCANMsg

strMsg = New StringBuilder(256)

' 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)
    MessageBox.Show(strMsg.ToString)
Else
    MessageBox.Show("Message sent successfully")
End If

Pascal OO:

var
    result : TPCANStatus;
    strMsg: array [0..256] of Char;
    msg: TPCANMsg;
begin
    // A CAN message is configured
    //
    msg.ID := $100;
    msg.MSGTYPE := 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 := TPCANBasic.Write(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 = TPCANMsg()
msg.ID = 0x100
msg.MSGTYPE = 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 = objPCAN.Write(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"

Read 

SetValue 

 

Plain function Version: CAN_Write

Copyright © 2017. PEAK-System Technik GmbH. All rights reserved.
Send feedback to this documentation