Skip to content

Commit bf541d9

Browse files
author
luoja
committed
add _read for get_version/get_serial_number
1 parent 11dbd6e commit bf541d9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

can/interfaces/slcan.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,31 @@ def fileno(self) -> int:
293293
except Exception as exception:
294294
raise CanOperationError("Cannot fetch fileno") from exception
295295

296+
def _read(self, timeout: Optional[float]) -> Optional[str]:
297+
_timeout = serial.Timeout(timeout)
298+
299+
with error_check("Could not read from serial device"):
300+
while True:
301+
# Due to accessing `serialPortOrig.in_waiting` too often will reduce the performance.
302+
# We read the `serialPortOrig.in_waiting` only once here.
303+
in_waiting = self.serialPortOrig.in_waiting
304+
for _ in range(max(1, in_waiting)):
305+
new_byte = self.serialPortOrig.read(size=1)
306+
if new_byte:
307+
self._buffer.extend(new_byte)
308+
else:
309+
break
310+
311+
if new_byte in (self._ERROR, self._OK):
312+
string = self._buffer.decode()
313+
self._buffer.clear()
314+
return string
315+
316+
if _timeout.expired():
317+
break
318+
319+
return None
320+
296321
def get_version(self, timeout: Optional[float]) -> Tuple[Optional[int], Optional[int]]:
297322
"""Get HW and SW version of the slcan interface.
298323

0 commit comments

Comments
 (0)