Skip to content

Commit 1296a1d

Browse files
committed
Add hardware handshake support for serial port based CAN interfaces
pySerial module provides 'rtscts' option to handle hardware handshake. This setting will be extracted from __init__'s kwargs. Example: bus = can.interface.Bus(bustype='slcan', channel='/dev/ttyUSB0@3000000', bitrate=1000000, rtscts=True)
1 parent c034773 commit 1296a1d

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

can/interfaces/serial/serial_can.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class SerialBus(BusABC):
3434
3535
"""
3636

37-
def __init__(self, channel, baudrate=115200, timeout=0.1, *args, **kwargs):
37+
def __init__(self, channel, baudrate=115200, timeout=0.1, rtscts=False,
38+
*args, **kwargs):
3839
"""
3940
:param str channel:
4041
The serial device to open. For example "/dev/ttyS1" or
@@ -49,13 +50,16 @@ def __init__(self, channel, baudrate=115200, timeout=0.1, *args, **kwargs):
4950
:param float timeout:
5051
Timeout for the serial device in seconds (default 0.1).
5152
53+
:param bool rtscts:
54+
turn hardware handshake (RTS/CTS) on and off
55+
5256
"""
5357
if not channel:
5458
raise ValueError("Must specify a serial port.")
5559

5660
self.channel_info = "Serial interface: " + channel
5761
self.ser = serial.serial_for_url(
58-
channel, baudrate=baudrate, timeout=timeout)
62+
channel, baudrate=baudrate, timeout=timeout, rtscts=rtscts)
5963

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

@@ -108,8 +112,8 @@ def _recv_internal(self, timeout):
108112
Read a message from the serial device.
109113
110114
:param timeout:
111-
112-
.. warning::
115+
116+
.. warning::
113117
This parameter will be ignored. The timeout value of the channel is used.
114118
115119
:returns:

can/interfaces/slcan.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from __future__ import absolute_import
1414

15-
import io
1615
import time
1716
import logging
1817

@@ -42,9 +41,10 @@ class slcanBus(BusABC):
4241
83300: 'S9'
4342
}
4443

45-
_SLEEP_AFTER_SERIAL_OPEN = 2 # in seconds
44+
_SLEEP_AFTER_SERIAL_OPEN = 2 # in seconds
4645

47-
def __init__(self, channel, ttyBaudrate=115200, timeout=1, bitrate=None, **kwargs):
46+
def __init__(self, channel, ttyBaudrate=115200, timeout=1, bitrate=None,
47+
rtscts=False, **kwargs):
4848
"""
4949
:param str channel:
5050
port of underlying serial or usb device (e.g. /dev/ttyUSB0, COM8, ...)
@@ -57,16 +57,18 @@ def __init__(self, channel, ttyBaudrate=115200, timeout=1, bitrate=None, **kwarg
5757
Poll interval in seconds when reading messages
5858
:param float timeout:
5959
timeout in seconds when reading message
60+
:param bool rtscts:
61+
turn hardware handshake (RTS/CTS) on and off
6062
"""
6163

62-
if not channel: # if None or empty
64+
if not channel: # if None or empty
6365
raise TypeError("Must specify a serial port.")
6466

6567
if '@' in channel:
6668
(channel, ttyBaudrate) = channel.split('@')
6769

6870
self.serialPortOrig = serial.serial_for_url(
69-
channel, baudrate=ttyBaudrate, timeout=timeout)
71+
channel, baudrate=ttyBaudrate, timeout=timeout, rtscts=rtscts)
7072

7173
time.sleep(self._SLEEP_AFTER_SERIAL_OPEN)
7274

@@ -80,7 +82,7 @@ def __init__(self, channel, ttyBaudrate=115200, timeout=1, bitrate=None, **kwarg
8082
self.open()
8183

8284
super(slcanBus, self).__init__(channel, ttyBaudrate=115200, timeout=1,
83-
bitrate=None, **kwargs)
85+
bitrate=None, rtscts=False, **kwargs)
8486

8587
def write(self, string):
8688
if not string.endswith('\r'):
@@ -104,7 +106,7 @@ def _recv_internal(self, timeout):
104106
frame = []
105107

106108
readStr = self.serialPortOrig.read_until(b'\r')
107-
109+
108110
if not readStr:
109111
return None, False
110112
else:

0 commit comments

Comments
 (0)