Initializing COMs over PCAN_USB via Python 3.4
-
- Posts: 3
- Joined: Fri 30. Jan 2015, 18:59
Initializing COMs over PCAN_USB via Python 3.4
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.
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 (543.4 KiB) Viewed 13252 times
Re: Initializing COMs over PCAN_USB via Python 3.4
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:
About your code:
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:
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.capelzmann wrote:1. Simple Read Function
2. Simple Write Function
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)
Best regards,
Keneth
Keneth
-
- Posts: 3
- Joined: Fri 30. Jan 2015, 18:59
Re: Initializing COMs over PCAN_USB via Python 3.4
Keneth,
That is incredibly helpful. Thank you for your patience and effort.
Andy
That is incredibly helpful. Thank you for your patience and effort.
Andy
-
- Posts: 3
- Joined: Fri 30. Jan 2015, 18:59
Re: Initializing COMs over PCAN_USB via Python 3.4
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.
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)
- PEAK-Support
- Sales & Support
- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Re: Initializing COMs over PCAN_USB via Python 3.4
Thanyk you for sharing your code!
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
Re: Initializing COMs over PCAN_USB via Python 3.4
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
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
- PEAK-Support
- Sales & Support
- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Re: Initializing COMs over PCAN_USB via Python 3.4
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
it told you that tall is OK
if you call the read function and get
then the Receice Queu is empty - no CAN Frames on the BUS -- no messages in the queue!
- 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
if you call the read function and get
Code: Select all
PCAN_ERROR_QRCVEMPTY
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------