Skip to content

Commit 7a69650

Browse files
committed
Cleanups and use new exceptions
1 parent 9319968 commit 7a69650

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

can/interfaces/slcan.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
"""
44

55
from typing import Any, Optional, Tuple
6-
from can import typechecking
76

87
import io
98
import time
109
import logging
1110

1211
from can import BusABC, Message
12+
from ..exceptions import CanInterfaceNotImplementedError, CanOperationError
13+
from can import typechecking
14+
1315

1416
logger = logging.getLogger(__name__)
1517

@@ -62,11 +64,11 @@ def __init__(
6264
"""
6365
:raise ValueError: if both *bitrate* and *btr* are set
6466
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)
7072
:param bitrate:
7173
Bitrate in bit/s
7274
:param btr:
@@ -78,6 +80,8 @@ def __init__(
7880
:param rtscts:
7981
turn hardware handshake (RTS/CTS) on and off
8082
"""
83+
if serial is None:
84+
raise CanInterfaceNotImplementedError("The serial module is not installed")
8185

8286
if not channel: # if None or empty
8387
raise TypeError("Must specify a serial port.")
@@ -115,11 +119,8 @@ def set_bitrate(self, bitrate: int) -> None:
115119
if bitrate in self._BITRATES:
116120
self._write(self._BITRATES[bitrate])
117121
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}.")
123124
self.open()
124125

125126
def set_bitrate_reg(self, btr: str) -> None:
@@ -215,6 +216,7 @@ def _recv_internal(
215216
dlc = int(string[9])
216217
extended = True
217218
remote = True
219+
218220
if canId is not None:
219221
msg = Message(
220222
arbitration_id=canId,
@@ -254,6 +256,8 @@ def fileno(self) -> int:
254256
raise NotImplementedError(
255257
"fileno is not implemented using current CAN bus on this platform"
256258
)
259+
except Exception as exception:
260+
raise CanOperationError("Cannot fetch fileno") from exception
257261

258262
def get_version(
259263
self, timeout: Optional[float]
@@ -299,10 +303,10 @@ def get_serial_number(self, timeout: Optional[float]) -> Optional[str]:
299303
"""Get serial number of the slcan interface.
300304
301305
: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
303307
304308
:return:
305-
None on timeout or a str object.
309+
``None`` on timeout or a :class:`~builtin.str` object.
306310
"""
307311
cmd = "N"
308312
self._write(cmd)

0 commit comments

Comments
 (0)