Python Serial Inwaiting Example

Active2 months ago

Examples¶ Miniterm¶ Miniterm is now available as module instead of example. See serial.tools.miniterm for details. Miniterm.py The miniterm program. Send zeroconf announcements when port appears or disappears (uses python-avahi and dbus). Service name: _serial_port._tcp. Pyserial - How to read the last line sent from a serial device. I want to make a Python script that will read from the serial port only every few seconds, so I. Listing ports¶. Python-m serial.tools.list_ports will print a list of available ports. It is also possible to add a regexp as first argument and the list will only include entries that matched. Do also have a look at the example files in the examples directory in the source distribution or online. Python-m serial.tools.list_ports will print a list of available ports. It is also possible to add a regexp as first argument and the list will only include entries that matched. If you connect a serial device with a R232-converter, make sure that it uses the 3.3V supply (pin # 1). There are also serial devices/adapters working with TTL levels. If you want to use such a device directly with the GPIO port, make sure that you can power it with 3.3V since the standard TTL signal 0V/5V can destroy your Raspberry Pi.

I am using a script in Python to collect data from a PIC microcontroller via serial port at 2Mbps.

The PIC works with perfect timing at 2Mbps, also the FTDI usb-serial port works great at 2Mbps (both verified with oscilloscope)

Im sending messages (size of about 15 chars) about 100-150x times a second and the number there increments (to check if i have messages being lost and so on)

On my laptop I have Xubuntu running as virtual machine, I can read the serial port via Putty and via my script (python 2.7 and pySerial)

Serial

The problem:

  • When opening the serial port via Putty I see all messages (the counter in the message increments 1 by 1). Perfect!
  • When opening the serial port via pySerial I see all messages but instead of receiving 100-150x per second i receive them at about 5 per second (still the message increments 1 by 1) but they are probably stored in some buffer as when I power off the PIC, i can go to the kitchen and come back and im still receiving messages.

Here is the code (I omitted most part of the code, but the loop is the same):

Anyone knows why pySerial takes so much time to read from the serial port till the end of the line?Any help?

I want to have this in real time.

Thank you

Vasco BaptistaVasco Baptista
1982 gold badges4 silver badges14 bronze badges

4 Answers

You can use inWaiting() to get the amount of bytes available at the input queue.

Then you can use read() to read the bytes, something like that:

Example

Pyserial Inwaiting

Why not to use readline()at this case from Docs:

You are waiting for the timeout at each reading since it waits for eol. the serial input Q remains the same it just a lot of time to get to the 'end' of the buffer, To understand it better: you are writing to the input Q like a race car, and reading like an old car :)

Kobi KKobi K
5,2173 gold badges28 silver badges64 bronze badges

You need to set the timeout to 'None' when you open the serial port:

This is a blocking command, so you are waiting until you receive data that has newline (n or rn) at the end:line = ser.readline()

Once you have the data, it will return ASAP.

Fabian MeierFabian Meier

From the manual:

Possible values for the parameter timeout: … x set timeout to x seconds

and

readlines(sizehint=None, eol='n') Read a list of lines, until timeout. sizehint is ignored and only present for API compatibility with built-in File objects.

Note that this function only returns on a timeout.

So your readlines will return at most every 2 seconds. Use read() as Tim suggested.

mswmsw
38.1k5 gold badges65 silver badges103 bronze badges

A very good solution to this can be found here:

Python Serial In Waiting Examples

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.

It is also supposed to be fast according to the author:

The code below gives me 790 kB/sec while replacing the code with pyserial's readline method gives me just 170kB/sec.

Python Serial In Raspberry

JoeJoe

Python Serial Timeout Example

2,8501 gold badge6 silver badges22 bronze badges

Python Serial Readline Example

Not the answer you're looking for? Browse other questions tagged pythonpython-2.7serial-portpyserial or ask your own question.