Note
MessageBuffer is valid in this case.
public static TPCANStatus ReadFD(
ushort Channel,
out TPCANMsgFD MessageBuffer
)
Public Shared Function ReadFD (
Channel As UShort,
<OutAttribute> ByRef MessageBuffer As TPCANMsgFD
) As TPCANStatus
public:
static TPCANStatus ReadFD(
unsigned short Channel,
[OutAttribute] TPCANMsgFD% MessageBuffer
)
The return value is a TPCANStatus value. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:
The ReadFD method returns CAN frames from the reception queue. It is important to call ReadFD repeatedly until the reception queue becomes empty. In case there are no more messages to retrieve, the error PCAN_ERROR_QRCVEMPTY is returned. The error PCAN_ERROR_QRCVEMPTY is also returned if the reception of messages, configurable through the PCAN_RECEIVE_STATUS parameter, is disabled.
The receive queue can contain up to 32768 messages.
There are two possibilities for reading messages from the receive queue of a Channel:
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. 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:
The message type (see TPCANMessageType) of a CAN message indicates if the message is a 11-bit, 29-bit, FD, RTR, Error, or Status message. This value should be checked every time a message has been read successfully.
If the flag PCAN_MESSAGE_ERRFRAME is set in the TPCANMsgFD.MSGTYPE field, the message is an Error Frame.
If the flag PCAN_MESSAGE_STATUS is set in the TPCANMsgFD.MSGTYPE field, the message is a PCAN Status Frame.
The following example shows the use of ReadFD(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.
TPCANStatus result;
System.Text.StringBuilder strMsg;
TPCANMsgFD msg;
strMsg = new System.Text.StringBuilder(256);
// ... channel is initialized and communication takes place ...
do
{
// Check the receive queue for new messages
//
result = PCANBasic.ReadFD(PCANBasic.PCAN_USBBUS1, out msg);
if (result != TPCANStatus.PCAN_ERROR_QRCVEMPTY)
{
// Process the received message
//
Console.WriteLine("A message was received");
ProcessMessage(msg);
}
else
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(result, 0, strMsg);
Console.WriteLine(strMsg.ToString());
// Here can be decided if the loop has to be terminated (eg. the bus
// status is bus-off)
//
HandleReadError(result);
}
// Try to read a message from the receive queue of the PCAN-USB, Channel 1,
// until the queue is empty
//
} while ((result & TPCANStatus.PCAN_ERROR_QRCVEMPTY) != TPCANStatus.PCAN_ERROR_QRCVEMPTY);
Dim result As TPCANStatus
Dim strMsg As System.Text.StringBuilder
Dim msg As TPCANMsgFD = Nothing
strMsg = New System.Text.StringBuilder(256)
' ... channel is initialized and communication takes place ...
Do
' Check the receive queue for new messages
'
result = PCANBasic.ReadFD(PCANBasic.PCAN_USBBUS1, msg)
If result <> TPCANStatus.PCAN_ERROR_QRCVEMPTY Then
' Process the received message
'
Console.WriteLine("A message was received")
ProcessMessage(msg)
Else
' An error occurred, get a text describing the error and show it
'
PCANBasic.GetErrorText(result, 0, strMsg)
Console.WriteLine(strMsg.ToString())
' Here can be decided if the loop has to be terminated (eg. the bus
' status is bus-off)
'
HandleReadError(result)
End If
' Try to read a message from the receive queue of the PCAN-USB, Channel 1,
' until the queue is empty
'
Loop While ((result And TPCANStatus.PCAN_ERROR_QRCVEMPTY) <> TPCANStatus.PCAN_ERROR_QRCVEMPTY)
TPCANStatus result;
System::Text::StringBuilder^ strMsg;
TPCANMsgFD msg;
strMsg = gcnew System::Text::StringBuilder(256);
// ... channel is initialized and communication takes place ...
do
{
// Check the receive queue for new messages
//
result = PCANBasic::ReadFD(PCANBasic::PCAN_USBBUS1, msg);
if (result != TPCANStatus::PCAN_ERROR_QRCVEMPTY)
{
// Process the received message
//
Console::WriteLine("A message was received");
ProcessMessage(msg);
}
else
{
// An error occurred, get a text describing the error and show it
//
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
// Here can be decided if the loop has to be terminated (e.g. the bus
// status is bus-off)
//
HandleReadError(result);
}
// Try to read a message from the receive queue of the PCAN-USB, Channel 1,
// until the queue is empty
//
} while ((result & TPCANStatus::PCAN_ERROR_QRCVEMPTY) != TPCANStatus::PCAN_ERROR_QRCVEMPTY);