Skip to content

Commit ed124c9

Browse files
1atabey1Atabey
and
Atabey
authored
Add Parameter for Enabling PCAN Auto Bus-Off Reset (#1345)
* parametrise pcan auto reset * add unit tests, fix build error * formatting Co-authored-by: Atabey <[email protected]>
1 parent c4f0789 commit ed124c9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

can/interfaces/pcan/pcan.py

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
PCAN_CHANNEL_FEATURES,
5757
FEATURE_FD_CAPABLE,
5858
PCAN_DICT_STATUS,
59+
PCAN_BUSOFF_AUTORESET,
5960
)
6061

6162

@@ -206,6 +207,10 @@ def __init__(
206207
In the range (1..16).
207208
Ignored if not using CAN-FD.
208209
210+
:param bool auto_reset:
211+
Enable automatic recovery in bus off scenario.
212+
Resetting the driver takes ~500ms during which
213+
it will not be responsive.
209214
"""
210215
self.m_objPCANBasic = PCANBasic()
211216

@@ -276,6 +281,14 @@ def __init__(
276281
"Ignoring error. PCAN_ALLOW_ERROR_FRAMES is still unsupported by OSX Library PCANUSB v0.10"
277282
)
278283

284+
if kwargs.get("auto_reset", False):
285+
result = self.m_objPCANBasic.SetValue(
286+
self.m_PcanHandle, PCAN_BUSOFF_AUTORESET, PCAN_PARAMETER_ON
287+
)
288+
289+
if result != PCAN_ERROR_OK:
290+
raise PcanCanInitializationError(self._get_formatted_error(result))
291+
279292
if HAS_EVENTS:
280293
self._recv_event = CreateEvent(None, 0, 0, None)
281294
result = self.m_objPCANBasic.SetValue(

test/test_pcan.py

+10
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@ def get_value_side_effect(handle, param):
363363
self.bus = can.Bus(bustype="pcan", device_id=dev_id)
364364
self.assertEqual(expected_result, self.bus.channel_info)
365365

366+
def test_bus_creation_auto_reset(self):
367+
self.bus = can.Bus(bustype="pcan", auto_reset=True)
368+
self.assertIsInstance(self.bus, PcanBus)
369+
self.MockPCANBasic.assert_called_once()
370+
371+
def test_auto_reset_init_fault(self):
372+
self.mock_pcan.SetValue = Mock(return_value=PCAN_ERROR_INITIALIZE)
373+
with self.assertRaises(CanInitializationError):
374+
self.bus = can.Bus(bustype="pcan", auto_reset=True)
375+
366376

367377
if __name__ == "__main__":
368378
unittest.main()

0 commit comments

Comments
 (0)