3
3
"""
4
4
5
5
from typing import Any , Optional , Tuple
6
- from can import typechecking
7
6
8
7
import io
9
8
import time
10
9
import logging
11
10
12
11
from can import BusABC , Message
12
+ from ..exceptions import CanInterfaceNotImplementedError , CanOperationError
13
+ from can import typechecking
14
+
13
15
14
16
logger = logging .getLogger (__name__ )
15
17
@@ -62,11 +64,11 @@ def __init__(
62
64
"""
63
65
:raise ValueError: if both *bitrate* and *btr* are set
64
66
65
- :param channel:
66
- port of underlying serial or usb device (e.g. /dev/ttyUSB0, COM8, ...)
67
- Must not be empty.
68
- :param ttyBaudrate:
69
- baudrate of underlying serial or usb device
67
+ :param str channel:
68
+ port of underlying serial or usb device (e.g. `` /dev/ttyUSB0``, `` COM8`` , ...)
69
+ Must not be empty. Can also end with ``@115200`` (or similarly) to specify the baudrate.
70
+ :param int ttyBaudrate:
71
+ baudrate of underlying serial or usb device (Ignored if set via the ``channel`` parameter)
70
72
:param bitrate:
71
73
Bitrate in bit/s
72
74
:param btr:
@@ -78,6 +80,8 @@ def __init__(
78
80
:param rtscts:
79
81
turn hardware handshake (RTS/CTS) on and off
80
82
"""
83
+ if serial is None :
84
+ raise CanInterfaceNotImplementedError ("The serial module is not installed" )
81
85
82
86
if not channel : # if None or empty
83
87
raise TypeError ("Must specify a serial port." )
@@ -115,11 +119,8 @@ def set_bitrate(self, bitrate: int) -> None:
115
119
if bitrate in self ._BITRATES :
116
120
self ._write (self ._BITRATES [bitrate ])
117
121
else :
118
- raise ValueError (
119
- "Invalid bitrate, choose one of "
120
- + (", " .join (str (k ) for k in self ._BITRATES .keys ()))
121
- + "."
122
- )
122
+ bitrates = ", " .join (str (k ) for k in self ._BITRATES .keys ())
123
+ raise ValueError (f"Invalid bitrate, choose one of { bitrates } ." )
123
124
self .open ()
124
125
125
126
def set_bitrate_reg (self , btr : str ) -> None :
@@ -215,6 +216,7 @@ def _recv_internal(
215
216
dlc = int (string [9 ])
216
217
extended = True
217
218
remote = True
219
+
218
220
if canId is not None :
219
221
msg = Message (
220
222
arbitration_id = canId ,
@@ -254,6 +256,8 @@ def fileno(self) -> int:
254
256
raise NotImplementedError (
255
257
"fileno is not implemented using current CAN bus on this platform"
256
258
)
259
+ except Exception as exception :
260
+ raise CanOperationError ("Cannot fetch fileno" ) from exception
257
261
258
262
def get_version (
259
263
self , timeout : Optional [float ]
@@ -299,10 +303,10 @@ def get_serial_number(self, timeout: Optional[float]) -> Optional[str]:
299
303
"""Get serial number of the slcan interface.
300
304
301
305
:param timeout:
302
- seconds to wait for serial number or None to wait indefinitely
306
+ seconds to wait for serial number or `` None`` to wait indefinitely
303
307
304
308
:return:
305
- None on timeout or a str object.
309
+ `` None`` on timeout or a :class:`~builtin. str` object.
306
310
"""
307
311
cmd = "N"
308
312
self ._write (cmd )
0 commit comments