Skip to content

Commit 4136f37

Browse files
authored
Fix Bus.__new__ for PEAK CAN-FD interfaces (hardbyte#1460)
* Add failing unit test to verify that issue hardbyte#1485 is fixed hardbyte#1458 * Remove unused generic BitTiming creation in Bus.__new__ The BitTiming class is an attempt at unifying the various timing parameters of the individual interfaces. The idea is that instead of manually supplying multiple parameters that make up the timing definition of the interface, one can instead supply a single instance of the BitTiming class, which will also automatically calculate derivative values from its input. At the moment, this class is only used by two interfaces: CANtact and CANanalystii. Both either accept a single bitrate or a BitTiming instance. The latter will overrule the former. The code that is removed with this commit is part of the generic Bus.__new__ constructor. The removed code searches the set of kwargs parameters for timing-related values. If it finds at least one such value, it creates a BitTiming class instance and adds it to the list of parameters. However, it breaks compatibility with the PEAK interface, see issue hardbyte#1458. Additionally, the code in question is generic and applies to all interfaces. Instantiating a class here is prone to issues since it must be generic enough to fit all use cases. A better approach would be to simply forward the parameters as was done previously and leave it up to the individual interfaces to handle things properly. * Format code with black Co-authored-by: lumagi <[email protected]>
1 parent f3136fb commit 4136f37

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

can/util.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,6 @@ def _create_bus_config(config: Dict[str, Any]) -> typechecking.BusConfig:
233233
if "data_bitrate" in config:
234234
config["data_bitrate"] = int(config["data_bitrate"])
235235

236-
# Create bit timing configuration if given
237-
timing_conf = {}
238-
for key in (
239-
"f_clock",
240-
"brp",
241-
"tseg1",
242-
"tseg2",
243-
"sjw",
244-
"nof_samples",
245-
"btr0",
246-
"btr1",
247-
):
248-
if key in config:
249-
timing_conf[key] = int(str(config[key]), base=0)
250-
del config[key]
251-
if timing_conf:
252-
timing_conf["bitrate"] = config["bitrate"]
253-
config["timing"] = can.BitTiming(**timing_conf)
254-
255236
return cast(typechecking.BusConfig, config)
256237

257238

test/test_pcan.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,26 @@ def test_auto_reset_init_fault(self):
373373
with self.assertRaises(CanInitializationError):
374374
self.bus = can.Bus(bustype="pcan", auto_reset=True)
375375

376+
def test_peak_fd_bus_constructor_regression(self):
377+
# Tests that the following issue has been fixed:
378+
# https://github.com/hardbyte/python-can/issues/1458
379+
params = {
380+
"interface": "pcan",
381+
"fd": True,
382+
"f_clock": 80000000,
383+
"nom_brp": 1,
384+
"nom_tseg1": 129,
385+
"nom_tseg2": 30,
386+
"nom_sjw": 1,
387+
"data_brp": 1,
388+
"data_tseg1": 9,
389+
"data_tseg2": 6,
390+
"data_sjw": 1,
391+
"channel": "PCAN_USBBUS1",
392+
}
393+
394+
can.Bus(**params)
395+
376396

377397
if __name__ == "__main__":
378398
unittest.main()

0 commit comments

Comments
 (0)