PCAN read messages

This forum covers issues concerning multiple software products.

PCAN read messages

Postby ianoz69 » Tue 27. Feb 2018, 16:30

Hi,

First of all I am new in PeakCAN.

I would like to read CAN messages whenever there is a message in the queue (receive event triggered) and I have a GUI (textbox) that will show whether the message is received or fault or the queue is empty to user.

So far I have implemented this with the help of documentation and sample example


Code: Select all
public static TPCANStatus resultReceive;
       
        public static StringBuilder strMsgReceive;       

        public static UInt32 iBufferRECEIVE;       
       
        private delegate void ReadDelegateHandler();
        private ReadDelegateHandler m_ReadDelegate;       
        public static System.Threading.Thread m_ReadThread;
        private System.Threading.AutoResetEvent m_ReceiveEvent;

private void Initialize_Click(object sender, EventArgs e)
        {
            strMsgInitializeRelease = new StringBuilder(256);
            m_ReadDelegate = new ReadDelegateHandler(ReadMessages);           
            m_ReceiveEvent = new System.Threading.AutoResetEvent(false);           

            resultInitializeRelease = PCANBasic.Initialize(PCANBasic.PCAN_USBBUS1, TPCANBaudrate.PCAN_BAUD_125K);

            if (resultInitializeRelease != TPCANStatus.PCAN_ERROR_OK)
            {
                PCANBasic.GetErrorText(resultInitializeRelease, 0, strMsgInitializeRelease);               
                MessageReceive.Text = (strMsgInitializeRelease.ToString());
            }
            else
                MessageReceive.Text = ("PCAN-USB channel 1 is initialized");

            Initialize.Enabled = false;           
           
            System.Threading.ThreadStart threadDelegate = new
            System.Threading.ThreadStart(this.CANReadThreadFunc);
            m_ReadThread = new System.Threading.Thread(threadDelegate);
            m_ReadThread.IsBackground = true;
            m_ReadThread.Start();
           
            CAN();
            timerx.Elapsed += new ElapsedEventHandler(CAN_Send);
            timerx.Interval = 1292;
            timerx.Start();
            .
            .
            .
            .

          } 

private void CANReadThreadFunc()
        {         

            iBufferRECEIVE = Convert.ToUInt32(m_ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
           
            resultReceiveStatus = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBufferRECEIVE, sizeof(UInt32));           
           
            if (m_ReceiveEvent.WaitOne(50))               
                this.Invoke(m_ReadDelegate);
        }

        private void ReadMessages()
        {
            strMsgReceive = new StringBuilder(256);
            do
            {
                resultReceive = PCANBasic.Read(PCANBasic.PCAN_USBBUS1, out msg, out timestamp);
               
                if (resultReceive != TPCANStatus.PCAN_ERROR_QRCVEMPTY)
                {
                    MessageReceive.Text = ("A message was received");
                    //ProcessMessage(msg, time);
                }
                else
                {
                    PCANBasic.GetErrorText(resultReceive, 0, strMsgReceive);
                    MessageReceive.Text = (strMsgReceive.ToString());
                }

            } while ((resultReceive & TPCANStatus.PCAN_ERROR_QRCVEMPTY) != TPCANStatus.PCAN_ERROR_QRCVEMPTY);

        }       



Note: I am also sending multiple CAN messages using System.Timers.Timer. That’s why message receive part is in background thread as suggested in the documentation.

Problem: I am using CAN viewer to send message to my application to test whether my application receives any message or not. Regardless of sending any message through CAN viewer, in my GUI it always show the receive queue is empty…..

I can’t detect what am doing wrong or what I am missing in the code?

I am using PCAN USB (IPEH-002022 54691)
ianoz69
 
Posts: 3
Joined: Tue 9. Jan 2018, 14:56

Re: PCAN read messages

Postby K.Wagner » Wed 28. Feb 2018, 09:10

Hello,

Checking the code you posted I can see that you forgot a loop within the function CANReadThreadFunc. In your code, you check for messages in the queue only once because the thread is silently finished, i.e. the thread function returns after checking the event the first time.

This is your code:
Code: Select all
private void CANReadThreadFunc()
{         
    iBufferRECEIVE = Convert.ToUInt32(m_ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
           
    resultReceiveStatus = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBufferRECEIVE, sizeof(UInt32));           
           
    // After 50 ms the function returns, no matter if you got or not messages
    if (m_ReceiveEvent.WaitOne(50))
        this.Invoke(m_ReadDelegate);
}


and it should be:
Code: Select all
private void CANReadThreadFunc()

    iBufferRECEIVE = Convert.ToUInt32(m_ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
           
    resultReceiveStatus = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBufferRECEIVE, sizeof(UInt32));           

    //  This loop makes a check for messages periodically
    while(You_Still_Want_To_Read_Or_Communicate)
    {
        // if signaled, then process messages, if not, loop again
        if (m_ReceiveEvent.WaitOne(50))                     
            this.Invoke(m_ReadDelegate);
    }
}

In the sample projects the while is controlled by a radio button value.

Be careful when copy code. Try to understand it first.
Best regards,
Keneth
K.Wagner
Software Development
Software Development
 
Posts: 622
Joined: Wed 22. Sep 2010, 12:36
Location: Darmstadt, Germany

Re: PCAN read messages

Postby ianoz69 » Wed 28. Feb 2018, 12:01

Hi Wagner,

Thanks for your response and direction. I have put the while condition true as I always want to check the message queue to read………but still its showing queue is empty :| :| :| .

I forgot to mention earlier, I am trying to send message from PCAN viewer with 500ms cycle time to my application to read.

Code: Select all
private void CANReadThreadFunc()
        {
            strMsgReceiveStatus = new StringBuilder(256);

            iBufferRECEIVE = Convert.ToUInt32(m_ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
            // Sets the handle of the Receive-Event.
            //
            resultReceiveStatus = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBufferRECEIVE, sizeof(UInt32));
            //InvokeUpdateSet();

            // Waiting for Receive-Event
            //
           
            while(true)
            {
                if (m_ReceiveEvent.WaitOne(50))
                // Process Receive-Event using .NET Invoke function
                // in order to interact with Winforms UI (calling the
                // function ReadMessages)
                //
                this.Invoke(m_ReadDelegate);           
            }           
        }
ianoz69
 
Posts: 3
Joined: Tue 9. Jan 2018, 14:56

Re: PCAN read messages

Postby K.Wagner » Wed 28. Feb 2018, 12:24

Do you have successfully used the (unmodified) sample project to read messages in the same way you are trying to do with your own project? Use breakpoints and debug the sample project to see how things are working.

Please try things step by step if you are not sure where the problem ist. Try first to do a "manual read" from your application. When this work, then try to packt it in a timer. At the end, if that also work, you can try to use that code within a thread.Going directly to the difficult part without understanding how things work is not a good idea.

Notes:
  • if you are trying to send messages from a device to another (you have at least two CAN nodes), then make sure you are using the same bit rate in both, you program and PCAN-View, and that the CAN network is well terminated. Otherwise you will just receive CAN errors and will see no messages.
  • If you are using only one PCAN-USB device and both, your program and the PCAN-View, are connected to it, then messages sent from PCAN-View must be in the receive queue of the PCAN-Basic program... this is fact.
  • If you are using an USB 3.x then check for updates. Maybe here could be the problem (only when two PCAN-Views connected to the same device are showing the same wrong behavior - one seems not to receive the messages from the other one). Here more about USB 3.0 problems
Best regards,
Keneth
K.Wagner
Software Development
Software Development
 
Posts: 622
Joined: Wed 22. Sep 2010, 12:36
Location: Darmstadt, Germany


Return to Software



This website uses cookies for analytics, for logins to the online shop and the forum, and for the handling of an order. By browsing this website you agree to the use of cookies. Detailed information on their use can be found in our privacy policy.

OKPrivacy Policy