PCANBasic: Event - triggered reading in Python

The free CAN Software API (Application Programming Interface) for Windows®
Locked
AStefan
Posts: 1
Joined: Mon 16. Jan 2012, 18:43

PCANBasic: Event - triggered reading in Python

Post by AStefan » Mon 16. Jan 2012, 19:27

Hello,
I have some difficulties in reading CAN messages using events.
I've tried to call periodically the example from the the chm docu, but it seems that somehow I get only the last message received from the PCAN queue.
I've found in the PCANBasic chm documentation for python this parameter PCAN_RECEIVE_EVENT which should be set in order to read messages using events. There are some short examples available for reading CAN msg using this parameter? Thank you for support!

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

Re: PCANBasic: Event - triggered reading in Python

Post by K.Wagner » Tue 17. Jan 2012, 10:44

Hello AStefan,

For reading CAN messages using events you need a mechanism that allows you asynchronous function calls, i.e. you need a dedicated thread for Message-Reading.

You can take the examples for this written in .Net (C#, VB, etc), and try to port them yourself to Python since there are similarities. Here is a link with some information about Threading with Python.
AStefan wrote:I've tried to call periodically the example from the the chm docu, but it seems that somehow I get only the last message received from the PCAN queue
Which example do you refer? could you please post the code?

Please note that an Event is triggered when messages are placed in the receive queue. You will not receive one event for each received message. You have to keep reading the receive queue until this is empty, every time an event is triggered.
Best regards,
Keneth

rohan4pcan
Posts: 6
Joined: Thu 26. Sep 2013, 14:11

Re: PCANBasic: Event - triggered reading in Python

Post by rohan4pcan » Fri 27. Sep 2013, 13:31

Can this also be done using VBA or VBS ? I am trying to achieve something similar and any help or pointers in this regards would be highly appreciated .

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: PCANBasic: Event - triggered reading in Python

Post by PEAK-Support » Fri 27. Sep 2013, 14:21

no, VBS and VBA do not support multi threading which is needed to use the PCAN Basic receive event.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

hmluqman
Posts: 8
Joined: Wed 12. Aug 2015, 13:01

Re: PCANBasic: Event - triggered reading in Python

Post by hmluqman » Mon 17. Aug 2015, 17:51

Hello,

I am facing the same problem reading CAN messages using events. When I run the code it gives me stream of CAn packets but when qeue gets empty code exits. I just want to listen CAN bus no writing. I am struggling with using event handling using 'PCAN_RECEIVE_EVENT'. Can anyone help regarding event handling.

Code: Select all

readResult = PCAN_ERROR_OK,
while (readResult[0] & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY:
    readResult = objPCAN.Read(PCAN_USBBUS1)
    if readResult[0] == PCAN_ERROR_OK:
    # Process the received message
    #
        #print "A message was received"
        #PCANBasicExample.ProcessMessage(readResult[1:])
        # ProcessMessage(result[1],result[2]) # Possible processing function, ProcessMessage(msg,timestamp)
        #print('[0]',readResult[0])      # A TPCANStatus error code
        #print('[1]',readResult[1])      # A TPCANMsg structure with the CAN message read
        #print('[2]',readResult[2])      # A TPCANTimestamp structure with the time when the message was read
       
        msg = readResult[1] #readResult[1] TPCANMsg()
            #print('msg     = ',msg   )
        idhex=format(msg.ID,'x')
        print'ID = ',idhex,
        #print('MSGTYPE = ',msg.MSGTYPE)
        #print('DLC     = ',msg.DLC)
        print(format(msg.DATA[0],'02x')),
        print(format(msg.DATA[1],'02x')),
        print(format(msg.DATA[2],'02x')),
        print(format(msg.DATA[3],'02x')),
        print(format(msg.DATA[4],'02x')),
        print(format(msg.DATA[5],'02x')),
        print(format(msg.DATA[6],'02x')),
        print(format(msg.DATA[7],'02x'))
    else:
    # An error occurred, get a text describing the error and show it
    #
        result = objPCAN.GetErrorText(readResult[0])
        print result[1]
        #HandleReadError(readResult[0]) # Possible errors handling function, HandleError(function_result)


User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: PCANBasic: Event - triggered reading in Python

Post by PEAK-Support » Tue 18. Aug 2015, 08:27

You simply poll the queue - for using real receive events please read the first entry of this post done by Keneth - he also add a link to o page that shows how to handle threads in python.
We also have a two very good working sample which shows how to use the API when polling the queue, or using the event. See the latest PCAN-Basic Package in the sample folder. The samples need the Tkinker GUI LIB installed. See here
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

hmluqman
Posts: 8
Joined: Wed 12. Aug 2015, 13:01

Re: PCANBasic: Event - triggered reading in Python

Post by hmluqman » Tue 18. Aug 2015, 14:44

Thanks for your reply Wilhelm.
I am using windows and Python.
I checked that code. I just want to listen the CAN bus, do I still need threading ? second, TkInter is GUI Lib but I am not using or creating GUI. Do I still need to use this library ?
I am struggling with event handling in python.

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

Re: PCANBasic: Event - triggered reading in Python

Post by K.Wagner » Tue 18. Aug 2015, 15:29

Hello,
hmluqman wrote:I checked that code. I just want to listen the CAN bus, do I still need threading ?
Note that in order to "Listen the CAN bus" you need to periodically call CAN_Read. The standard way to do this is using a timer. Nevertheless, with Python, timers are also threads, so, the answer is YES, you need a thread to listen the CAN bus. Check this link to learn more about the timer class within Python.
hmluqman wrote:TkInter is GUI Lib but I am not using or creating GUI. Do I still need to use this library ?
tkinter, which is installed along with Python, is used with our examples, since we do have an GUI. If you want to run our examples, then you need it.

About our examples: We deliver two examples for Python within the PCAN-Basic package:
  • PCANBasicExample.pyw: showing how to read CAN frames per button click (manual read) and doing CAN polling (periodically read of CAN messages) with a timer.
  • PCANBasicExample - (Read Event).pyw: shows how to poll for messages using events.

In both of them we show how to read periodically
Best regards,
Keneth

Locked