PySerial + Arduino MEGA 1 second delay/response time

Hi all,

We have this issue with communication delay between PySerial and Arduino. We use the Arduino to set some switches and wait for a ACK back from the Arduino before continuing. For some reason there is a perfect 1 second delay when using PySerial instead of the Serial Monitor from Arduino. With the Serial Monitor there is only a few ms delay like expected.

I have made a simple test code to show the issue:

Python

import serial
import datetime 

s = serial.Serial(port='/dev/cu.usbmodem1412301', baudrate=115200, timeout=2)

while 1:
    a = datetime.datetime.now()
    s.write(str.encode("marco"))
    data = s.readline()
    b = datetime.datetime.now()
    print(data)
    c = b-a
    print(c)

Python Output

b'polo\r\n'
0:00:01.003257

b'polo\r\n'
0:00:01.003490

Arduino

void setup() 
{
  // Start Serial
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  if(Serial.available())
  {
    String cmd = Serial.readStringUntil('\n');    

    if (cmd.equals("marco"))
    {
      Serial.println("polo");
    }
  }
}

Any idea what the potential issue could be here?

send a \n. readStringUntil has a 1 second timeout

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.