PCANBasic: Event - triggered reading in Python
PCANBasic: Event - triggered reading in Python
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!
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!
Re: PCANBasic: Event - triggered reading in Python
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.
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.
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.
Which example do you refer? could you please post the code?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
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
Keneth
-
- Posts: 6
- Joined: Thu 26. Sep 2013, 14:11
Re: PCANBasic: Event - triggered reading in Python
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 .
- PEAK-Support
- Sales & Support
- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Re: PCANBasic: Event - triggered reading in Python
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
-------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
Re: PCANBasic: Event - triggered reading in Python
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.
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)
- PEAK-Support
- Sales & Support
- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Re: PCANBasic: Event - triggered reading in Python
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
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
-------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
Re: PCANBasic: Event - triggered reading in Python
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.
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.
Re: PCANBasic: Event - triggered reading in Python
Hello,
About our examples: We deliver two examples for Python within the PCAN-Basic package:
In both of them we show how to read periodically
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:I checked that code. I just want to listen the CAN bus, do I still need threading ?
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.hmluqman wrote:TkInter is GUI Lib but I am not using or creating GUI. Do I still need to use this library ?
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
Keneth