Skip to content

Commit c034773

Browse files
authored
Merge pull request hardbyte#393 from yegorich/url_to_serial
serial: switch to serial_for_url instead of Serial
2 parents 80ca49f + 05261fc commit c034773

5 files changed

Lines changed: 38 additions & 18 deletions

File tree

can/interfaces/serial/serial_can.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def __init__(self, channel, baudrate=115200, timeout=0.1, *args, **kwargs):
5454
raise ValueError("Must specify a serial port.")
5555

5656
self.channel_info = "Serial interface: " + channel
57-
self.ser = serial.Serial(channel, baudrate=baudrate, timeout=timeout)
57+
self.ser = serial.serial_for_url(
58+
channel, baudrate=baudrate, timeout=timeout)
5859

5960
super(SerialBus, self).__init__(channel=channel, *args, **kwargs)
6061

can/interfaces/slcan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def __init__(self, channel, ttyBaudrate=115200, timeout=1, bitrate=None, **kwarg
6565
if '@' in channel:
6666
(channel, ttyBaudrate) = channel.split('@')
6767

68-
self.serialPortOrig = serial.Serial(channel, baudrate=ttyBaudrate, timeout=timeout)
68+
self.serialPortOrig = serial.serial_for_url(
69+
channel, baudrate=ttyBaudrate, timeout=timeout)
6970

7071
time.sleep(self._SLEEP_AFTER_SERIAL_OPEN)
7172

doc/interfaces/serial.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ CAN over Serial
44
===============
55
A text based interface. For example use over serial ports like
66
``/dev/ttyS1`` or ``/dev/ttyUSB0`` on Linux machines or ``COM1`` on Windows.
7+
Remote ports can be also used via a special URL. Both raw TCP sockets as
8+
also RFC2217 ports are supported: ``socket://192.168.254.254:5000`` or
9+
``rfc2217://192.168.254.254:5000``. In addition a virtual loopback can be
10+
used via ``loop://`` URL.
711
The interface is a simple implementation that has been used for
812
recording CAN traces.
913

doc/interfaces/slcan.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ CAN over Serial / SLCAN
55

66
A text based interface: compatible to slcan-interfaces (slcan ASCII protocol) should also support LAWICEL direct.
77
These interfaces can also be used with socketcan and slcand with Linux.
8-
This driver directly uses the serial port, it makes slcan-compatible interfaces usable with Windows also.
8+
This driver directly uses either the local or remote serial port, it makes slcan-compatible interfaces usable with Windows also.
9+
Remote serial ports will be specified via special URL. Both raw TCP sockets as also RFC2217 ports are supported.
910

10-
Usage: use ``port[@baurate]`` to open the device.
11-
For example use ``/dev/ttyUSB0@115200`` or ``COM4@9600``
11+
Usage: use ``port or URL[@baurate]`` to open the device.
12+
For example use ``/dev/ttyUSB0@115200`` or ``COM4@9600`` for local serial ports and
13+
``socket://192.168.254.254:5000`` or ``rfc2217://192.168.254.254:5000`` for remote ports.
1214

1315
.. note:
1416
An Arduino-Interface could easily be build with this:

test/serial_test.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,9 @@ def reset(self):
3636
self.msg = None
3737

3838

39-
class SimpleSerialTest(unittest.TestCase):
39+
class SimpleSerialTestBase(object):
4040
MAX_TIMESTAMP = 0xFFFFFFFF / 1000
4141

42-
def setUp(self):
43-
self.patcher = patch('serial.Serial')
44-
self.mock_serial = self.patcher.start()
45-
self.serial_dummy = SerialDummy()
46-
self.mock_serial.return_value.write = self.serial_dummy.write
47-
self.mock_serial.return_value.read = self.serial_dummy.read
48-
self.addCleanup(self.patcher.stop)
49-
self.bus = SerialBus('bus')
50-
51-
def tearDown(self):
52-
self.serial_dummy.reset()
53-
5442
def test_rx_tx_min_max_data(self):
5543
"""
5644
Tests the transfer from 0x00 to 0xFF for a 1 byte payload
@@ -136,5 +124,29 @@ def test_rx_tx_min_timestamp_error(self):
136124
self.assertRaises(ValueError, self.bus.send, msg)
137125

138126

127+
class SimpleSerialTest(unittest.TestCase, SimpleSerialTestBase):
128+
129+
def setUp(self):
130+
self.patcher = patch('serial.Serial')
131+
self.mock_serial = self.patcher.start()
132+
self.serial_dummy = SerialDummy()
133+
self.mock_serial.return_value.write = self.serial_dummy.write
134+
self.mock_serial.return_value.read = self.serial_dummy.read
135+
self.addCleanup(self.patcher.stop)
136+
self.bus = SerialBus('bus')
137+
138+
def tearDown(self):
139+
self.serial_dummy.reset()
140+
141+
142+
class SimpleSerialLoopTest(unittest.TestCase, SimpleSerialTestBase):
143+
144+
def setUp(self):
145+
self.bus = SerialBus('loop://')
146+
147+
def tearDown(self):
148+
self.bus.shutdown()
149+
150+
139151
if __name__ == '__main__':
140152
unittest.main()

0 commit comments

Comments
 (0)