Python Code:
from can import *
def myFunc():
USBChannel = 'PCAN_USBBUS1'
baudRate = 500000
with Bus(bustype='pcan', channel=USBChannel, bitrate=baudRate) as busRxClient:
busRxClient.state = bus.BusState.ACTIVE
try:
#My Code in python
except CanError as e:
#Exception handling in python
Error Output:
with Bus(bustype=‘pcan’, channel=USBChannel, bitrate=baudRate) as busRxClient:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages\can\util.py”, line 378, in wrapper
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File “C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages\can\interface.py”, line 121, in Bus
kwargs = util.load_config(config=kwargs, context=config_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages\can\util.py”, line 182, in load_config
can.rc,
^^^^^^
AttributeError: ‘dict_itemiterator’ object has no attribute ‘rc’
I made small change in the code as follows still getting the same error:
from can import *
def myFunc():
USBChannel = 'PCAN_USBBUS1'
baudRate = 500000
with interface.Bus(interface='pcan', channel=USBChannel, bitrate=baudRate) as busRxClient:
busRxClient.state = can.bus.BusState.ACTIVE
try:
#My Code in python
except CanError as e:
#Exception handling in python
with interface.Bus(interface='pcan', channel=USBChannel, bitrate=boudRate) as busRxClient:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages\can\util.py”, line 378, in wrapper
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File “C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages\can\interface.py”, line 121, in Bus
kwargs = util.load_config(config=kwargs, context=config_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages\can\util.py”, line 182, in load_config
can.rc,
^^^^^^
AttributeError: ‘dict_itemiterator’ object has no attribute ‘rc’
Following is the information of my python-can library:
Command prompt command: pip show python-can
Output:
Name: python-can
Version: 4.4.0
Summary: Controller Area Network interface module for Python
Home-page:
Author: python-can contributors
Author-email:
License: LGPL v3
Location: C:\Users\emb-vikrkas\AppData\Local\Programs\Python\Python311\Lib\site-packages
Requires: packaging, pywin32, typing-extensions, wrapt
Required-by:
I am handling the above exception just to avoid error log in the console following is my complete log which you can see below, basically I am doing this in the loop where I am initializing the bus transmitting the CAN message shutting down the bus. Again opening the bus receiving the response message and shutting down the bus. Following is my console log:
timeStamp: 1718727337.464577
Module ‘can’ imported successfully!
Bus instance created successfully!
Message class exists…
1 pMessage is not NULL…
CANIL_Python_Message.TimeStamp: 1718727337.464577
Send method exists…
CAN message sent successfully!
Message: Timestamp: 1718727337.464577 ID: 7c1 S Tx DL: 8 02 10 01 00 00 00 00 00
BUS shut down successfully!
result: 0
Inside recvMsgP2Server!!!
USBChannel: PCAN_USBBUS1 baudRate: 500000
self.busRxClient bus is started…
Inside recvMsgP2StarServer!!!
Inside recvMsgP2StarServer self.msg: Timestamp: 1.090882 ID: 7c9 S Rx DL: 8 06 50 01 00 32 01 f4 ff
Retval inside recvMsgP2StarServer 0
2 self.busRxClient bus is shutdown inside recvMsgP2StarServer…
timer value: 812
startButtonClicked: 0
startExecutionClicked: 1
timeStamp: 1718727338.771896
Module ‘can’ imported successfully!
Bus instance created successfully!
Message class exists…
1 pMessage is not NULL…
CANIL_Python_Message.TimeStamp: 1718727338.771896
Send method exists…
CAN message sent successfully!
Message: Timestamp: 1718727338.771896 ID: 7c1 S Tx DL: 8 02 10 02 00 00 00 00 00
BUS shut down successfully!
result: 0
Inside recvMsgP2Server!!!
USBChannel: PCAN_USBBUS1 baudRate: 500000
self.busRxClient bus is started…
Inside recvMsgP2StarServer!!!
Inside recvMsgP2StarServer self.msg: Timestamp: 2.414082 ID: 7c9 S Rx DL: 8 06 50 02 00 32 01 f4 ff
Retval inside recvMsgP2StarServer 0
2 self.busRxClient bus is shutdown inside recvMsgP2StarServer…
timer value: 760
timeStamp: 1718727340.114089
Module ‘can’ imported successfully!
Bus instance created successfully!
Message class exists…
1 pMessage is not NULL…
CANIL_Python_Message.TimeStamp: 1718727340.114089
Send method exists…
CAN message sent successfully!
Message: Timestamp: 1718727340.114089 ID: 7c1 S Tx DL: 8 02 10 03 00 00 00 00 00
BUS shut down successfully!
result: 0
Inside recvMsgP2Server!!!
USBChannel: PCAN_USBBUS1 baudRate: 500000
self.busRxClient bus is started…
Inside recvMsgP2StarServer!!!
Inside recvMsgP2StarServer self.msg: Timestamp: 3.779751 ID: 7c9 S Rx DL: 8 06 50 03 00 32 01 f4 ff
Retval inside recvMsgP2StarServer 0
2 self.busRxClient bus is shutdown inside recvMsgP2StarServer…
timer value: 748
timeStamp: 1718727341.452743
Module ‘can’ imported successfully!
Bus instance created successfully!
Message class exists…
1 pMessage is not NULL…
CANIL_Python_Message.TimeStamp: 1718727341.452743
Send method exists…
CAN message sent successfully!
Message: Timestamp: 1718727341.452743 ID: 7c1 S Tx DL: 8 02 10 04 00 00 00 00 00
BUS shut down successfully!
result: 0
Inside recvMsgP2Server!!!
USBChannel: PCAN_USBBUS1 baudRate: 500000
Failed to create CAN bus!!!
Error: ‘dict_itemiterator’ object has no attribute ‘rc’
messageTxAttemptCount: 1
timeStamp: 1718727341.682145