Skip to content

Commit 167c161

Browse files
committed
SerialHandler: Raise exceptions on unrecoverable errors
1 parent fcdc51a commit 167c161

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

pslab/serial_handler.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _check_udev():
8383
raise OSError(e)
8484

8585
@staticmethod
86-
def _list_ports() -> List[str]: # Promote to public?
86+
def _list_ports() -> List[str]:
8787
"""Return a list of serial port names."""
8888
return [p.device for p in list_ports.comports()]
8989

@@ -196,26 +196,31 @@ def get_version(self) -> str:
196196
self._write_log(version, "RX")
197197
return version.decode("utf-8")
198198

199-
def get_ack(self) -> int: # Make _internal?
199+
def get_ack(self) -> int:
200200
"""Get response code from PSLab.
201201
202-
Also functions as handshake.
203-
204202
Returns
205203
-------
206204
int
207205
Response code. Meanings:
208-
1 SUCCESS
209-
2 ARGUMENT_ERROR
210-
3 FAILED
206+
0x01 ACK
207+
0x10 I2C ACK
208+
0x20 I2C bus collision
209+
0x10 Radio max retransmits
210+
0x20 Radio not present
211+
0x40 Radio reply timout
211212
"""
212213
response = self.read(1)
213214

214-
try:
215-
return CP.Byte.unpack(response)[0]
216-
except Exception as e:
217-
logger.error(e)
218-
return 3 # raise exception instead?
215+
if not response:
216+
raise serial.SerialException("Timeout while waiting for ACK.")
217+
218+
ack = CP.Byte.unpack(response)[0]
219+
220+
if not (ack & 0x01):
221+
raise serial.SerialException("Received non ACK byte while waiting for ACK.")
222+
223+
return ack
219224

220225
@staticmethod
221226
def _get_integer_type(size: int) -> struct.Struct:
@@ -250,16 +255,21 @@ def _receive(self, size: int) -> int:
250255
Returns
251256
-------
252257
int
253-
Unpacked data, or -1 if too few bytes were read.
258+
Unpacked data.
259+
260+
Raises
261+
------
262+
SerialException if too few bytes received.
254263
"""
255264
received = self.read(size)
256265

257266
if len(received) == size:
258267
unpacker = self._get_integer_type(size)
259268
retval = unpacker.unpack(received)[0]
260269
else:
261-
logger.error(f"Requested {size} bytes, got {len(received)}.")
262-
retval = -1 # raise an exception instead?
270+
raise serial.SerialException(
271+
f"Requested {size} bytes, got {len(received)}."
272+
)
263273

264274
return retval
265275

tests/recordings/i2c/test_scan.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[[4], [14], [4], [9], [148, 0], [4], [16], [4], [1], [3], [4], [3], [4], [1], [5], [4], [3], [4], [1], [7], [4], [3], [4], [1], [9], [4], [3], [4], [1], [11], [4], [3], [4], [1], [13], [4], [3], [4], [1], [15], [4], [3], [4], [1], [17], [4], [3], [4], [1], [19], [4], [3], [4], [1], [21], [4], [3], [4], [1], [23], [4], [3], [4], [1], [25], [4], [3], [4], [1], [27], [4], [3], [4], [1], [29], [4], [3], [4], [1], [31], [4], [3], [4], [1], [33], [4], [3], [4], [1], [35], [4], [3], [4], [1], [37], [4], [3], [4], [1], [39], [4], [3], [4], [1], [41], [4], [3], [4], [1], [43], [4], [3], [4], [1], [45], [4], [3], [4], [1], [47], [4], [3], [4], [1], [49], [4], [3], [4], [1], [51], [4], [3], [4], [1], [53], [4], [3], [4], [1], [55], [4], [3], [4], [1], [57], [4], [3], [4], [1], [59], [4], [3], [4], [1], [61], [4], [3], [4], [1], [63], [4], [3], [4], [1], [65], [4], [3], [4], [1], [67], [4], [3], [4], [1], [69], [4], [3], [4], [1], [71], [4], [3], [4], [1], [73], [4], [3], [4], [1], [75], [4], [3], [4], [1], [77], [4], [3], [4], [1], [79], [4], [3], [4], [1], [81], [4], [3], [4], [1], [83], [4], [3], [4], [1], [85], [4], [3], [4], [1], [87], [4], [3], [4], [1], [89], [4], [3], [4], [1], [91], [4], [3], [4], [1], [93], [4], [3], [4], [1], [95], [4], [3], [4], [1], [97], [4], [3], [4], [1], [99], [4], [3], [4], [1], [101], [4], [3], [4], [1], [103], [4], [3], [4], [1], [105], [4], [3], [4], [1], [107], [4], [3], [4], [1], [109], [4], [3], [4], [1], [111], [4], [3], [4], [1], [113], [4], [3], [4], [1], [115], [4], [3], [4], [1], [117], [4], [3], [4], [1], [119], [4], [3], [4], [1], [121], [4], [3], [4], [1], [123], [4], [3], [4], [1], [125], [4], [3], [4], [1], [127], [4], [3], [4], [1], [129], [4], [3], [4], [1], [131], [4], [3], [4], [1], [133], [4], [3], [4], [1], [135], [4], [3], [4], [1], [137], [4], [3], [4], [1], [139], [4], [3], [4], [1], [141], [4], [3], [4], [1], [143], [4], [3], [4], [1], [145], [4], [3], [4], [1], [147], [4], [3], [4], [1], [149], [4], [3], [4], [1], [151], [4], [3], [4], [1], [153], [4], [3], [4], [1], [155], [4], [3], [4], [1], [157], [4], [3], [4], [1], [159], [4], [3], [4], [1], [161], [4], [3], [4], [1], [163], [4], [3], [4], [1], [165], [4], [3], [4], [1], [167], [4], [3], [4], [1], [169], [4], [3], [4], [1], [171], [4], [3], [4], [1], [173], [4], [3], [4], [1], [175], [4], [3], [4], [1], [177], [4], [3], [4], [1], [179], [4], [3], [4], [1], [181], [4], [3], [4], [1], [183], [4], [3], [4], [1], [185], [4], [3], [4], [1], [187], [4], [3], [4], [1], [189], [4], [3], [4], [1], [191], [4], [3], [4], [1], [193], [4], [3], [4], [1], [195], [4], [3], [4], [1], [197], [4], [3], [4], [1], [199], [4], [3], [4], [1], [201], [4], [3], [4], [1], [203], [4], [3], [4], [1], [205], [4], [3], [4], [1], [207], [4], [3], [4], [1], [209], [4], [3], [4], [1], [211], [4], [3], [4], [1], [213], [4], [3], [4], [1], [215], [4], [3], [4], [1], [217], [4], [3], [4], [1], [219], [4], [3], [4], [1], [221], [4], [3], [4], [1], [223], [4], [3], [4], [1], [225], [4], [3], [4], [1], [227], [4], [3], [4], [1], [229], [4], [3], [4], [1], [231], [4], [3], [4], [1], [233], [4], [3], [4], [1], [235], [4], [3], [4], [1], [237], [4], [3], [4], [1], [239], [4], [3], [4], [1], [241], [4], [3], [4], [1], [243], [4], [3], [4], [1], [245], [4], [3], [4], [1], [247], [4], [3], [4], [1], [249], [4], [3], [4], [1], [251], [4], [3], [4], [1], [253], [4], [3], [4], [1], [255], [4], [3]], [[], [1], [], [], [1], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [1], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1]]]
1+
[[[4], [14], [4], [9], [244, 1], [4], [1], [3], [4], [3], [4], [1], [5], [4], [3], [4], [1], [7], [4], [3], [4], [1], [9], [4], [3], [4], [1], [11], [4], [3], [4], [1], [13], [4], [3], [4], [1], [15], [4], [3], [4], [1], [17], [4], [3], [4], [1], [19], [4], [3], [4], [1], [21], [4], [3], [4], [1], [23], [4], [3], [4], [1], [25], [4], [3], [4], [1], [27], [4], [3], [4], [1], [29], [4], [3], [4], [1], [31], [4], [3], [4], [1], [33], [4], [3], [4], [1], [35], [4], [3], [4], [1], [37], [4], [3], [4], [1], [39], [4], [3], [4], [1], [41], [4], [3], [4], [1], [43], [4], [3], [4], [1], [45], [4], [3], [4], [1], [47], [4], [3], [4], [1], [49], [4], [3], [4], [1], [51], [4], [3], [4], [1], [53], [4], [3], [4], [1], [55], [4], [3], [4], [1], [57], [4], [3], [4], [1], [59], [4], [3], [4], [1], [61], [4], [3], [4], [1], [63], [4], [3], [4], [1], [65], [4], [3], [4], [1], [67], [4], [3], [4], [1], [69], [4], [3], [4], [1], [71], [4], [3], [4], [1], [73], [4], [3], [4], [1], [75], [4], [3], [4], [1], [77], [4], [3], [4], [1], [79], [4], [3], [4], [1], [81], [4], [3], [4], [1], [83], [4], [3], [4], [1], [85], [4], [3], [4], [1], [87], [4], [3], [4], [1], [89], [4], [3], [4], [1], [91], [4], [3], [4], [1], [93], [4], [3], [4], [1], [95], [4], [3], [4], [1], [97], [4], [3], [4], [1], [99], [4], [3], [4], [1], [101], [4], [3], [4], [1], [103], [4], [3], [4], [1], [105], [4], [3], [4], [1], [107], [4], [3], [4], [1], [109], [4], [3], [4], [1], [111], [4], [3], [4], [1], [113], [4], [3], [4], [1], [115], [4], [3], [4], [1], [117], [4], [3], [4], [1], [119], [4], [3], [4], [1], [121], [4], [3], [4], [1], [123], [4], [3], [4], [1], [125], [4], [3], [4], [1], [127], [4], [3], [4], [1], [129], [4], [3], [4], [1], [131], [4], [3], [4], [1], [133], [4], [3], [4], [1], [135], [4], [3], [4], [1], [137], [4], [3], [4], [1], [139], [4], [3], [4], [1], [141], [4], [3], [4], [1], [143], [4], [3], [4], [1], [145], [4], [3], [4], [1], [147], [4], [3], [4], [1], [149], [4], [3], [4], [1], [151], [4], [3], [4], [1], [153], [4], [3], [4], [1], [155], [4], [3], [4], [1], [157], [4], [3], [4], [1], [159], [4], [3], [4], [1], [161], [4], [3], [4], [1], [163], [4], [3], [4], [1], [165], [4], [3], [4], [1], [167], [4], [3], [4], [1], [169], [4], [3], [4], [1], [171], [4], [3], [4], [1], [173], [4], [3], [4], [1], [175], [4], [3], [4], [1], [177], [4], [3], [4], [1], [179], [4], [3], [4], [1], [181], [4], [3], [4], [1], [183], [4], [3], [4], [1], [185], [4], [3], [4], [1], [187], [4], [3], [4], [1], [189], [4], [3], [4], [1], [191], [4], [3], [4], [1], [193], [4], [3], [4], [1], [195], [4], [3], [4], [1], [197], [4], [3], [4], [1], [199], [4], [3], [4], [1], [201], [4], [3], [4], [1], [203], [4], [3], [4], [1], [205], [4], [3], [4], [1], [207], [4], [3], [4], [1], [209], [4], [3], [4], [1], [211], [4], [3], [4], [1], [213], [4], [3], [4], [1], [215], [4], [3], [4], [1], [217], [4], [3], [4], [1], [219], [4], [3], [4], [1], [221], [4], [3], [4], [1], [223], [4], [3], [4], [1], [225], [4], [3], [4], [1], [227], [4], [3], [4], [1], [229], [4], [3], [4], [1], [231], [4], [3], [4], [1], [233], [4], [3], [4], [1], [235], [4], [3], [4], [1], [237], [4], [3], [4], [1], [239], [4], [3], [4], [1], [241], [4], [3], [4], [1], [243], [4], [3], [4], [1], [245], [4], [3], [4], [1], [247], [4], [3], [4], [1], [249], [4], [3], [4], [1], [251], [4], [3], [4], [1], [253], [4], [3], [4], [1], [255], [4], [3]], [[], [1], [], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [1], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1], [], [], [17], [], [1]]]

tests/test_serial_handler.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def test_get_ack_success(mock_serial, mock_handler):
7474

7575

7676
def test_get_ack_failure(mock_serial, mock_handler):
77-
error = 3
7877
mock_serial().read.return_value = b""
79-
assert mock_handler.get_ack() == error
78+
with pytest.raises(SerialException):
79+
mock_handler.get_ack()
8080

8181

8282
def test_send_bytes(mock_serial, mock_handler):
@@ -98,9 +98,8 @@ def test_receive(mock_serial, mock_handler):
9898

9999
def test_receive_failure(mock_serial, mock_handler):
100100
mock_serial().read.return_value = b""
101-
r = mock_handler.get_byte()
102-
mock_serial().read.assert_called_with(1)
103-
assert r == -1
101+
with pytest.raises(SerialException):
102+
mock_handler.get_byte()
104103

105104

106105
def test_wait_for_data(mock_serial, mock_handler):

0 commit comments

Comments
 (0)