Skip to content
Open
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
21 changes: 11 additions & 10 deletions can/interfaces/slcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,17 @@ def _read(self, timeout: float | None) -> str | None:
# Due to accessing `serialPortOrig.in_waiting` too often will reduce the performance.
# We read the `serialPortOrig.in_waiting` only once here.
in_waiting = self.serialPortOrig.in_waiting
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in_waiting = self.serialPortOrig.in_waiting
size = self.serialPortOrig.in_waiting or 1

You could remove the if-else by always reading at least one.

for _ in range(max(1, in_waiting)):
new_byte = self.serialPortOrig.read(1)
if new_byte:
self._buffer.extend(new_byte)
else:
break

if new_byte in (self._ERROR, self._OK):
string = self._buffer.decode()
self._buffer.clear()
if in_waiting > 0:
self._buffer.extend(self.serialPortOrig.read(in_waiting))
else:
byte = self.serialPortOrig.read(1)
if byte:
self._buffer.extend(byte)

for i in range(len(self._buffer)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i in range(len(self._buffer)):
for i, byte in enumerate(self._buffer):

if self._buffer[i] in (self._OK[0], self._ERROR[0]):
string = self._buffer[: i + 1].decode()
del self._buffer[: i + 1]
return string

if _timeout.expired():
Expand Down