From 5320005072c384c8fcb58050a90896e9c8812d29 Mon Sep 17 00:00:00 2001 From: Felix Divo <4403130+felixdivo@users.noreply.github.com> Date: Wed, 27 Oct 2021 18:34:59 +0200 Subject: [PATCH 1/3] Remove duplicated documentation --- can/interfaces/slcan.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index 30f346697..99f754a42 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -1,10 +1,5 @@ """ Interface for slcan compatible interfaces (win32/linux). - -.. note:: - - Linux users can use slcand or socketcan as well. - """ from typing import Any, Optional, Tuple From 9366f0ac832cbaa5a18009a7cf6b6567da5fde5b Mon Sep 17 00:00:00 2001 From: Felix Divo <4403130+felixdivo@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:00:20 +0200 Subject: [PATCH 2/3] Cleanups and use new exceptions --- can/interfaces/slcan.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index 99f754a42..f3022cf89 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -3,13 +3,15 @@ """ 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,11 +64,11 @@ def __init__( """ :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: @@ -78,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.") @@ -115,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: @@ -215,6 +216,7 @@ def _recv_internal( dlc = int(string[9]) extended = True remote = True + if canId is not None: msg = Message( arbitration_id=canId, @@ -254,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] @@ -299,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) From 4ccf5ea9c6cd75b7dc43e6ccb83129cb49cf329a Mon Sep 17 00:00:00 2001 From: Felix Divo <4403130+felixdivo@users.noreply.github.com> Date: Wed, 27 Oct 2021 17:01:53 +0000 Subject: [PATCH 3/3] Format code with black --- can/interfaces/slcan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index f3022cf89..ac90a2600 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -59,7 +59,7 @@ 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