8000 Specific Exceptions: Adapting serial interface (#1088) · projectgus/python-can@872486a · GitHub
[go: up one dir, main page]

Skip to content

Commit 872486a

Browse files
authored
Specific Exceptions: Adapting serial interface (hardbyte#1088)
* Adjust exceptions in serial * Format code with black
1 parent fcbef0e commit 872486a

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

can/interfaces/serial/serial_can.py

Lines changed: 33 additions & 10 deletions
9A3E
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
import struct
1212
from typing import Any, List, Tuple, Optional
1313

14-
from can import BusABC, Message, CanError
14+
from can import BusABC, Message
15+
from can import (
16+
CanInterfaceNotImplementedError,
17+
CanInitializationError,
18+
CanOperationError,
19+
CanTimeoutError,
20+
)
1521
from can.typechecking import AutoDetectedConfig
1622

1723
logger = logging.getLogger("can.serial")
@@ -67,14 +73,26 @@ def __init__(
6773
:param rtscts:
6874
turn hardware handshake (RTS/CTS) on and off
6975
76+
:raises can.CanInitializationError: If the given parameters are invalid.
77+
:raises can.CanInterfaceNotImplementedError: If the serial module is not installed.
7078
"""
79+
80+
if not serial:
81+
raise CanInterfaceNotImplementedError("the serial module is not installed")
82+
7183
if not channel:
72-
raise ValueError("Must specify a serial port.")
84+
raise TypeError("Must specify a serial port.")
7385

7486
self.channel_info = f"Serial interface: {channel}"
75-
self._ser = serial.serial_for_url(
76-
channel, baudrate=baudrate, timeout=timeout, rtscts=rtscts
77-
)
87+
88+
try:
89+
self._ser = serial.serial_for_url(
90+
channel, baudrate=baudrate, timeout=timeout, rtscts=rtscts
91+
)
92+
except ValueError as error:
93+
raise CanInitializationError(
94+
"could not create the serial device"
95+
) from error
7896

7997
super().__init__(channel, *args, **kwargs)
8098

@@ -124,7 +142,12 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:
124142
byte_msg.append(0xBB)
125143

126144
# Write to serial device
127-
self._ser.write(byte_msg)
145+
try:
146+
self._ser.write(byte_msg)
147+
except serial.PortNotOpenError as error:
148+
raise CanOperationError("writing to closed port") from error
149+
except serial.SerialTimeoutException as error:
150+
raise CanTimeoutError() from error
128151

129152
def _recv_internal(
130153
self, timeout: Optional[float]
@@ -153,12 +176,12 @@ def _recv_internal(
153176
timestamp = struct.unpack("<I", s)[0]
154177
dlc = ord(self._ser.read())
155178
if dlc > 8:
156-
raise CanError("received DLC may not exceed 8 bytes")
179+
raise ValueError("received DLC may not exceed 8 bytes")
157180

158181
s = self._ser.read(4)
159182
arbitration_id = struct.unpack("<I", s)[0]
160183
if arbitration_id >= 0x20000000:
161-
raise CanError(
184+
raise ValueError(
162185
"received arbitration id may not exceed 2^29 (0x20000000)"
163186
)
164187

@@ -177,15 +200,15 @@ def _recv_internal(
177200
return msg, False
178201

179202
else:
180-
raise CanError(
203+
raise CanOperationError(
181204
f"invalid delimiter byte while reading message: {delimiter_byte}"
182205
)
183206

184207
else:
185208
return None, False
186209

187210
except serial.SerialException as error:
188-
raise CanError("could not read from serial") from error
211+
raise CanOperationError("could not read from serial") from error
189212

190213
def fileno(self) -> int:
191214
if hasattr(self._ser, "fileno"):

0 commit comments

Comments
 (0)
0