11
11
import struct
12
12
from typing import Any , List , Tuple , Optional
13
13
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
+ )
15
21
from can .typechecking import AutoDetectedConfig
16
22
17
23
logger = logging .getLogger ("can.serial" )
@@ -67,14 +73,26 @@ def __init__(
67
73
:param rtscts:
68
74
turn hardware handshake (RTS/CTS) on and off
69
75
76
+ :raises can.CanInitializationError: If the given parameters are invalid.
77
+ :raises can.CanInterfaceNotImplementedError: If the serial module is not installed.
70
78
"""
79
+
80
+ if not serial :
81
+ raise CanInterfaceNotImplementedError ("the serial module is not installed" )
82
+
71
83
if not channel :
72
- raise ValueError ("Must specify a serial port." )
84
+ raise TypeError ("Must specify a serial port." )
73
85
74
86
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
78
96
79
97
super ().__init__ (channel , * args , ** kwargs )
80
98
@@ -124,7 +142,12 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:
124
142
byte_msg .append (0xBB )
125
143
126
144
# 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
128
151
129
152
def _recv_internal (
130
153
self , timeout : Optional [float ]
@@ -153,12 +176,12 @@ def _recv_internal(
153
176
timestamp = struct .unpack ("<I" , s )[0 ]
154
177
dlc = ord (self ._ser .read ())
155
178
if dlc > 8 :
156
- raise CanError ("received DLC may not exceed 8 bytes" )
179
+ raise ValueError ("received DLC may not exceed 8 bytes" )
157
180
158
181
s = self ._ser .read (4 )
159
182
arbitration_id = struct .unpack ("<I" , s )[0 ]
160
183
if arbitration_id >= 0x20000000 :
161
- raise CanError (
184
+ raise ValueError (
162
185
"received arbitration id may not exceed 2^29 (0x20000000)"
163
186
)
164
187
9A3E
@@ -177,15 +200,15 @@ def _recv_internal(
177
200
return msg , False
178
201
179
202
else :
180
- raise CanError (
203
+ raise CanOperationError (
181
204
f"invalid delimiter byte while reading message: { delimiter_byte } "
182
205
)
183
206
184
207
else :
185
208
return None , False
186
209
187
210
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
189
212
190
213
def fileno (self ) -> int :
191
214
if hasattr (self ._ser , "fileno" ):
0 commit comments