Pyserial Write Timeout
Join GitHub today
- Mar 27, 2017 It allows you to read lines without 100% CPU. It does not contain any timeout logic. If a timeout occurs, self.s.read(i) returns an empty string and you might want to throw an exception to indicate the timeout. I just measured. The code below gives me 790 kB/sec while replacing the code with pyserial's readline method gives me just 170kB/sec.
- I have used pyserial for a couple of days. However, a problem occurs today. I met serial write timeout. Several days before, when I used a switch, everything is OK. But today I changed for another switch. Then serial write timeout appears. I did not change any code, but the problem is actually quite severe.
Welcome to pySerial’s documentation¶. This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named “serial” automatically selects the appropriate backend. SerialTimeoutException – In case a write timeout is configured for the port and the time is exceeded. Write the bytes data to the port.
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upHave 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
Comments
commented Mar 27, 2017
I am using pyserial on RPi to read data from sensor. The code basically looks like this: However, it comsumes almost 100% of the cpu. I also run the program on a Windows laptop which is more powerful, the cpu usage is about 12% and the reading speed on RPi is slower than the speed on my laptop. |
commented Mar 28, 2017
12% sounds like one full CPU on a quad core with 8 'threads' (hyperthreading)
|
commented Mar 28, 2017
I will try the two links tomorrow and provide feedback. Thank you for your help. |
commented Mar 28, 2017 • edited
edited
I checked my laptop's CPU, it is i7-6500U which has 2 core and 4 threads for each core, so the program also consume all the resource of a thread on my laptop. I got the following result: For the |
commented Mar 28, 2017 • edited
edited
I wrote another script to test and I found something interesting: I changed sleep time from 0.25s to 10s, it always take about 2s to read all data from the buffer even the size of data varied, on both my laptop and RPi. |
Pyserial Readline
commented Mar 29, 2017 • edited
edited
I fixed the problem by get the number of bytes to read first and then read it from buffer: Do you need me to close the issue? |
commented Mar 29, 2017
Pyserial Write Timeout Error
maybe the timeout process let's overload the cpu. |
commented Jul 18, 2017
@leomikezee I think the problem is because the pyserial is working with polling mode. It doesn't like java or c with event trigger mechanism. |
commented Feb 28, 2018 • edited
edited
It is easy to implement a non-CPU time wasting readline method. In a loop, first call Not only does |
commented Feb 28, 2018
Here's a class that serves as a wrapper to a pyserial object. It allows you to read lines without 100% CPU. It does not contain any timeout logic. If a timeout occurs, I just measured. The code below gives me 790 kB/sec while replacing the code with pyserial's readline method gives me just 170kB/sec. |
commented Mar 9, 2018
@skoehler That code looks really useful! I wonder if it should be included in the examples for pyserial, if it's the best way to access the serial port via the library? I noticed that the pyserial examples haven't been updated in years. Do you have a complete working example of the class that you could shaare? |
commented Aug 28, 2018
this seems to be an issue with the lib; I took the complete system image and put on another raspberry (same model), and in the second device the CPU usage of the following part of the code is 50%, against >20% for the whole application in the other raspberry. The input is the same. |