Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/PySerial and CPU usage

I've created a script to monitor the output of a serial port that receives 3-4 lines of data every half hour - the script runs fine and grabs everything that comes off the port which at the end of the day is what matters...

What bugs me, however, is that the cpu usage seems rather high for a program that's just monitoring a single serial port, 1 core will always be at 100% usage while this script is running.

I'm basically running a modified version of the code in this question: pyserial - How to Read Last Line Sent from Serial Device

I've tried polling the inWaiting() function at regular intervals and having it sleep when inWaiting() is 0 - I've tried intervals from 1 second down to 0.001 seconds (basically, as often as I can without driving up the cpu usage) - this will succeed in grabbing the first line but seems to miss the rest of the data.

Adjusting the timeout of the serial port doesn't seem to have any effect on cpu usage, nor does putting the listening function into it's own thread (not that I really expected a difference but it was worth trying).

  • Should python/pyserial be using this much cpu? (this seems like overkill)
  • Am I wasting my time on this quest / Should I just bite the bullet and schedule the script to sleep for the periods that I know no data will be coming?
like image 623
Crazy Joe Malloy Avatar asked Aug 25 '09 14:08

Crazy Joe Malloy


People also ask

Is pySerial the same as serial?

pySerial includes a small console based terminal program called serial. tools.

What is pySerial used for?

PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on. It also supports remote serial ports via RFC 2217 (since V2. 5).

What does timeout mean in pySerial?

timeout = x : set timeout to x seconds (float allowed) returns immediately when the requested number of bytes are available, otherwise wait until the timeout expires and return all bytes that were received until then.

How do you check if I have pySerial?

To check that it is installed, start Pyzo and at the command prompt type in: import serial If it just gives you another >>> prompt, all is good.


1 Answers

Maybe you could issue a blocking read(1) call, and when it succeeds use read(inWaiting()) to get the right number of remaining bytes.

like image 111
tonfa Avatar answered Sep 21 '22 05:09

tonfa