Can't Read CAN Messeges with PCANBasic

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
mert.sahin
Posts: 4
Joined: Fri 2. Nov 2018, 08:17

Can't Read CAN Messeges with PCANBasic

Post by mert.sahin » Fri 2. Nov 2018, 08:30

Hello,

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());
                }

            }
        }

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Can't Read CAN Messeges with PCANBasic

Post by K.Wagner » Fri 2. Nov 2018, 09:01

Hello,

you are reading right after initializing the channel until "the queue is empty". This means, you probably are reading the queue before a message has the chance to get in there, so that you abandon the read loop too early. Try using a timer for reading instead. You also can try to set a delay big enough between Initialize and the Read call.

In order to generally check if you are receiving messages, please start one of the demo projects and see if it receives messages.
Best regards,
Keneth

mert.sahin
Posts: 4
Joined: Fri 2. Nov 2018, 08:17

Re: Can't Read CAN Messeges with PCANBasic

Post by mert.sahin » Fri 2. Nov 2018, 09:41

Hello Keneth,
I tried to put a delay between Initialize and the Read call but again always gives "Received Queue is Empty". I couldn't find a simpler demo of CAN_Read(). You sample C# project runs smoothly on my pc without a problem. Any other suggestions?
Regards.

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Can't Read CAN Messeges with PCANBasic

Post by K.Wagner » Fri 2. Nov 2018, 10:36

Hello,

If the PCAN-Basic example works with the same device your application is using, then your application will work too. Check that you are using the right bit rate (and right PCAN-USB channel, if you have several of them). Also check that you are not restricting the filter somewhere in your code.

In order to have a "simple demo" of any function, you can just copy the sample codes within the help file. Each method has a minimalistic sample code in each supported programming language, for instance, here the c# codes for Initialize and Read:

PCANBasic.Initialize:

Code: Select all

TPCANStatus result;
StringBuilder strMsg;

// The Plug & Play Channel (PCAN-PCI) is initialized
//
result = PCANBasic.Initialize(PCANBasic.PCAN_PCIBUS2, TPCANBaudrate.PCAN_BAUD_500K);
if (result != TPCANStatus.PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    strMsg = new StringBuilder(256);
    PCANBasic.GetErrorText(result, 0, strMsg);
    MessageBox.Show(strMsg.ToString());
}
else
    MessageBox.Show("PCAN-PCI (Ch-2) was initialized");

// All initialized channels are released
//
PCANBasic.Uninitialize(PCANBasic.PCAN_NONEBUS);
PCANBasic.Read:

Code: Select all

TPCANStatus result;
StringBuilder strMsg;
TPCANMsg msg;

strMsg = new StringBuilder(256);

do
{
    // Check the receive queue for new messages
    //
    result = PCANBasic.Read(PCANBasic.PCAN_USBBUS1, out msg);
    if (result != TPCANStatus.PCAN_ERROR_QRCVEMPTY)
    {
        // Process the received message
        //
        MessageBox.Show("A message was received");
        ProcessMessage(msg);
    }
    else
    {
        // An error occurred, get a text describing the error and show it
        //
        PCANBasic.GetErrorText(result, 0, strMsg);
        MessageBox.Show(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);
Changes the codes so that the PCAN channel and bit rate fix to your needs, create a Timer with an interval of 50 milliseconds and call the Read code from there.
Best regards,
Keneth

mert.sahin
Posts: 4
Joined: Fri 2. Nov 2018, 08:17

Re: Can't Read CAN Messeges with PCANBasic

Post by mert.sahin » Fri 2. Nov 2018, 12:05

Hello,
Thanks for the suggestion. I was able to read messages from CAN channel. Thanks a lot.
Regards.

Post Reply