-
Notifications
You must be signed in to change notification settings - Fork 633
Improve slcan.py #1490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improve slcan.py #1490
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2c9e1e6
Improve slcan.py
mikisama f95ca7d
Merge branch 'hardbyte:develop' into develop
mikisama 7fa9f5c
use `read_all` to read out serial port data.
mikisama c581cc9
fix when the `timeout` parameter is 0, it cannot enter the while loop.
mikisama 8722a11
For performance reason, revert to using `read` to read serial data.
mikisama fa62a63
add `size=1` and renamed `new_data` to `new_byte`
mikisama 73ce3dd
fix the issue of returning `None` before receiving
mikisama 3eb80b9
Due to the simplification of the control flow,
mikisama 2bbee49
Revert "Due to the simplification of the control flow,"
mikisama 1635381
Simplify the control flow for finding the end of a SLCAN message.
mikisama 42db106
improve the `timeout` handling of slcan.py
mikisama 6f994cc
Merge branch 'hardbyte:develop' into develop
mikisama 3c8a9e3
Change the plain format calls to f-strings.
mikisama ce3b9a4
using `bytearray.fromhex` to parse the frame's data.
mikisama 2c22b4e
change `del self._buffer[:]` to `self._buffer.clear` since it's more …
mikisama 09e1248
Simplify the timeout handling
mikisama e3a388b
fix the issue when the returned string is `None`.
mikisama 370a2ff
using `pyserial.reset_input_buffer` to discard
mikisama 68c9c71
fix failing PyPy test
mikisama cec01d7
fix failing PyPy test
mikisama 6c3b13a
improve the comments
mikisama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting the serial port timeout in the while loop slow down the RX performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to ensure, that the function respects the
timeout
parameter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
timeout
parameterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this proposed change I think there's still a scenario where the timeout is not enforced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
read_all
sounds good to me.The serial port must be instantiated with
timeout=0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tbruno25
read
method only copies it out. Only when the data bytes in the buffer are smaller than the parameter__size
, theread
method will blocking until the data bytes in the buffer is__size
, then copy it out.in_waiting
parameter shows the current number of bytes in the buffer, blocking will not occur. (The time to copy data out is negligibly small)MAX
receive buffer size in win10 is16384
, and ubuntu 20.04 is4095
. I'm not sure whether it can be changed. But I think there will not beinfinite
junk.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zariiii9003
Looks good. Just a few minor changes requested.
According to my test, the
read_all
method returnsb''
if there is no data that can be read out.So the
if new_data is not None:
should be changed toif new_data:
, otherwise it will never dotime.sleep
, which will cause high CPU usage.By the way, I did not initialize the
timeout
parameter ofserialPortOrig
at all and it defaults toNone
. It seems still works for me.I'm curious. If you don't set the
timeout
to 0, what will happen to you?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing, i don't have an interface to test this 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! And I agree -- I wasn't considering the physical limitations of the hardware/kernel. Good catch 🙂