6
6
import logging
7
7
8
8
from can import BusABC , Message
9
+ from ..exceptions import CanInterfaceNotImplementedError
9
10
10
11
logger = logging .getLogger (__name__ )
11
12
@@ -56,15 +57,17 @@ def __init__(
56
57
):
57
58
"""
58
59
: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.
61
62
: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)
63
64
:param int bitrate:
64
65
CAN Bitrate in bit/s. Value is stored in the adapter and will be used as default if no bitrate is specified
65
66
:param bool rtscts:
66
67
turn hardware handshake (RTS/CTS) on and off
67
68
"""
69
+ if serial is None :
70
+ raise CanInterfaceNotImplementedError ("The serial module is not installed" )
68
71
69
72
if not channel : # if None or empty
70
73
raise TypeError ("Must specify a serial port." )
@@ -74,7 +77,7 @@ def __init__(
74
77
channel , baudrate = ttyBaudrate , rtscts = rtscts
75
78
)
76
79
77
- ## Disable flushing queued config ACKs on lookup channel (for unit tests)
80
+ # Disable flushing queued config ACKs on lookup channel (for unit tests)
78
81
self ._loopback_test = channel == "loop://"
79
82
80
83
self ._rxbuffer = bytearray () # raw bytes from the serial port
@@ -86,11 +89,10 @@ def __init__(
86
89
if bitrate is not None :
87
90
self .set_bitrate (bitrate )
88
91
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 } "
92
94
)
93
- logger .info ("Using device: {}" . format ( self .channel_info ) )
95
+ logger .info ("Using device: %s" , self .channel_info )
94
96
95
97
super ().__init__ (channel = channel , ** kwargs )
96
98
@@ -103,9 +105,7 @@ def set_bitrate(self, bitrate):
103
105
if bitrate <= self ._MAX_CAN_BAUD :
104
106
self ._writeconfig (self ._CAN_BAUD_ID , bitrate )
105
107
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 } " )
109
109
110
110
def set_auto_retransmit (self , retrans_flag ):
111
111
"""
@@ -119,8 +119,8 @@ def set_auto_bus_management(self, auto_man):
119
119
:param bool auto_man:
120
120
Enable/disable automatic bus management
121
121
"""
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)
124
124
self ._writeconfig (self ._CAN_ABOM_ID , 1 if auto_man else 0 )
125
125
126
126
def set_serial_rate (self , serial_bps ):
@@ -161,7 +161,7 @@ def _getconfigsize(self, configid):
161
161
return 4
162
162
if configid == self ._CAN_READ_SERIAL1 or configid <= self ._CAN_READ_SERIAL2 :
163
163
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 :
165
165
return 8
166
166
return 0
167
167
@@ -178,9 +178,7 @@ def _readconfig(self, configid, timeout):
178
178
newmsg = self ._readmessage (not self ._loopback_test , True , timeout )
179
179
if newmsg is None :
180
180
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} ."
184
182
)
185
183
return None
186
184
return newmsg [4 :12 ]
@@ -211,8 +209,7 @@ def _writeconfig(self, configid, value, value2=0):
211
209
newmsg = self ._readmessage (not self ._loopback_test , True , 1 )
212
210
if newmsg is None :
213
211
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
216
213
)
217
214
218
215
def _readmessage (self , flushold , cfgchannel , timeout ):
@@ -261,17 +258,16 @@ def _readmessage(self, flushold, cfgchannel, timeout):
261
258
cs = (cs + newmsg [idx ]) & 0xFF
262
259
if newmsg [16 ] == cs :
263
260
# 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
265
262
self ._configmsg .append (newmsg )
266
263
else :
267
264
self ._rxmsg .append (newmsg )
268
265
else :
269
266
logger .warning ("Incorrect message checksum, discarded message" )
270
267
else :
271
268
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 ),
275
271
)
276
272
277
273
# Check if we have a message in the desired queue - if so copy and return
0 commit comments