I've also asked this question over on stack exchange so I'll link the actual question here.

The short story is I have a plot that updates in real time from data collected on an Arduino. The plot updates on a 50ms + overhead interval and I can either choose on the Arduino side to either delay the loop 50ms + compensation or use flush(). I'm running into some problems with the delay option where my program begins to lag (Python side). I'm wondering if flush will work as I expect and hold the output until my python program reads from the serial port. So then the Python program is completely controlling the flow of data every time it polls for an update.

Edit: Here's a snippet of the code.

void loop() {
  // put your main code here, to run repeatedly:
  val1 = analogRead(0);
  val2 = analogRead(1);
  Serial.print(" ");
  • Please post a snippet of the Arduino side of the code. It should not send data faster than every 50 ms. Dec 3, 2015 at 20:41
  • I edited my post to add the snippet Dec 3, 2015 at 20:45
  • Thanks! You can test this: Reduce the update speed and verify the lag. When you get no lag. Increase the number of samples written. Typically python does not read the serial port directly. It goes through the operating system. There is also buffering going on in the python serial module and on top of that there is the garbage collector. You can help the python side by using a lower update rate but still keep the sample rate on the Arduino side. Dec 3, 2015 at 20:51
  • What serial speed are you using - is 50ms enough time to transmit all the data? If you're using slow serial, maybe just transferring the data is taking too long.
    – Kingsley
    Dec 4, 2015 at 0:58
  • Uh... answered over there, copied below.
    – slash-dev
    Dec 4, 2015 at 1:41

1 Answer 1


On the Arduino side, flush simply waits until the last byte has been fully transmitted. It says nothing about how quickly it is being read by the PC.

The general problem you are having is called "flow control". It has been solved with many different techniques, like "handshaking". For example, the PC could send a character when it has received the last byte of a data set (a line, as returned by readline):

line = self.ser.readline()
self.ser.write("!"); // Tell Arduino line was received
line = line.split()

While the PC proceeds to parse the line, the Arduino will eventually receive the handshake character:

void loop()
  // Send a line
  Serial.println( ... );

  // Wait for handshake character
  bool gotHandshake;
  do {
    while (!Serial.available())
      ; // wait for chars
    char c = Serial.read();
    gotHandshake = (c == '!');
  } while (!gotHandshake);

Then the Arduino can proceed to send the next data set (i.e., a line). The OS on the PC will buffer characters from the Arduino until the python program receives them with readline and sends another handshake character. No delay required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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