mazus
mazus

Reputation: 1

High latency on serial Communication between PC and Ardunio

I want to communicate between my PC and Arduino Lenardo. On my PC I use Python and on my Arduino Lenardo I use C++. I can communicate between them with Serial, but the latency for "PC to Arduino and back" is always like 1 sek. I want to communicate between them as fast as possible (I mean low latency / my data is just a small variable).

Python Code on PC

import serial
import time

ser = serial.Serial('COM11', 9600) 

while True:
    data = input("message: ") 
    start_time = time.time()
    ser.write(data.encode())
    print("sent message:", data)
    
    response = ser.readline().decode().strip()
    delta_time = time.time() - start_time
    print("answer received:", response, delta_time)

C++ code on Arduino Lenardo

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    String data = Serial.readStringUntil('\n');
    
    String response = "received message: " + data;
    
    Serial.println(response);
  }
}

Can I get a latency of 1 ms?

Upvotes: 0

Views: 65

Answers (1)

mazus
mazus

Reputation: 1

You just have to add timeout=0 in the python code. My latency is now <1ms.

ser = serial.Serial('COM11', 9600, timeout=0)

Upvotes: 0

Related Questions