Skip to content

Add Parameter for Enabling PCAN Auto Bus-Off Reset #1345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions can/interfaces/pcan/pcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
PCAN_CHANNEL_FEATURES,
FEATURE_FD_CAPABLE,
PCAN_DICT_STATUS,
PCAN_BUSOFF_AUTORESET,
)


Expand Down Expand Up @@ -206,6 +207,10 @@ def __init__(
In the range (1..16).
Ignored if not using CAN-FD.

:param bool auto_reset:
Enable automatic recovery in bus off scenario.
Resetting the driver takes ~500ms during which
it will not be responsive.
"""
self.m_objPCANBasic = PCANBasic()

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

if kwargs.get("auto_reset", False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the parameter to the __init__() method instead of using kwargs.get?
I do not know why some of the other parameters are not __init__-arguments but i thinks that's bad for readability.

result = self.m_objPCANBasic.SetValue(
self.m_PcanHandle, PCAN_BUSOFF_AUTORESET, PCAN_PARAMETER_ON
)

if result != PCAN_ERROR_OK:
raise PcanCanInitializationError(self._get_formatted_error(result))

if HAS_EVENTS:
self._recv_event = CreateEvent(None, 0, 0, None)
result = self.m_objPCANBasic.SetValue(
Expand Down
10 changes: 10 additions & 0 deletions test/test_pcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ def get_value_side_effect(handle, param):
self.bus = can.Bus(bustype="pcan", device_id=dev_id)
self.assertEqual(expected_result, self.bus.channel_info)

def test_bus_creation_auto_reset(self):
self.bus = can.Bus(bustype="pcan", auto_reset=True)
self.assertIsInstance(self.bus, PcanBus)
self.MockPCANBasic.assert_called_once()

def test_auto_reset_init_fault(self):
self.mock_pcan.SetValue = Mock(return_value=PCAN_ERROR_INITIALIZE)
with self.assertRaises(CanInitializationError):
self.bus = can.Bus(bustype="pcan", auto_reset=True)


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