-
Notifications
You must be signed in to change notification settings - Fork 636
CANtact Support #853
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
CANtact Support #853
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5bf1a40
cantact support
ericevenchick 46314e9
Merge branch 'develop' into cantact
ericevenchick ef02781
add cantact to extras_require
ericevenchick 87c014b
Merge branch 'cantact' of github.com:ericevenchick/python-can into ca…
ericevenchick ef86ab0
formatting fixes
ericevenchick 08ba68a
rename class to follow convention, add tests
ericevenchick 8622223
update pip before running tests in travis-ci
ericevenchick 71bb78a
fix error in tests, modify pip upgrade
ericevenchick 6bcb8ee
remove dependency for tests
ericevenchick 896c760
add CI flag to travis
ericevenchick 328d2bb
disable SimpleCyclicSendTaskTest in CI (causes random failures)
ericevenchick ef3eac7
disable the correct test (test_thread_based_cyclic_send_task)
ericevenchick d8f86a7
fix formatting
ericevenchick 129aa02
improve docs, add timestamps, custom bit timing, and is_rx
ericevenchick af532b5
Merge branch 'develop' into cantact
ericevenchick 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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
""" | ||
Interface for CANtact devices from Linklayer Labs | ||
""" | ||
|
||
import time | ||
import logging | ||
from unittest.mock import Mock | ||
|
||
from can import BusABC, Message | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import cantact | ||
except ImportError: | ||
logger.warning( | ||
"The CANtact module is not installed. Install it using `python3 -m pip install cantact`" | ||
) | ||
|
||
|
||
class CantactBus(BusABC): | ||
"""CANtact interface""" | ||
|
||
@staticmethod | ||
def _detect_available_configs(): | ||
try: | ||
interface = cantact.Interface() | ||
except NameError: | ||
# couldn't import cantact, so no configurations are available | ||
return [] | ||
|
||
channels = [] | ||
for i in range(0, interface.channel_count()): | ||
channels.append({"interface": "cantact", "channel": "ch:%d" % i}) | ||
return channels | ||
|
||
def __init__( | ||
self, | ||
channel, | ||
bitrate=500000, | ||
poll_interval=0.01, | ||
monitor=False, | ||
bit_timing=None, | ||
_testing=False, | ||
**kwargs | ||
ericevenchick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
""" | ||
:param int channel: | ||
Channel number (zero indexed, labeled on multi-channel devices) | ||
:param int bitrate: | ||
Bitrate in bits/s | ||
:param bool monitor: | ||
If true, operate in listen-only monitoring mode | ||
:param BitTiming bit_timing | ||
Optional BitTiming to use for custom bit timing setting. Overrides bitrate if not None. | ||
""" | ||
|
||
if _testing: | ||
self.interface = MockInterface() | ||
else: | ||
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() | ||
|
||
super().__init__( | ||
channel=channel, bitrate=bitrate, poll_interval=poll_interval, **kwargs | ||
) | ||
|
||
def _recv_internal(self, timeout): | ||
frame = self.interface.recv(int(timeout * 1000)) | ||
if frame is None: | ||
# timeout occured | ||
return None, False | ||
|
||
msg = Message( | ||
arbitration_id=frame["id"], | ||
is_extended_id=frame["extended"], | ||
timestamp=frame["timestamp"], | ||
is_remote_frame=frame["rtr"], | ||
dlc=frame["dlc"], | ||
data=frame["data"][: frame["dlc"]], | ||
channel=frame["channel"], | ||
is_rx=(not frame["loopback"]), # received if not loopback frame | ||
) | ||
ericevenchick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
) | ||
|
||
def shutdown(self): | ||
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 | ||
else: | ||
# simulate timeout when timeout = 0 | ||
return None | ||
|
||
|
||
class MockInterface: | ||
""" | ||
Mock interface to replace real interface when testing. | ||
This allows for tests to run without actual hardware. | ||
""" | ||
|
||
start = Mock() | ||
set_bitrate = Mock() | ||
set_bit_timing = Mock() | ||
set_enabled = Mock() | ||
set_monitor = Mock() | ||
start = Mock() | ||
stop = Mock() | ||
send = Mock() | ||
channel_count = Mock(return_value=1) | ||
|
||
recv = Mock(side_effect=mock_recv) |
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
""" | ||
Tests for CANtact interfaces | ||
""" | ||
|
||
import time | ||
import logging | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
|
||
import can | ||
from can.interfaces import cantact | ||
|
||
|
||
class CantactTest(unittest.TestCase): | ||
def test_bus_creation(self): | ||
bus = can.Bus(channel=0, bustype="cantact", _testing=True) | ||
self.assertIsInstance(bus, cantact.CantactBus) | ||
cantact.MockInterface.set_bitrate.assert_called() | ||
cantact.MockInterface.set_bit_timing.assert_not_called() | ||
cantact.MockInterface.set_enabled.assert_called() | ||
cantact.MockInterface.set_monitor.assert_called() | ||
cantact.MockInterface.start.assert_called() | ||
|
||
def test_bus_creation_bittiming(self): | ||
cantact.MockInterface.set_bitrate.reset_mock() | ||
|
||
bt = can.BitTiming(tseg1=13, tseg2=2, brp=6, sjw=1) | ||
bus = can.Bus(channel=0, bustype="cantact", bit_timing=bt, _testing=True) | ||
self.assertIsInstance(bus, cantact.CantactBus) | ||
cantact.MockInterface.set_bitrate.assert_not_called() | ||
cantact.MockInterface.set_bit_timing.assert_called() | ||
cantact.MockInterface.set_enabled.assert_called() | ||
cantact.MockInterface.set_monitor.assert_called() | ||
cantact.MockInterface.start.assert_called() | ||
|
||
def test_transmit(self): | ||
bus = can.Bus(channel=0, bustype="cantact", _testing=True) | ||
msg = can.Message( | ||
arbitration_id=0xC0FFEF, data=[1, 2, 3, 4, 5, 6, 7, 8], is_extended_id=True | ||
) | ||
bus.send(msg) | ||
cantact.MockInterface.send.assert_called() | ||
|
||
def test_recv(self): | ||
bus = can.Bus(channel=0, bustype="cantact", _testing=True) | ||
frame = bus.recv(timeout=0.5) | ||
cantact.MockInterface.recv.assert_called() | ||
self.assertIsInstance(frame, can.Message) | ||
|
||
def test_recv_timeout(self): | ||
bus = can.Bus(channel=0, bustype="cantact", _testing=True) | ||
frame = bus.recv(timeout=0.0) | ||
cantact.MockInterface.recv.assert_called() | ||
self.assertIsNone(frame) | ||
|
||
def test_shutdown(self): | ||
bus = can.Bus(channel=0, bustype="cantact", _testing=True) | ||
bus.shutdown() | ||
cantact.MockInterface.stop.assert_called() |
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.
Uh oh!
There was an error while loading. Please reload this page.