simple example to receive can message in python

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
klaus2
Posts: 5
Joined: Tue 4. Sep 2018, 07:21

simple example to receive can message in python

Post by klaus2 » Tue 4. Sep 2018, 07:35

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

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

Re: simple example to receive can message in python

Post by K.Wagner » Tue 4. Sep 2018, 07:43

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.
Best regards,
Keneth

klaus2
Posts: 5
Joined: Tue 4. Sep 2018, 07:21

Re: simple example to receive can message in python

Post by klaus2 » Tue 4. Sep 2018, 07:55

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;-)

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

Re: simple example to receive can message in python

Post by K.Wagner » Tue 4. Sep 2018, 08:37

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
Best regards,
Keneth

efariv
Posts: 3
Joined: Tue 24. Sep 2019, 00:41

Re: simple example to receive can message in python

Post by efariv » Tue 24. Sep 2019, 01:37

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

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

Re: simple example to receive can message in python

Post by K.Wagner » Tue 24. Sep 2019, 08:44

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.
Best regards,
Keneth

Post Reply