How to use events with PCAN-Basics

This forum covers issues concerning multiple software products.

How to use events with PCAN-Basics

Postby christian_bajana » Tue 30. May 2017, 18:03

Hello,

I am writing a CAN application using PCAN-Basics in which everytime when it receives a CAN message, this message will be parsed (I used my own parsing)

Code: Select all
// Creates a new can message
   TPCANMsg msg = new TPCANMsg();
    while (true) {
     if (can.Read(TPCANHandle.PCAN_USBBUS1, msg, null) == TPCANStatus.PCAN_ERROR_OK) {
        List<SignalParsed> signals;
        //Here I use my own parsing which returns a list of signals
        signals = myDBC.parseMessage(msg);
                }
       }


The problem is that when no CAN message arrives, the while(true) loop wastes the processor time. What I want is when no CAN message comes the application goes in a sleep modus, and when it arrives it wakes up. The only way to do that is using events.

Can you give me an example in code how to use events thereby when a message comes triggest the event and wake up the processor?

I read that one can make this using setValue and setting the parameter to PC_RECEIVE_EVENT, but I dont know how to use it. I am new on this.

Thanks.
christian_bajana
 
Posts: 1
Joined: Tue 30. May 2017, 15:31

Re: How to use events with PCAN-Basics

Postby K.Wagner » Wed 31. May 2017, 08:37

Dear,

christian_bajana wrote:The problem is that when no CAN message arrives, the while(true) loop wastes the processor time. What I want is when no CAN message comes the application goes in a sleep modus, and when it arrives it wakes up. The only way to do that is using events.

Actually, the normal or standard way to do this is not reading forever (...while (true)...), but until the queue is empty. Using a timer, for instance, you have to read the queue until it is empty each time the time triggert the "OnTimer" event. Using an event object is actually the same. You will read the queue until it is empty every time you get an event signaled.

christian_bajana wrote:What I want is when no CAN message comes the application goes in a sleep modus, and when it arrives it wakes up. The only way to do that is using events.

You are talking about "Wait-Functions", like WaitForSingleObject or WaitForMultipleObject. To learn more about those and other wait mechanisms please refer to the MSDN help or check for programming forums in the internet. There are plenty of samples out there.

christian_bajana wrote:Can you give me an example in code how to use events thereby when a message comes triggest the event and wake up the processor?

The PCAN-Basic package contains sample projects for C#,VB.NET,C++/CLR, C++/MFC, C++ Builder, Delphi, and Python. All of theme show how to read the queue using a Timer, an Event, and manually. Please check the code.
Best regards,
Keneth
K.Wagner
Software Development
Software Development
 
Posts: 682
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