Initializing COMs over PCAN_USB via Python 3.4

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
capelzmann
Posts: 3
Joined: Fri 30. Jan 2015, 18:59

Initializing COMs over PCAN_USB via Python 3.4

Post by capelzmann » Mon 2. Feb 2015, 19:20

Hello all,

I apologize for the simple nature of this question in advance. I am new to CAN and I am attempting something I am not currently skilled for so my powers of deduction are still under development.

I am attempting to initialize communications with my CAN network using the PCAN_USB channel via Python 3.4. Unfortunately I need 3.4 to run other control systems functions not available in Python 2.6. My secondary short term objectives are:

1. Simple Read Function
2. Simple Write Function

I've been using the PCANBasic.py and PCANBasicExample.pyw for guidance along with posts such as Pbreaction1: (http://www.peak-system.com/forum/viewto ... ython#p957). Note I've attempted to update both PCANBasic and PCANBasicExample to 3.x via moderizie and manual replacement of functions with no luck regarding the Example.

Below is my current attempt to initialize communications, including the py file and the associated error when calling which is:
" AttributeError: 'c_ubyte' object has no attribute '_PCANBasic__m_dllBasic' "

How do I properly initialize communications?

Additional Notes:
System: Windows 7, 64-bit
Channel: PCAN_USBBUS1
Baud Rate: 500k

Hardware: PCAN USB
Successful Communications using PCAN-View and PCAN-MicroMod Configuration

Thank you sincerely in advance.
Attachments
PCAN_COM_Test1_20150202.png
PCAN_COM_Test1_20150202.png (543.4 KiB) Viewed 13253 times

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

Re: Initializing COMs over PCAN_USB via Python 3.4

Post by K.Wagner » Tue 3. Feb 2015, 10:03

Hello capelzmann,

your best start is using the Help file which is delivered within the PCAN-Basic package. There you will find minimalistic examples on each PCAN-Basic method, for example:
PCAN-Basic Help Document
PCAN-Basic Help Document
Python_Help.jpg (128.87 KiB) Viewed 13248 times
capelzmann wrote:1. Simple Read Function
2. Simple Write Function
Search in the help document for the methods "Read" and "Write" and you will see a small example on these operations within Python, as shown above.

About your code:
  • First of all, PCAN_NONEBUS is not intended to be used for communication.
  • PCAN_NONEBUS is not allowed within Initialize at all.
  • Initialize only received one parameter of type Handle. You are passing 2 in your code (PCAN_NONEBUS, and PCAN_USBBUS1)
I have installed Python 3.4 and used the Python shell to initialize the PCAN_USBBUS1 at 500Kb, to send a message, to receive one, and to uninitialize the channel again. See the picture bellow:
Simple Initialize-Read/Write-Uninitialize example
Simple Initialize-Read/Write-Uninitialize example
Python 3.4.jpg (192.08 KiB) Viewed 13248 times
Best regards,
Keneth

capelzmann
Posts: 3
Joined: Fri 30. Jan 2015, 18:59

Re: Initializing COMs over PCAN_USB via Python 3.4

Post by capelzmann » Wed 4. Feb 2015, 00:00

Keneth,

That is incredibly helpful. Thank you for your patience and effort.

Andy

capelzmann
Posts: 3
Joined: Fri 30. Jan 2015, 18:59

Re: Initializing COMs over PCAN_USB via Python 3.4

Post by capelzmann » Thu 5. Feb 2015, 02:44

For the record and to help any other new CAN coder, here is my functional code. I can successfully read and write to my CAN network. Note that another error in my implementation resulted from installing the 64-bit PCANBasic.dll instead of the 32-bit version. I am running 32-bit Python 3.4 on a Windows 7 64-bit machine.

Thank you again Keneth for the help.

Code: Select all

#PCANBus_COM_testMIN.py
#Communications test for CAN bus via Python -> USB -> CAN
#version 1.0
###############################################################################

#Useful Shell Commands
#import sys
#import imp
#sys.path.append('''C:\Python34\Projects\ARM''')
#import PCANBus_COM_testMIN
#imp.reload(PCANBus_COM_testMIN)

#------------------------------------------------------------------------------
#INITIALIZE
import sys
sys.path.append('''C:\Python34\Projects\ARM''')
sys.path.append('''C:\Python34\Lib\site-packages\python_can-1.4-py3.4.egg\can\interfaces''')

from PCANBasic import *         ## PCAN-Basic library import

channel = PCAN_USBBUS1
baud    = PCAN_BAUD_500K

# The Plug & Play Channel (PCAN-USB) is initialized
#
objPCAN = PCANBasic()
result = objPCAN.Initialize(channel, baud)
if result != PCAN_ERROR_OK:
    # An error occured, get a text describing the error and show it
    #
    result = objPCAN.GetErrorText(result)
    print(result[1])
else:
    print("PCAN-USBx was initialized")

# Check the status of the USB Channel
#
result = objPCAN.GetStatus(channel)
if result == PCAN_ERROR_BUSLIGHT:
    print("PCAN-USB (Ch-x): Handling a BUS-LIGHT status...")
elif result == PCAN_ERROR_BUSHEAVY:
    print("PCAN-USB (Ch-x): Handling a BUS-HEAVY status...")
elif result == PCAN_ERROR_BUSOFF:
    print("PCAN-USB (Ch-x): Handling a BUS-OFF status...")
elif result == PCAN_ERROR_OK:
    print("PCAN_USB (Ch-x): Status is OK")
else:
    # An error occured, get a text describing the error and show it
    #
    result = objPCAN.GetErrorText(result)
    print(result[1])
    
# Set Message Filter
#
result = objPCAN.FilterMessages(channel, 0x100, 0x199, PCAN_MESSAGE_STANDARD)
if result != PCAN_ERROR_OK:
    result = objPCAN.GetErrorText(result)
    print(result[1])
else:
    print("Message Filter Applied Successfully")

# Read message from the USB Channel
#
n = 0
readResult = PCAN_ERROR_OK,
while(n < 1):  #readResult[0] & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY:
    # Check the receive queue for new message
    #
    readResult = objPCAN.Read(channel)
    if readResult[0] == PCAN_ERROR_OK:
        # Process the received message
        #
        print("A message was received")
        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   )
        print('ID      = ',msg.ID)
        print('MSGTYPE = ',msg.MSGTYPE)
        print('LEN     = ',msg.LEN)
        print('DATA[0] = ',msg.DATA[0])
        print('DATA[1] = ',msg.DATA[1])
        print('DATA[2] = ',msg.DATA[2])
        print('DATA[3] = ',msg.DATA[3])
        print('DATA[4] = ',msg.DATA[4])
        print('DATA[5] = ',msg.DATA[5])
        print('DATA[6] = ',msg.DATA[6])
        print('DATA[7] = ',msg.DATA[7])
        
    else:
        # An error occured, get a text describing the error and show it
        #
        result = objPCAN.GetErrorText(readResult[0])
        print(result[1])
    n = n + 1

n = 0
while(n < 1):
    msg = TPCANMsg()
    msg.ID = 0x209
    msg.MSGTYPE = PCAN_MESSAGE_STANDARD
    msg.LEN = 8
    msg.DATA[0] = 0xFF
    msg.DATA[1] = 0x00
    msg.DATA[2] = 0x00
    msg.DATA[3] = 0x00
    msg.DATA[4] = 0x00
    msg.DATA[5] = 0x00
    msg.DATA[6] = 0x00
    msg.DATA[7] = 0x00

    result = objPCAN.Write(PCAN_USBBUS1,msg)
    if result != PCAN_ERROR_OK:
        # An error occured, get a text describing the error and show it
        #
        result = objPCAN.GetErrorText(result)
        print(result)
    else:
        print("Message sent successfully")
    n = n + 1

# All initialized channgels are released
#
objPCAN.Uninitialize(PCAN_NONEBUS)

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

Re: Initializing COMs over PCAN_USB via Python 3.4

Post by PEAK-Support » Thu 5. Feb 2015, 13:24

Thanyk you for sharing your code!
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

haseel
Posts: 9
Joined: Thu 27. Jul 2017, 09:09

Re: Initializing COMs over PCAN_USB via Python 3.4

Post by haseel » Thu 10. Aug 2017, 10:00

Dear capelzmann,

Thank you for sharing your code.

I am able to write the message over CAN BUS, but unable to use Read function.
Please find below the output .
-------------------------------------
PCAN-USBx was initialized

PCAN_USB (Ch-x): Status is OK

Message Filter Applied Successfully

The receive queue is empty
----------------------------------

This means "objPCAN.Getstatus(Channel)" = PCAN_ERROR_OK
but "objPCAN.Read(Channel)" = The receive queue is empty

What could be the probable reason?? Any idea

Thanks in advance

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

Re: Initializing COMs over PCAN_USB via Python 3.4

Post by PEAK-Support » Thu 10. Aug 2017, 10:26

PLEASE READ THE MANUAL
- you also have asked the same question in a other thread - please do not create support for nothing !

If you call the status function and get

Code: Select all

PCAN_ERROR_OK
it told you that tall is OK

if you call the read function and get

Code: Select all

PCAN_ERROR_QRCVEMPTY 
then the Receice Queu is empty - no CAN Frames on the BUS -- no messages in the queue!
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

Post Reply