PuddingBear
PuddingBear

Reputation: 21

PySerial + Arduino MEGA 1 second delay/response time

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.

Running on MacOS, Python 3.8, Arduino 1.8.13

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?

Upvotes: 1

Views: 550

Answers (2)

gustavo-alberto
gustavo-alberto

Reputation: 1

Adding '\n' to the string worked for me.

Upvotes: 0

PuddingBear
PuddingBear

Reputation: 21

@Jasonharper is right. Str.encode doesn't add any '\n' I assumed that. Adding a '\n' to the string send from the Python solved all issues.

Upvotes: 1

Related Questions