Python Flush Serial Port Buffer

Jul 27, 2020 serial.flush — This method will flush file-like objects. It also makes the port wait until all of the data is written. Serial.inwaiting — This method returns the number of bytes in the received buffer. Serial.outwaiting — This method returns the number of bytes in the output buffer. This is because pyserial returns from opening the port before it is actually ready. I've noticed that things like flushInput don't actually clear the input buffer, for example, if called immediately after the open. Ser.flushInput #flush input buffer, discarding all its contents. Ser.flushOutput#flush output buffer, aborting current output. Python Serial.flush - 30 examples found. These are the top rated real world Python examples of serial.Serial.flush extracted from open source projects. You can rate examples to help us improve the quality of examples.

HomeProductsTeensyBlogForum
You are here:TeensyTeensyduinoUSB Serial
PJRC Store
Teensy 4.1, $26.85
Teensy 4.0, $19.95
Teensy 3.6, $29.25
Teensy 3.5, $24.25
Teensy 3.2, $19.80
Teensy LC, $11.65
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
Teensy
Main Page
Hardware
Getting Started
Tutorial
How-To Tips
Code Library
Projects
Teensyduino
Main
Download+Install
Basic Usage
Digital I/O
PWM & Tone
Timing
USB Serial
USB Keyboard
USB Mouse
USB Joystick
USB MIDI
USB Flight Sim
Serial
Libraries
Reference

Teensyduino provides a Serial object which is compatible with theSerial object on standard Arduino boards. Usually Serial is usedto print information to the Arduino IDE Serial Monitor.Unlike a standard Arduino, the Teensy Serial object always communicatesat 12 Mbit/sec USB speed. Many computes, especially older Macs, can notupdate the serial monitor window if there is no delay to limit the speed!In this example, 'Hello World...' is printed once per second.

The Teensy does not actually become a serial device until your sketch is running,so you must select the serial port (Tools -> Serial Port menu) afteryour sketch begins.

Standard Serial Functions

All of the standard Serial functions are supported.

Serial.begin()

Serial.begin() is optional on Teensy. USB hardware initialization isperformed before setup() runs.

The baud rate input is ignored, and only used for Arduino compatibility.USB serial communication always occurs at full USB speed. However,see the baud() function below.

Serial.begin() may wait up to 2.5 seconds for USB serial communicationto be ready. For fastest program startup, simply do not use thisunnecessary function. Its only purpose is for compatibility withprograms written for Arduino. The delay is intended for programswhich do not test the Serial boolean.

(bool) Serial

Test whether the Arduino Serial Monitor has opened. Normally this isused with a while loop to wait until the Arduino Serial Monitor isready to receive data.If you will use Teensy in a stand-alone application without theserial monitor, remember to remove this wait, so your program doesnot remain forever waiting for the serial monotor.

Alternately, the wait can be done with a timeout.

Serial.print() and Serial.println()

Print a number or string. Serial.print() prints only the number or string, andSerial.println() prints it with a newline character.On a standard Arduino, this function waits while the data is transmitted. With Teensyduino,Serial.print() and Serial.println() typically return quickly when the message fits within theUSB buffers. See Transmit Buffering below.

Serial.write()

Transmit a byte. You can also use Serial.write(buffer, length) to send more thanone byte at a time, for very fast and efficient data transmission.

Serial.available()

Returns the number of received bytes available to read, or zero if nothing has been received.

On a standard Arduino, Serial.available() tends to report individual bytes, whereaslarge blocks can become instantly available with Teensyduino.See Receive Buffering below for details.

Usually the return value from Serial.available() should be tested as a boolean, eitherthere is or is not data available. Only the bytes available from the most recentlyreceived USB packet are visible. SeeInefficient Single Byte USB Packets below for details.

Serial.read()

Read 1 byte (0 to 255), if available, or -1 if nothing available. Normally Serial.read()is used after Serial.available(). For example:

Serial.flush()

Wait for any transmitted data still in buffers to actually transmit. If no data is waiting in a buffer to transmit, flush() returns immediately.

Arduino 0022 & 0023: flush() discards any received data that has not been read.

Teensy USB Serial Extensions

Teensyduino provides extensions to the standard Arduino Serial object, soyou can access USB-specific features.

Serial.send_now()

Transmit any buffered data as soon as possible.See Transmit Buffering below.

Serial.dtr()

Read the DTR signal state. By default, DTR is low when no software hasthe serial device open, and it goes high when a program opens the port.Some programs override this behavior, but for normal software you can useDTR to know when a program is using the serial port.On a standard Arduino, the DTR and RTS signals are present on pins of theFTDI chip, but they are not connected to anything. You can solderwires between I/O pins and the FTDI chip if you need these signals.

Serial.rts()

Read the RTS signal state. USB includes flow control automatically,so you do not need to read this bit to know if the PC is ready to receiveyour data. No matter how fast you transmit, USB always manages buffersso all data is delivered reliably. However, you can cause excessive CPUusage by the receiving program is a GUI-based java applicationlike the Arduino serial monitior!

For programs that use RTS to signal some useful information, you can readit with this function.

The Windows USBSER.SYS driver has aknown bugwhere changes made to RTS are not communicated to Teensy (or any other boardsusing CDC-ACM protocol) until DTR is written. Some programs, like CoolTerm,work around this bug.

Serial.baud()

Read the baud rate setting from the PC or Mac. Communication is alwaysperformed at full USB speed. The baud rate is useful if you intend tomake a USB to serial bridge, where you need to know what speed the PCintends the serial communication to use.

Serial.stopbits()

Read the stop bits setting from the PC or Mac. USB never uses stopbits.

Serial.paritytype()

Read the parity type setting from the PC or Mac. USB uses CRC checkingon all bulk mode data packets and automatically retransmits corrupteddata, so parity bits are never used.

Serial.numbits()

Read the number of bits setting from the PC or Mac. USB always communicates8 bit bytes.

USB Buffering and Timing Details

Usually the Serial object is used to transmit and receive data withoutworrying about the finer timing details. It 'just works' in most cases.But sometimes communication timing details are important, particularlytransmitting to the PC.

Transmit Buffering

On a standard Arduino, when you transmit with Serial.print(), the bytesare transmitted slowly by the on-chip UART to a FTDI USB-serial converterchip. The UART buffers 2 bytes, so Serial.print() will return when allbut the last 2 bytes have been sent to the FTDI converter chip, which inturn stores the bytes into its own USB buffer.

On a Teensy, Serial.print() writes directly into the USB buffer. Ifyour entire message fits within the buffer, Serial.print() returns toyour sketch very quickly.

Both Teensyduino and the FTDI chip hold a partially filled bufferin case you want to transmit more data. After a brief timeout, usually8 or 16 ms on FTDI and 3 ms in Teensyduino, the partially filled bufferis scheduled to transmit on the USB.

The FTDI chip can be made to immediately schedule a partially filledbuffer by toggling any of the handshake lines (which are not connectedon a standard Arduino board). It can also be configured (by the PCdevice driver) to schedule when an 'event character' is received.Normally it is difficult to control when the FTDI chip schedules itspartially filled transmit buffer.

Teensyduino immediately schedules any partially filled buffer to transmitwhen the Serial.send_now() function is called.

All USB bandwidth is managed by the host controller chip in your PC orMac. When a full or partially filled buffer is ready to transmit, itactual transmission occurs when the hostcontroller allows. Usually this host controller chip requests anyscheduled transfers 1000 times per second, so typically actual transmissionoccurs within 0 to 1 ms after the buffer is scheduled to transmit. Ifother devices are using a lot of USB bandwidth, priority is given to'interrupt' (keyboard, mouse, etc) and 'isychronous' (video, audio, etc)type transfers.

When the host controller receives the data, the operating system thenschedules the receiving program to run. On Linux, serial ports openedwith the 'low latency' option are awakened quickly, others usually waituntil a normal 'tick' to run. Windows and MacOS likely add processscheduling delays. Complex runtime environments (eg, Java) can alsoadd substantial delay.

Receive Buffering

When the PC transmits, usually the host controller will send at leastthe first USB packet within the next 1ms. Both Teensyduino and theFTDI chip on Arduino receive a full USB packet (and verify its CRC check). TheFTDI chip then sends the data to a standard Arduino via slow serial.A sketch repetitively calling Serial.available() and Serial.read() willtend to see each byte, then many calls to Serial.availble() will returnfalse until the next byte arrives via the serial communication.On aTeensy, the entire packet, up to 64 bytes, becomes available all at once.Sketches that do other work while receiving data might depend onslow reception behavior, where successive calls to Serial.available()are very unlikely to return true. On a Teensy receiving large amountsof data, it may be necessary to add a variable to count the number ofbytes processed and limit the delay before other important work mustbe done.

Inefficient Single Byte USB Packets

When transmitting, Serial.write() and Serial.print() group bytes from successivewrites together into USB packets, to make best possible use of USB bandwidth. Youcan override this behavior using Serial.send_now(), but by default multiple writesare merged to form packets.

Microsoft Windows and Linux unfortunately do NOT provide a similar function whentransmitting data. If an application writes inefficiently, such as a single byteat a time, each byte is sent in a single USB packet (which could have held 64 bytes).While this makes poor use of USB bandwidth, a larger concern is how this affectsbuffering as seen by Serial.available().

The USB hardware present in Teensy can buffer 2 USB packets. Serial.available()reports the number of bytes that are unread from the first packet only. If thepacket contains only 1 byte, Serial.available() will return 1, regardless ofhow many bytes may be present in the 2nd package, or how many bytes may be waiting in more packets still buffered by the PC's USB host controller.

This code will not work on Teensy when the PC transmits the expected 11 byte messagein more than 1 USB packet.

This code can be rewritten to always read a byte when Serial.available() returns non-zero.

Of course, there are always many ways to write a program. The above versions lookfor a '@' character to begin the message, but do not handle the case where additionalbytes (incorrectly) appear before the 10 digit number. It is also not necessary tostore the entire message in a buffer, since the work can be done as the bytes areread. Here is a more robust and more efficient version.

I am using Python script to record data from a gyroscope. Arduino-compatible micro-controller collects the data and sends it via a serial connection to the PC. Here is the simplified Arduino and Python code:

Python

Arduino:

Python:

Data rate is 300 samples per second and size of the sample is 3x4Bytes floating point values.

Python Flush Serial Port Buffer Tool

What is missing:

  1. Synchronisation between Python script and the serial data coming from Arduino device. When I suddenly start the Python script sometimes it starts in the middle of data stream. It would be nice to make it detect the beginning of the 16 byte sample.
  2. It does not capture everything and sometimes skips the samples.
  3. Many other things which I probably do not know.

What I want to achieve:
My goal is to create a Python logger which can fully capture the serial data from the sensor in any circumstances. Please let me know your tips and advise me how to do it.

Python Flush Serial Port Buffer Example

Thanks!