Hi
I am trying to write python receive CAN message on my PCAN USB device. The python example under PC interfaces\Windows\PCAN-Basic API\Samples\Python
is quite complex. Is there no example code with 10 lines?
Thanks
simple example to receive can message in python
Re: simple example to receive can message in python
Helio,
Please check the help for the method “Read”. After the description of the method you will see minimalistic examples of how to read a message in different programming languages, among others, in python.
Please check the help for the method “Read”. After the description of the method you will see minimalistic examples of how to read a message in different programming languages, among others, in python.
Best regards,
Keneth
Keneth
Re: simple example to receive can message in python
HI
I saw that, but it doesnt conain what I need to import and what I need to init!
Sorry for the stupid question, but my python knowledge is unfortunately limited. But still better than in the other languages which you provide;-)
I saw that, but it doesnt conain what I need to import and what I need to init!
Sorry for the stupid question, but my python knowledge is unfortunately limited. But still better than in the other languages which you provide;-)
Re: simple example to receive can message in python
Ok,
you only need to import the PCANBasic library like this:
Initialize:
Read:
Uninitialize:
Note that, even if it is complex, you will find all what you need in the python sample, and within the help files. Also note that teaching programming languages is not part of our support, so please visit a python forum if you still need help with the programming language.
Thanks for your understanding
you only need to import the PCANBasic library like this:
And then use the code shown within the methods Initialize, Read, and Uninitialize (modified to use always USBBUS1):from PCANBasic import *
Initialize:
Code: Select all
# The Plug & Play Channel (PCAN-USB) is initialized
#
objPCAN = PCANBasic()
result = objPCAN.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K)
if result != PCAN_ERROR_OK:
# An error occurred, get a text describing the error and show it
#
result = objPCAN.GetErrorText(result)
print result[1]
else:
print "PCAN-USB (Ch-1) was initialized"
Code: Select all
readResult = PCAN_ERROR_OK,
while (readResult[0] & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY:
# Check the receive queue for new messages
#
readResult = objPCAN.Read(PCAN_USBBUS1)
if readResult[0] != PCAN_ERROR_QRCVEMPTY:
# Process the received message
#
print "A message was received"
ProcessMessage(result[1],result[2]) # Possible processing function, ProcessMessage(msg,timestamp)
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)
Code: Select all
# The USB Channel is released
#
result = objPCAN.Uninitialize(PCAN_USBBUS1)
if result != PCAN_ERROR_OK:
# An error occurred, get a text describing the error and show it
#
result = objPCAN.GetErrorText(result)
print result[1]
else:
print "PCAN-USB (Ch-1) was released"
Thanks for your understanding
Best regards,
Keneth
Keneth
Re: simple example to receive can message in python
Hello,
I'm running into an issue following your steps where the function ProcessMessage() in Read is defined in PCANBasicExample.py instead of the PCANBasic.py file you said to include. For some reason I am unable to get python to import the method. Is it intended to import this other file as well? Also just to confirm is this setup intended to be used in Python 2.7 or 3.5; 32-bit or 64bit?
Thanks,
Eric
I'm running into an issue following your steps where the function ProcessMessage() in Read is defined in PCANBasicExample.py instead of the PCANBasic.py file you said to include. For some reason I am unable to get python to import the method. Is it intended to import this other file as well? Also just to confirm is this setup intended to be used in Python 2.7 or 3.5; 32-bit or 64bit?
Thanks,
Eric
Re: simple example to receive can message in python
Hello,
Note that the actual PCAN-Basic python module is "PCANBasic.py". PCANBasicExample.py, as its names indicate, is just an example on how to use the PCAN-Basic module.
Note that the actual PCAN-Basic python module is "PCANBasic.py". PCANBasicExample.py, as its names indicate, is just an example on how to use the PCAN-Basic module.
The method "ProcessMessage()" is not part of the API, it is just an example. It is a method that uses PCANBasic.Read to process CAN messages.efariv wrote:... where the function ProcessMessage() in Read is defined in PCANBasicExample.py instead of the PCANBasic.py file ...
If you want to use the method ProcessMessage() in your application, then you need to copy it (an their dependencies) to your python file.efariv wrote: For some reason I am unable to get python to import the method.
The PCANBasic module (PCANBasic.py) can be used with both python versions (2.7, 3.x), for both 32-bit and 64-bit compilers / platforms. You only need to use the right PCANBasic.dll with your script/application.efariv wrote:Also just to confirm is this setup intended to be used in Python 2.7 or 3.5; 32-bit or 64bit?
Best regards,
Keneth
Keneth