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
@@ -65,6 +66,8 @@ def __init__(
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
@@ -104,7 +107,7 @@ def set_bitrate(self, bitrate):
104
107
self ._writeconfig (self ._CAN_BAUD_ID , bitrate )
105
108
else :
106
109
raise ValueError (
107
- "Invalid bitrate, must be less than " + str ( self ._MAX_CAN_BAUD )
110
+ f "Invalid bitrate, must be less than { self ._MAX_CAN_BAUD } "
108
111
)
109
112
110
113
def set_auto_retransmit (self , retrans_flag ):
@@ -119,8 +122,8 @@ def set_auto_bus_management(self, auto_man):
119
122
:param bool auto_man:
120
123
Enable/disable automatic bus management
121
124
"""
122
- ## Not sure what "automatic bus managemenet " does. Does not seem to control
123
- ## automatic ACK of CAN frames (listen only mode)
125
+ # Not sure what "automatic bus management " does. Does not seem to control
126
+ # automatic ACK of CAN frames (listen only mode)
124
127
self ._writeconfig (self ._CAN_ABOM_ID , 1 if auto_man else 0 )
125
128
126
129
def set_serial_rate (self , serial_bps ):
@@ -161,7 +164,7 @@ def _getconfigsize(self, configid):
161
164
return 4
162
165
if configid == self ._CAN_READ_SERIAL1 or configid <= self ._CAN_READ_SERIAL2 :
163
166
return 8
164
- if configid >= self ._CAN_FILTER_BASE_ID and configid <= self ._CAN_FILTER_MAX_ID :
167
+ if self ._CAN_FILTER_BASE_ID <= configid <= self ._CAN_FILTER_MAX_ID :
165
168
return 8
166
169
return 0
167
170
@@ -178,9 +181,7 @@ def _readconfig(self, configid, timeout):
178
181
newmsg = self ._readmessage (not self ._loopback_test , True , timeout )
179
182
if newmsg is None :
180
183
logger .warning (
181
- "Timeout waiting for response when reading config value {:04X}." .format (
182
- configid
183
- )
184
+ f"Timeout waiting for response when reading config value { configid :04X} ."
184
185
)
185
186
return None
186
187
return newmsg [4 :12 ]
@@ -211,8 +212,8 @@ def _writeconfig(self, configid, value, value2=0):
211
212
newmsg = self ._readmessage (not self ._loopback_test , True , 1 )
212
213
if newmsg is None :
213
214
logger .warning (
214
- "Timeout waiting for response when writing config value "
215
- + str ( configid )
215
+ "Timeout waiting for response when writing config value %d" ,
216
+ configid
216
217
)
217
218
218
219
def _readmessage (self , flushold , cfgchannel , timeout ):
@@ -261,17 +262,16 @@ def _readmessage(self, flushold, cfgchannel, timeout):
261
262
cs = (cs + newmsg [idx ]) & 0xFF
262
263
if newmsg [16 ] == cs :
263
264
# OK, valid message - place it in the correct queue
264
- if newmsg [13 ] == 0xFF : ## Check for config channel
265
+ if newmsg [13 ] == 0xFF : # Check for config channel
265
266
self ._configmsg .append (newmsg )
266
267
else :
267
268
self ._rxmsg .append (newmsg )
268
269
else :
269
270
logger .warning ("Incorrect message checksum, discarded message" )
270
271
else :
271
272
logger .warning (
272
- "Invalid message structure length "
273
- + str (len (newmsg ))
274
- + ", ignoring message"
273
+ "Invalid message structure length %d, ignoring message" ,
274
+ len (newmsg )
275
275
)
276
276
277
277
# Check if we have a message in the desired queue - if so copy and return
0 commit comments