-
Notifications
You must be signed in to change notification settings - Fork 666
Specific Exceptions: Adapting cantact interface #1153
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,20 @@ | |
| from unittest.mock import Mock | ||
|
|
||
| from can import BusABC, Message | ||
| from ..exceptions import ( | ||
| CanInitializationError, | ||
| CanInterfaceNotImplementedError, | ||
| error_check, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| try: | ||
| import cantact | ||
| except ImportError: | ||
| cantact = None | ||
| logger.warning( | ||
| "The CANtact module is not installed. Install it using `python3 -m pip install cantact`" | ||
| "The CANtact module is not installed. Install it using `python -m pip install cantact`" | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -25,8 +31,10 @@ class CantactBus(BusABC): | |
| def _detect_available_configs(): | ||
| try: | ||
| interface = cantact.Interface() | ||
| except (NameError, SystemError): | ||
| # couldn't import cantact, so no configurations are available | ||
| except (NameError, SystemError, AttributeError): | ||
| logger.debug( | ||
| "Could not import or instantiate cantact, so no configurations are available" | ||
| ) | ||
| return [] | ||
|
|
||
| channels = [] | ||
|
|
@@ -42,7 +50,7 @@ def __init__( | |
| monitor=False, | ||
| bit_timing=None, | ||
| _testing=False, | ||
| **kwargs | ||
| **kwargs, | ||
| ): | ||
| """ | ||
| :param int channel: | ||
|
|
@@ -58,36 +66,45 @@ def __init__( | |
| if _testing: | ||
| self.interface = MockInterface() | ||
| else: | ||
| self.interface = cantact.Interface() | ||
| if cantact is None: | ||
| raise CanInterfaceNotImplementedError( | ||
| "The CANtact module is not installed. Install it using `python -m pip install cantact`" | ||
| ) | ||
| with error_check( | ||
| "Cannot create the cantact.Interface", CanInitializationError | ||
| ): | ||
| self.interface = cantact.Interface() | ||
|
|
||
| self.channel = int(channel) | ||
| self.channel_info = "CANtact: ch:%s" % channel | ||
|
|
||
| # configure the interface | ||
| if bit_timing is None: | ||
| # use bitrate | ||
| self.interface.set_bitrate(int(channel), int(bitrate)) | ||
| else: | ||
| # use custom bit timing | ||
| self.interface.set_bit_timing( | ||
| int(channel), | ||
| int(bit_timing.brp), | ||
| int(bit_timing.tseg1), | ||
| int(bit_timing.tseg2), | ||
| int(bit_timing.sjw), | ||
| ) | ||
| self.interface.set_enabled(int(channel), True) | ||
| self.interface.set_monitor(int(channel), monitor) | ||
| self.interface.start() | ||
| self.channel_info = f"CANtact: ch:{channel}" | ||
|
|
||
| # Configure the interface | ||
| with error_check("Cannot setup the cantact.Interface", CanInitializationError): | ||
| if bit_timing is None: | ||
| # use bitrate | ||
| self.interface.set_bitrate(int(channel), int(bitrate)) | ||
| else: | ||
| # use custom bit timing | ||
| self.interface.set_bit_timing( | ||
| int(channel), | ||
| int(bit_timing.brp), | ||
| int(bit_timing.tseg1), | ||
| int(bit_timing.tseg2), | ||
| int(bit_timing.sjw), | ||
| ) | ||
| self.interface.set_enabled(int(channel), True) | ||
| self.interface.set_monitor(int(channel), monitor) | ||
| self.interface.start() | ||
|
|
||
| super().__init__( | ||
| channel=channel, bitrate=bitrate, poll_interval=poll_interval, **kwargs | ||
| ) | ||
|
|
||
| def _recv_internal(self, timeout): | ||
| frame = self.interface.recv(int(timeout * 1000)) | ||
| with error_check("Cannot receive message"): | ||
| frame = self.interface.recv(int(timeout * 1000)) | ||
| if frame is None: | ||
| # timeout occured | ||
| # timeout occurred | ||
| return None, False | ||
|
|
||
| msg = Message( | ||
|
|
@@ -103,31 +120,33 @@ def _recv_internal(self, timeout): | |
| return msg, False | ||
|
|
||
| def send(self, msg, timeout=None): | ||
| self.interface.send( | ||
| self.channel, | ||
| msg.arbitration_id, | ||
| bool(msg.is_extended_id), | ||
| bool(msg.is_remote_frame), | ||
| msg.dlc, | ||
| msg.data, | ||
| ) | ||
| with error_check("Cannot send message"): | ||
| self.interface.send( | ||
| self.channel, | ||
| msg.arbitration_id, | ||
| bool(msg.is_extended_id), | ||
| bool(msg.is_remote_frame), | ||
| msg.dlc, | ||
| msg.data, | ||
| ) | ||
|
|
||
| def shutdown(self): | ||
| self.interface.stop() | ||
| with error_check("Cannot shutdown interface"): | ||
| self.interface.stop() | ||
|
|
||
|
|
||
| def mock_recv(timeout): | ||
| if timeout > 0: | ||
| frame = {} | ||
| frame["id"] = 0x123 | ||
| frame["extended"] = False | ||
| frame["timestamp"] = time.time() | ||
| frame["loopback"] = False | ||
| frame["rtr"] = False | ||
| frame["dlc"] = 8 | ||
| frame["data"] = [1, 2, 3, 4, 5, 6, 7, 8] | ||
| frame["channel"] = 0 | ||
| return frame | ||
| return { | ||
| "id": 0x123, | ||
| "extended": False, | ||
| "timestamp": time.time(), | ||
| "loopback": False, | ||
| "rtr": False, | ||
| "dlc": 8, | ||
| "data": [1, 2, 3, 4, 5, 6, 7, 8], | ||
| "channel": 0, | ||
| } | ||
| else: | ||
| # simulate timeout when timeout = 0 | ||
| return None | ||
|
|
@@ -144,7 +163,6 @@ class MockInterface: | |
| set_bit_timing = Mock() | ||
| set_enabled = Mock() | ||
| set_monitor = Mock() | ||
| start = Mock() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was duplicated |
||
| stop = Mock() | ||
| send = Mock() | ||
| channel_count = Mock(return_value=1) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice