diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index 30f346697..ac90a2600 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -1,20 +1,17 @@ """ Interface for slcan compatible interfaces (win32/linux). - -.. note:: - - Linux users can use slcand or socketcan as well. - """ from typing import Any, Optional, Tuple -from can import typechecking import io import time import logging from can import BusABC, Message +from ..exceptions import CanInterfaceNotImplementedError, CanOperationError +from can import typechecking + logger = logging.getLogger(__name__) @@ -62,16 +59,16 @@ def __init__( btr: Optional[str] = None, sleep_after_open: float = _SLEEP_AFTER_SERIAL_OPEN, rtscts: bool = False, - **kwargs: Any + **kwargs: Any, ) -> None: """ :raise ValueError: if both *bitrate* and *btr* are set - :param channel: - port of underlying serial or usb device (e.g. /dev/ttyUSB0, COM8, ...) - Must not be empty. - :param ttyBaudrate: - baudrate of underlying serial or usb device + :param str channel: + port of underlying serial or usb device (e.g. ``/dev/ttyUSB0``, ``COM8``, ...) + Must not be empty. Can also end with ``@115200`` (or similarly) to specify the baudrate. + :param int ttyBaudrate: + baudrate of underlying serial or usb device (Ignored if set via the ``channel`` parameter) :param bitrate: Bitrate in bit/s :param btr: @@ -83,6 +80,8 @@ def __init__( :param rtscts: turn hardware handshake (RTS/CTS) on and off """ + if serial is None: + raise CanInterfaceNotImplementedError("The serial module is not installed") if not channel: # if None or empty raise TypeError("Must specify a serial port.") @@ -120,11 +119,8 @@ def set_bitrate(self, bitrate: int) -> None: if bitrate in self._BITRATES: self._write(self._BITRATES[bitrate]) else: - raise ValueError( - "Invalid bitrate, choose one of " - + (", ".join(str(k) for k in self._BITRATES.keys())) - + "." - ) + bitrates = ", ".join(str(k) for k in self._BITRATES.keys()) + raise ValueError(f"Invalid bitrate, choose one of {bitrates}.") self.open() def set_bitrate_reg(self, btr: str) -> None: @@ -220,6 +216,7 @@ def _recv_internal( dlc = int(string[9]) extended = True remote = True + if canId is not None: msg = Message( arbitration_id=canId, @@ -259,6 +256,8 @@ def fileno(self) -> int: raise NotImplementedError( "fileno is not implemented using current CAN bus on this platform" ) + except Exception as exception: + raise CanOperationError("Cannot fetch fileno") from exception def get_version( self, timeout: Optional[float] @@ -304,10 +303,10 @@ def get_serial_number(self, timeout: Optional[float]) -> Optional[str]: """Get serial number of the slcan interface. :param timeout: - seconds to wait for serial number or None to wait indefinitely + seconds to wait for serial number or ``None`` to wait indefinitely :return: - None on timeout or a str object. + ``None`` on timeout or a :class:`~builtin.str` object. """ cmd = "N" self._write(cmd)