Pyserial Write Timeout

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. Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that readlines only works with a timeout. Readlines depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.

Join GitHub today

GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Sign upPython pyserial write timeoutPyserial readline timeout 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

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)

  • you say the loop basically looks like this? what else is there? is the CPU load really from this thread or some other thread in the application?
  • Which version of Python and pySerial are involved?
  • have you tried read instead of readline?
  • see also http://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.threaded.LineReader (usage example at the bottom of the page)
  • try http://pyserial.readthedocs.io/en/latest/url_handlers.html#spy to see what its doing on the serial port

commented Mar 28, 2017

  • I created a read thread to do all the reading.
  • I am using the latest python 3.6.1 and the latest pyserial installed by pip3 in a vitualenv.

  • Yes, I have tried read and detected newline characters by myself but it didn't solve the problem.

I will try the two links tomorrow and provide feedback.

Thank you for your help.

commented Mar 28, 2017
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 run the following script on both my laptop and RPi:

I got the following result:
test.txt
test-pi.txt
Both of them get a same amount of readings except that RPi took a little bit more time to fetch readings from the buffer.

For the serial.threaded.LineReader, I got error AttributeError: module 'serial' has no attribute 'threaded'.

commented Mar 28, 2017
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.
I used cProfile to take a look at the method call time and found this:
1 0.000 0.000 2.000 2.000 {method 'readall' of '_io._RawIOBase' objects}

commented Mar 29, 2017
edited

Pyserial Serial Write Timeout

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?
Thank you for your library and help!

Pyserial Write Timeout

commented Mar 29, 2017

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

It is easy to implement a non-CPU time wasting readline method. In a loop, first call ser.read(1) and then call ser.read(ser.in_waiting). Each time, check whether the returned data contains n. Of course the data after n needs to be saved as it may be part of another line. Also see the my recv() function from issue #318

Not only does ser.readline() waste 100% of one CPU core. It is also slow as hell! Reading from a virtual COM port /dev/ttyACM1 attached via USB, ser.readline() would give me a throughput of about 150 kB/sec. Using ser.read(1) in combination with ser.read(ser.in_waiting) would give me a throughput of about 660kB/sec.

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, 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.

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.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Active2 years, 5 months ago

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. More seriously, the timeout not always occurs, which means that sometimes I can write in the serial successfully.

I have ensured that the port is COM5 and the baudrate of the switch is 9600. Thanks a lot for answering my question.

foreverXZCforeverXZC

1 Answer

I ran into this problem recently. I found that setting write_timeout=0 solved the issue (also, not really sure why a write timeout even exists...)

Another answer here suggests the same thing :)

Community

Pyserial Write Timeout Exception

linzwattlinzwatt

Not the answer you're looking for? Browse other questions tagged pythonpyserial or ask your own question.