Skip to content

Opening a PCanBus in FD mode with Bus.__new__ breaks with python-can 4.0.0 / 4.1.0 #1458

@lumagi

Description

@lumagi

Describe the bug

I am using the PCAN interface in an FD configuration with custom bit timings. I provide the parameters, i.e. f_clock, nom_brp, data_brp, ..., directly as kwargs to Bus.__new__ (see example below). With the transition from version v3.3.4 to v4.1.0, I can no longer instantiate the bus and always receive the error message below.

I took a look at the code in question, and it appears like this might be a corner case that was forgotten about when adding the BitTimingclass. When instantiating the PcanBus for a CAN-FD bus, it is mandatory to supply the f_clock parameter as well as the bit timings directly to the constructor. The bitrate parameter is ignored because the PCAN driver configures the interface directly based on the timing parameters (see pcan.py).
The exception is thrown in _create_bus_config in util.py. The function in question assumes that whenever certain timing-related arguments (such as f_clock, tseg1, tseg2 or brp) are present in kwargs, the bitrate argument must be present as well. For the case of the PcanBus, this is not the case since the interface implementation does not use the BitTiming class nor requires bitrate to be set when running in CAN-FD mode.

I did some searching in the repository and found issue #614 by @christiansandberg . I also added a comment there, but didn't receive a response.

To Reproduce

See code example below

Expected behavior

The constructor should not require the bitrate parameter to be set and instantiate the bus correctly.

Additional context

OS and version: Windows
Python version: 3.9
python-can version: 4.1.0
python-can interface/s (if applicable): PEAK PCAN-USB FD

Traceback and logs
Traceback (most recent call last):
  File "<...>", line 23, in <module>
    main()
  File "<...>", line 12, in main
    bus = Bus(**params)
  File "<...>", line 108, in __new__
    kwargs = load_config(config=kwargs, context=context)
  File "<...>", line 192, in load_config
    bus_config = _create_bus_config(config)
  File "<...>", line 252, in _create_bus_config
    timing_conf["bitrate"] = config["bitrate"]
KeyError: 'bitrate'
from can import Bus

from can.interfaces.pcan import PcanBus

def main():
    params = {'interface': 'pcan', 'fd': True, 'f_clock': 80000000, 'nom_brp': 1,
              'nom_tseg1': 129, 'nom_tseg2': 30, 'nom_sjw': 1, 'data_brp': 1,
              'data_tseg1': 9, 'data_tseg2': 6, 'data_sjw': 1, 'channel': 'PCAN_USBBUS1'}

    # Breaks
    bus = Bus(**params)

    # doesn't break
    bus = PcanBus(**params)

    while True:
        r = bus.recv()
        print(r)

if __name__ == "__main__":
    main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions