I'm trying to read CAN messages. But it always returns received queue is empty. When I look at the CAN channel with PCAN View, I can see messages. Below you can find the code. It's a C# interface that reads the CAN channel when clicked on a specific button.
Looking forward to your reply.
Code: Select all
private async void button1_Click(object sender, EventArgs e)
{
StringBuilder strMsg1;
TPCANStatus CANStatus;
TPCANMsg ReadMessageBuffer;
CANStatus = PCANBasic.Initialize(PCANBasic.PCAN_USBBUS1, TPCANBaudrate.PCAN_BAUD_500K);
if (CANStatus != TPCANStatus.PCAN_ERROR_OK)
{
strMsg1 = new StringBuilder(256);
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(CANStatus, 0, strMsg1);
MessageBox.Show(strMsg1.ToString());
}
else
{
strMsg1 = new StringBuilder(256);
// Check the receive queue for new messages
//
do
{
CANStatus = PCANBasic.Read(PCANBasic.PCAN_USBBUS1, out ReadMessageBuffer);
await Task.Delay(10);
} while ((CANStatus & TPCANStatus.PCAN_ERROR_QRCVEMPTY) != TPCANStatus.PCAN_ERROR_QRCVEMPTY);
if (CANStatus != TPCANStatus.PCAN_ERROR_QRCVEMPTY)
{
// Process the received message
//
if (ReadMessageBuffer.ID == 250)
{
// Process the received message
}
if (ReadMessageBuffer.ID == 251)
{
// Process the received message
}
}
else
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(CANStatus, 0, strMsg1);
MessageBox.Show(strMsg1.ToString());
}
CANStatus = PCANBasic.Uninitialize(PCANBasic.PCAN_USBBUS1);
if (CANStatus != TPCANStatus.PCAN_ERROR_OK)
{
strMsg1 = new StringBuilder(256);
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(CANStatus, 0, strMsg1);
MessageBox.Show(strMsg1.ToString());
}
}
}