hardbyte / python-can

The can package provides controller area network support for Python developers
https://python-can.readthedocs.io
GNU Lesser General Public License v3.0
1.26k stars 599 forks source link

CAN-FD initialization error on PEAK device #1331

Open sortkim opened 2 years ago

sortkim commented 2 years ago

Describe the bug

CAN-FD initialization error on PEAK device (PCANUSB-FD) vector can works well with CAN-FD.

To Reproduce

h_can = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000, fd=True) # fail

Expected behavior

return bus object address

Additional context

OS and version: Windows10 Python version : 3.9.12 python-can version : 4.0.0 python-can interface/s (if applicable): PCAN-USB FD

Traceback and logs none none import can try: # h_can = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000) # pass h_can = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000, fd=True) # fail # h_can = can.interface.Bus(bustype='vector', channel=2, bitrate=500000, fd=True) # pass except: h_can = None if h_can is not None: tx_msg = can.Message(arbitration_id=100, dlc= 12, data=[0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4], is_fd=True) # tx_msg = can.Message(arbitration_id=100, dlc= 8, data=[0, 0, 0, 0, 0, 0, 0, 0]) h_can.send(tx_msg)
zariiii9003 commented 2 years ago

Traceback?

skipper85 commented 2 years ago

Hi there,

I also use PEAK system IPEH-004002 USBCAN converter with CAN FD support in combination with python-can, but also get error during initialization.

Context

OS and version: Windows 10 Python version: 3.9.13 python-can version: 4.0.0 python-can interface/s (if applicable): PCAN-USB FD

Code

bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000, fd=True)

Traceback

runfile('C:/Users/a1084371/python_can_ni/test_peak_pcan.py', wdir='C:/Users/a1084371/python_can_ni')
Traceback (most recent call last):

  File "C:\Users\a1084371\python_can_ni\ni_env_3_9\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "c:\users\a1084371\python_can_ni\test_peak_pcan.py", line 59, in <module>
    test_play_blf()

  File "c:\users\a1084371\python_can_ni\test_peak_pcan.py", line 29, in test_play_blf
    bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000, fd=True)

  File "C:\Users\a1084371\python_can_ni\ni_env_3_9\lib\site-packages\can\interface.py", line 120, in __new__
    bus = cls(channel, *args, **kwargs)

  File "C:\Users\a1084371\python_can_ni\ni_env_3_9\lib\site-packages\can\interfaces\pcan\pcan.py", line 246, in __init__
    raise PcanCanInitializationError(self._get_formatted_error(result))

PcanCanInitializationError: An unknown error has occurred

During debug the following variables in PcanBus init were noticed: result: 65536 self.fd_bitrate: b'f_clock_mhz=None' self.m_PcanHandle: c_ushort(81)

I would expect a different value for the fd_bitrate.

skipper85 commented 2 years ago

Hi there,

Apparently the setup for the CAN-FD is quite different from normal CAN. When using the following initialization

bus = can.interface.Bus(
        bustype='pcan',
        channel='PCAN_USBBUS1',
        fd=True,
        f_clock_mhz=80,
        nom_brp=1,
        nom_tseg1=138,
        nom_tseg2=21,
        nom_sjw=21,
        data_brp=1,
        data_tseg1=80,
        data_tseg2=79,
        data_sjw=79)

the device works fine. I used Kvaser calculator to calculate all the CAN-FD settings.

sortkim commented 2 years ago

Thank you everyone.

It works fine now. I took your advice into account.

zariiii9003 commented 2 years ago

Apparently the setup for the CAN-FD is quite different from normal CAN. When using the following initialization

the device works fine. I used Kvaser calculator to calculate all the CAN-FD settings.

I'd still consider it a bug. The implementation should have reasonable default values, so FD works without explicitely setting bit timings. But i don't have access to a peak interface.

j-c-cook commented 2 years ago

I have a PCAN-USB, but not the PCAN-USB FD, so I don't think I have the right hardware to help with this (if I'm wrong, let me know). Unfortunately, the FD device is out of stock. I'll ask around and see if I can locate a PEAK FD device to borrow for long enough to resolve this issue.