Skip to content

Commit b2ec42c

Browse files
committed
Implement is_fd property for BusABC and PCANBus
1 parent 75fdfe4 commit b2ec42c

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

can/bus.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class BusABC(metaclass=ABCMeta):
4848
def __init__(
4949
self,
5050
channel: Any,
51+
is_fd: bool = False,
5152
can_filters: Optional[can.typechecking.CanFilters] = None,
5253
**kwargs: object
5354
):
@@ -59,6 +60,9 @@ def __init__(
5960
:param channel:
6061
The can interface identifier. Expected type is backend dependent.
6162
63+
:param is_fd:
64+
Indicates that this bus supports CAN-FD.
65+
6266
:param can_filters:
6367
See :meth:`~can.BusABC.set_filters` for details.
6468
@@ -71,6 +75,7 @@ def __init__(
7175
:raises ~can.exceptions.CanInitializationError:
7276
If the bus cannot be initialized
7377
"""
78+
self._is_fd = is_fd
7479
self._periodic_tasks: List[_SelfRemovingCyclicTask] = []
7580
self.set_filters(can_filters)
7681

@@ -442,6 +447,10 @@ def state(self, new_state: BusState) -> None:
442447
"""
443448
raise NotImplementedError("Property is not implemented.")
444449

450+
@property
451+
def is_fd(self) -> bool:
452+
return self._is_fd
453+
445454
@staticmethod
446455
def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]:
447456
"""Detect all configurations/channels that this interface could

can/interfaces/pcan/pcan.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def __init__(
247247
raise ValueError(err_msg)
248248

249249
self.channel_info = str(channel)
250-
self.fd = isinstance(timing, BitTimingFd) if timing else kwargs.get("fd", False)
250+
is_fd = isinstance(timing, BitTimingFd) if timing else kwargs.get("fd", False)
251251

252252
hwtype = PCAN_TYPE_ISA
253253
ioport = 0x02A0
@@ -271,7 +271,7 @@ def __init__(
271271
result = self.m_objPCANBasic.Initialize(
272272
self.m_PcanHandle, pcan_bitrate, hwtype, ioport, interrupt
273273
)
274-
elif self.fd:
274+
elif is_fd:
275275
if isinstance(timing, BitTimingFd):
276276
timing = check_or_adjust_timing_clock(
277277
timing, sorted(VALID_PCAN_FD_CLOCKS, reverse=True)
@@ -338,7 +338,9 @@ def __init__(
338338
if result != PCAN_ERROR_OK:
339339
raise PcanCanInitializationError(self._get_formatted_error(result))
340340

341-
super().__init__(channel=channel, state=state, bitrate=bitrate, **kwargs)
341+
super().__init__(
342+
channel=channel, is_fd=is_fd, state=state, bitrate=bitrate, **kwargs
343+
)
342344

343345
def _find_channel_by_dev_id(self, device_id):
344346
"""
@@ -484,7 +486,7 @@ def _recv_internal(
484486
end_time = time.time() + timeout if timeout is not None else None
485487

486488
while True:
487-
if self.fd:
489+
if self.is_fd:
488490
result, pcan_msg, pcan_timestamp = self.m_objPCANBasic.ReadFD(
489491
self.m_PcanHandle
490492
)
@@ -546,7 +548,7 @@ def _recv_internal(
546548
error_state_indicator = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ESI.value)
547549
is_error_frame = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ERRFRAME.value)
548550

549-
if self.fd:
551+
if self.is_fd:
550552
dlc = dlc2len(pcan_msg.DLC)
551553
timestamp = boottimeEpoch + (pcan_timestamp.value / (1000.0 * 1000.0))
552554
else:
@@ -592,7 +594,7 @@ def send(self, msg, timeout=None):
592594
if msg.error_state_indicator:
593595
msgType |= PCAN_MESSAGE_ESI.value
594596

595-
if self.fd:
597+
if self.is_fd:
596598
# create a TPCANMsg message structure
597599
CANMsg = TPCANMsgFD()
598600

test/test_pcan.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ def test_bus_creation(self) -> None:
5353
self.bus = can.Bus(interface="pcan")
5454

5555
self.assertIsInstance(self.bus, PcanBus)
56-
self.MockPCANBasic.assert_called_once()
56+
self.assertFalse(self.bus.is_fd)
5757

58+
self.MockPCANBasic.assert_called_once()
5859
self.mock_pcan.Initialize.assert_called_once()
5960
self.mock_pcan.InitializeFD.assert_not_called()
6061

@@ -80,6 +81,8 @@ def test_bus_creation_fd(self, clock_param: str, clock_val: int) -> None:
8081
)
8182

8283
self.assertIsInstance(self.bus, PcanBus)
84+
self.assertTrue(self.bus.is_fd)
85+
8386
self.MockPCANBasic.assert_called_once()
8487
self.mock_pcan.Initialize.assert_not_called()
8588
self.mock_pcan.InitializeFD.assert_called_once()
@@ -452,10 +455,11 @@ def test_peak_fd_bus_constructor_regression(self):
452455

453456
def test_constructor_bit_timing(self):
454457
timing = can.BitTiming.from_registers(f_clock=8_000_000, btr0=0x47, btr1=0x2F)
455-
can.Bus(interface="pcan", channel="PCAN_USBBUS1", timing=timing)
458+
bus = can.Bus(interface="pcan", channel="PCAN_USBBUS1", timing=timing)
456459

457460
bitrate_arg = self.mock_pcan.Initialize.call_args[0][1]
458461
self.assertEqual(bitrate_arg.value, 0x472F)
462+
self.assertFalse(bus.is_fd)
459463

460464
def test_constructor_bit_timing_fd(self):
461465
timing = can.BitTimingFd(
@@ -469,7 +473,8 @@ def test_constructor_bit_timing_fd(self):
469473
data_tseg2=6,
470474
data_sjw=1,
471475
)
472-
can.Bus(interface="pcan", channel="PCAN_USBBUS1", timing=timing)
476+
bus = can.Bus(interface="pcan", channel="PCAN_USBBUS1", timing=timing)
477+
self.assertTrue(bus.is_fd)
473478

474479
bitrate_arg = self.mock_pcan.InitializeFD.call_args[0][-1]
475480

0 commit comments

Comments
 (0)