CAN_Write

PCAN-Basic

PCAN-Basic Documentation
Home
PreviousUpNext
CAN_Write

Transmits a CAN message.

Syntax
TPCANStatus __stdcall CAN_Write(
        TPCANHandle Channel,
        TPCANMsg* 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 function 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 function 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, but only when no more clients are connected to that channel.

The following example shows the use of CAN_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++:  

TPCANMsg msg;
TPCANStatus result;
char strMsg[256];

// A CAN message is configured
//
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 = CAN_Write(PCAN_USBBUS1, &msg);
if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, strMsg);
    MessageBox(strMsg);
}
else
    MessageBox("Message sent successfully");

CAN_Read 

CAN_SetValue 

 

Class-method Version: Write

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