Please help me understand why the following python code only works if the delay ( time.sleep (1) ) is more than 1 second?

import time import serial ser = serial.Serial(port='/dev/ttyUSB0',baudrate=115200) ser.write(b'1') while 1: ser.write(b'test\n') time.sleep(1) while ser.inWaiting() > 0: line = ser.readline() if line: print(line.decode().strip()) 

Sketch code for Arduino UNO:

 void setup() { Serial.begin(115200); } void loop() { String incomingData; if (Serial.available() > 0) { //если есть доступные данные incomingData = Serial.readString(); Serial.println("123456789"); } } 
  • Try adding serial.flash() after serial.write(b'test') . - Vanyamba Electronics
  • unfortunately it did not help. - Nikita Chukov
  • When connected, Arduino waits for a while when I start uploading a sketch to it, and only after that will it start receiving commands from you. Therefore, after connecting, you must wait 2 to 4 seconds. Or to make so that arduino did not expect loading of the sketch. This is done in hardware. - Avernial
  • You were right. Arduin reboots and needs a pause at the beginning. Rewrote the sketch and code in python: gist.github.com/nikitachukov/e4c3538c72ff7f269cf6e78aef85259d - Nikita Chukov
  • gist.github.com/nikitachukov/7dbe0d6c1a2099181cd1603808a88c61 From delays it was possible to be beaten. - Nikita Chukov

0