Read(TPCANHandle)

PCAN-Basic

PCAN-Basic Documentation
Home
PreviousUpNext
Read(TPCANHandle)

Reads a CAN message and its time stamp from the receive queue of a PCAN Channel.

Syntax
def Read(
    self,
    Channel)
Parameters 
Description 
Channel 
The handle of a PCAN Channel (see TPCANHandle). 

The return value is a 3-touple. The order of the returned values is as follow: 

[0]: The method's return value as 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_BUSLIGHT: 
Indicates a bus error within the given PCAN Channel. The hardware is in bus-light status. 
PCAN_ERROR_BUSHEAVY: 
Indicates a bus error within the given PCAN Channel. The hardware is in bus-heavy status. 
PCAN_ERROR_BUSOFF: 
Indicates a bus error within the given PCAN Channel. The hardware is in bus-off status. 
PCAN_ERROR_QRCVEMPTY: 
Indicates that the receive queue of the Channel is empty. 

[1]: A TPCANMsg structure with the CAN message read. 

[2]: A TPCANTimestamp structure with the time when a message was read. 

The Read method returns received messages or status messages from the receive queue. It is important to call Read repeatedly until the queue is empty. In case there are no more messages in queue, the value PCAN_ERROR_QRCVEMPTY is returned. The error code PCAN_ERROR_QRCVEMPTY is also returned if the reception of messages is disabled. See Receive Status Parameter for more information. 

The receive queue can contain up to 32767 messages. 

There are two possibilities for reading messages from the receive queue of a Channel: 

Time-Triggered Reading: Consists in periodically calls to the Read method. Typically, an application start a timer that every 50 or 100 milliseconds check for messages, calling the Read method in a loop until the value of PCAN_ERROR_QRCVEMTY or another error condition is reached. 

Event-Triggered Reading: Consists in reacting to a notification sent by the PCAN driver to a registered application, when a message is received and inserted in its receive queue. See Using Events to obtain more information about reading with events. 

About bus errors / Status messages 

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, but only when no more clients are connected to that channel. 

The message type (see TPCANMessageType) of a CAN message indicates if the message is a 11-bit, 29-bit, RTR, Error, or Status message. This value should be checked every time a message has been read successfully. 

If the bit PCAN_MESSAGE_ERRFRAME is set in the TPCANMsg.MSGTYPE field, the message is an Error frame (see Error Frames). 

If the bit PCAN_MESSAGE_STATUS is set in the TPCANMsg.MSGTYPE field, the message is a Status message. The ID and LEN fields do not contain valid data. The first 4 data bytes of the message contain the Error Code. The MSB of the Error Code is in data byte 0, the LSB is in data byte 3. If a status message was read the return value of Read is also the error code. 

Examples:

Data0 
Data1 
Data2 
Data3 
Error 
Error Code 
Description 
00h 
00h 
00h 
02h 
PCAN_ERROR_OVERRUN 
0002h 
CAN controller has been read out too late. 
00h 
00h 
00h 
04h 
PCAN_ERROR_BUSLIGHT 
0004h 
Bus error: An error counter has reached the 'Light' limit. 
00h 
00h 
00h 
08h 
PCAN_ERROR_BUSHEAVY 
0008h 
Bus error: An error counter has reached the 'Heavy' limit. 
00h 
00h 
00h 
10h 
PCAN_ERROR_BUSOFF 
0010h 
Bus error: CAN Controller is in 'Bus Off' state. 

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 method Read 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 and that the following code is executed periodically. 

Python:  

readResult = PCAN_ERROR_OK,
while (readResult[0] & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY:
    # Check the receive queue for new messages
    #
    readResult = objPCAN.Read(PCAN_USBBUS1)
    if readResult[0] != PCAN_ERROR_QRCVEMPTY:
        # Process the received message
        #
        print "A message was received"
        ProcessMessage(result[1],result[2]) # Possible processing function, ProcessMessage(msg,timestamp)
    else:
        # An error occurred, get a text describing the error and show it
        #
        result = objPCAN.GetErrorText(readResult[0])
        print result[1]
        HandleReadError(readResult[0]) # Possible errors handling function, HandleError(function_result)

Write 

Using Events 

Error Frames 

 

Plain function Version: CAN_Read

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