Skip to content

Commit cee2d78

Browse files
authored
Merge pull request #1155 from hardbyte/felixdivo-issue-1046-robotell
Specific Exceptions: Adapting robotell interface
2 parents 73057d8 + aa5dfd0 commit cee2d78

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

can/interfaces/robotell.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77

88
from can import BusABC, Message
9+
from ..exceptions import CanInterfaceNotImplementedError
910

1011
logger = logging.getLogger(__name__)
1112

@@ -56,15 +57,17 @@ def __init__(
5657
):
5758
"""
5859
:param str channel:
59-
port of underlying serial or usb device (e.g. /dev/ttyUSB0, COM8, ...)
60-
Must not be empty.
60+
port of underlying serial or usb device (e.g. ``/dev/ttyUSB0``, ``COM8``, ...)
61+
Must not be empty. Can also end with ``@115200`` (or similarly) to specify the baudrate.
6162
:param int ttyBaudrate:
62-
baudrate of underlying serial or usb device
63+
baudrate of underlying serial or usb device (Ignored if set via the ``channel`` parameter)
6364
:param int bitrate:
6465
CAN Bitrate in bit/s. Value is stored in the adapter and will be used as default if no bitrate is specified
6566
:param bool rtscts:
6667
turn hardware handshake (RTS/CTS) on and off
6768
"""
69+
if serial is None:
70+
raise CanInterfaceNotImplementedError("The serial module is not installed")
6871

6972
if not channel: # if None or empty
7073
raise TypeError("Must specify a serial port.")
@@ -74,7 +77,7 @@ def __init__(
7477
channel, baudrate=ttyBaudrate, rtscts=rtscts
7578
)
7679

77-
## Disable flushing queued config ACKs on lookup channel (for unit tests)
80+
# Disable flushing queued config ACKs on lookup channel (for unit tests)
7881
self._loopback_test = channel == "loop://"
7982

8083
self._rxbuffer = bytearray() # raw bytes from the serial port
@@ -86,11 +89,10 @@ def __init__(
8689
if bitrate is not None:
8790
self.set_bitrate(bitrate)
8891

89-
self.channel_info = "Robotell USB-CAN s/n %s on %s" % (
90-
self.get_serial_number(1),
91-
channel,
92+
self.channel_info = (
93+
f"Robotell USB-CAN s/n {self.get_serial_number(1)} on {channel}"
9294
)
93-
logger.info("Using device: {}".format(self.channel_info))
95+
logger.info("Using device: %s", self.channel_info)
9496

9597
super().__init__(channel=channel, **kwargs)
9698

@@ -103,9 +105,7 @@ def set_bitrate(self, bitrate):
103105
if bitrate <= self._MAX_CAN_BAUD:
104106
self._writeconfig(self._CAN_BAUD_ID, bitrate)
105107
else:
106-
raise ValueError(
107-
"Invalid bitrate, must be less than " + str(self._MAX_CAN_BAUD)
108-
)
108+
raise ValueError(f"Invalid bitrate, must be less than {self._MAX_CAN_BAUD}")
109109

110110
def set_auto_retransmit(self, retrans_flag):
111111
"""
@@ -119,8 +119,8 @@ def set_auto_bus_management(self, auto_man):
119119
:param bool auto_man:
120120
Enable/disable automatic bus management
121121
"""
122-
## Not sure what "automatic bus managemenet" does. Does not seem to control
123-
## automatic ACK of CAN frames (listen only mode)
122+
# Not sure what "automatic bus management" does. Does not seem to control
123+
# automatic ACK of CAN frames (listen only mode)
124124
self._writeconfig(self._CAN_ABOM_ID, 1 if auto_man else 0)
125125

126126
def set_serial_rate(self, serial_bps):
@@ -161,7 +161,7 @@ def _getconfigsize(self, configid):
161161
return 4
162162
if configid == self._CAN_READ_SERIAL1 or configid <= self._CAN_READ_SERIAL2:
163163
return 8
164-
if configid >= self._CAN_FILTER_BASE_ID and configid <= self._CAN_FILTER_MAX_ID:
164+
if self._CAN_FILTER_BASE_ID <= configid <= self._CAN_FILTER_MAX_ID:
165165
return 8
166166
return 0
167167

@@ -178,9 +178,7 @@ def _readconfig(self, configid, timeout):
178178
newmsg = self._readmessage(not self._loopback_test, True, timeout)
179179
if newmsg is None:
180180
logger.warning(
181-
"Timeout waiting for response when reading config value {:04X}.".format(
182-
configid
183-
)
181+
f"Timeout waiting for response when reading config value {configid:04X}."
184182
)
185183
return None
186184
return newmsg[4:12]
@@ -211,8 +209,7 @@ def _writeconfig(self, configid, value, value2=0):
211209
newmsg = self._readmessage(not self._loopback_test, True, 1)
212210
if newmsg is None:
213211
logger.warning(
214-
"Timeout waiting for response when writing config value "
215-
+ str(configid)
212+
"Timeout waiting for response when writing config value %d", configid
216213
)
217214

218215
def _readmessage(self, flushold, cfgchannel, timeout):
@@ -261,17 +258,16 @@ def _readmessage(self, flushold, cfgchannel, timeout):
261258
cs = (cs + newmsg[idx]) & 0xFF
262259
if newmsg[16] == cs:
263260
# OK, valid message - place it in the correct queue
264-
if newmsg[13] == 0xFF: ## Check for config channel
261+
if newmsg[13] == 0xFF: # Check for config channel
265262
self._configmsg.append(newmsg)
266263
else:
267264
self._rxmsg.append(newmsg)
268265
else:
269266
logger.warning("Incorrect message checksum, discarded message")
270267
else:
271268
logger.warning(
272-
"Invalid message structure length "
273-
+ str(len(newmsg))
274-
+ ", ignoring message"
269+
"Invalid message structure length %d, ignoring message",
270+
len(newmsg),
275271
)
276272

277273
# Check if we have a message in the desired queue - if so copy and return

0 commit comments

Comments
 (0)