Skip to content

Specific Exceptions: Adapting slcan interface #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions can/interfaces/slcan.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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:
Expand All @@ -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.")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down