Skip to content
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
3 changes: 2 additions & 1 deletion can/interfaces/serial/serial_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def __init__(self, channel, baudrate=115200, timeout=0.1, *args, **kwargs):
raise ValueError("Must specify a serial port.")

self.channel_info = "Serial interface: " + channel
self.ser = serial.Serial(channel, baudrate=baudrate, timeout=timeout)
self.ser = serial.serial_for_url(
channel, baudrate=baudrate, timeout=timeout)

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

Expand Down
3 changes: 2 additions & 1 deletion can/interfaces/slcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def __init__(self, channel, ttyBaudrate=115200, timeout=1, bitrate=None, **kwarg
if '@' in channel:
(channel, ttyBaudrate) = channel.split('@')

self.serialPortOrig = serial.Serial(channel, baudrate=ttyBaudrate, timeout=timeout)
self.serialPortOrig = serial.serial_for_url(
channel, baudrate=ttyBaudrate, timeout=timeout)

time.sleep(self._SLEEP_AFTER_SERIAL_OPEN)

Expand Down
4 changes: 4 additions & 0 deletions doc/interfaces/serial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ CAN over Serial
===============
A text based interface. For example use over serial ports like
``/dev/ttyS1`` or ``/dev/ttyUSB0`` on Linux machines or ``COM1`` on Windows.
Remote ports can be also used via a special URL. Both raw TCP sockets as
also RFC2217 ports are supported: ``socket://192.168.254.254:5000`` or
``rfc2217://192.168.254.254:5000``. In addition a virtual loopback can be
used via ``loop://`` URL.
The interface is a simple implementation that has been used for
recording CAN traces.

Expand Down
8 changes: 5 additions & 3 deletions doc/interfaces/slcan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ CAN over Serial / SLCAN

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

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

.. note:
An Arduino-Interface could easily be build with this:
Expand Down
38 changes: 25 additions & 13 deletions test/serial_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,9 @@ def reset(self):
self.msg = None


class SimpleSerialTest(unittest.TestCase):
class SimpleSerialTestBase(object):
MAX_TIMESTAMP = 0xFFFFFFFF / 1000

def setUp(self):
self.patcher = patch('serial.Serial')
self.mock_serial = self.patcher.start()
self.serial_dummy = SerialDummy()
self.mock_serial.return_value.write = self.serial_dummy.write
self.mock_serial.return_value.read = self.serial_dummy.read
self.addCleanup(self.patcher.stop)
self.bus = SerialBus('bus')

def tearDown(self):
self.serial_dummy.reset()

def test_rx_tx_min_max_data(self):
"""
Tests the transfer from 0x00 to 0xFF for a 1 byte payload
Expand Down Expand Up @@ -136,5 +124,29 @@ def test_rx_tx_min_timestamp_error(self):
self.assertRaises(ValueError, self.bus.send, msg)


class SimpleSerialTest(unittest.TestCase, SimpleSerialTestBase):

def setUp(self):
self.patcher = patch('serial.Serial')
self.mock_serial = self.patcher.start()
self.serial_dummy = SerialDummy()
self.mock_serial.return_value.write = self.serial_dummy.write
self.mock_serial.return_value.read = self.serial_dummy.read
self.addCleanup(self.patcher.stop)
self.bus = SerialBus('bus')

def tearDown(self):
self.serial_dummy.reset()


class SimpleSerialLoopTest(unittest.TestCase, SimpleSerialTestBase):

def setUp(self):
self.bus = SerialBus('loop://')

def tearDown(self):
self.bus.shutdown()


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