Page 1 of 1

simple example to receive can message in python

Posted: Tue 4. Sep 2018, 07:35
by klaus2
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

Re: simple example to receive can message in python

Posted: Tue 4. Sep 2018, 07:43
by K.Wagner
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.

Re: simple example to receive can message in python

Posted: Tue 4. Sep 2018, 07:55
by klaus2
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;-)

Re: simple example to receive can message in python

Posted: Tue 4. Sep 2018, 08:37
by K.Wagner
Ok,

you only need to import the PCANBasic library like this:
from PCANBasic import *
And then use the code shown within the methods Initialize, Read, and Uninitialize (modified to use always USBBUS1):
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"
Read:

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)
Uninitialize:

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"
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

Re: simple example to receive can message in python

Posted: Tue 24. Sep 2019, 01:37
by efariv
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

Re: simple example to receive can message in python

Posted: Tue 24. Sep 2019, 08:44
by K.Wagner
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.
efariv wrote:... where the function ProcessMessage() in Read is defined in PCANBasicExample.py instead of the PCANBasic.py file ...
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: For some reason I am unable to get python to import the method.
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:Also just to confirm is this setup intended to be used in Python 2.7 or 3.5; 32-bit or 64bit?
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.