Very slow serial communication - Raspberry Pi 4> Arduino Mega

I read all your documentation, but, as I told you, a lot was in Chinese because it is a new topic for me. But, little by little everything is clearing up.

I placed behind the while True line
ser = serial.Serial ("/dev/ttyUSB0", 115200)

I put a time.sleep of 0.851 and it does work, that is, what you said about: "At 115200 baud, it takes more than 80 microseconds to send a single byte" is correct.

But how do I increase that speed? It's possible? I ask because the documentation only reaches 115200 https://www.arduino.cc/en/Serial.Begin

What I don't want is to send the data every 0.8 seconds.

Get rid of the delay - you only needed it because you were restarting the Arduino by repeatedly opening the serial port

Like I said, if I remove the delay it prints a lot of information and sends a lot of data to the Arduino, but it doesn't have time to react. That is the problem I want to solve.

raym3d:
Like I said, if I remove the delay it prints a lot of information and sends a lot of data to the Arduino, but it doesn't have time to react. That is the problem I want to solve.

Post the latest version of your programs so we can see exactly what you have done. The devil is in the detail.

...R

Arduino:

int led = 12;

void setup() {
  Serial.begin(115200);
  pinMode(led , OUTPUT);
  Serial.setTimeout(100);
}

void loop() {
  if (Serial.available() > 0) {
    int command = Serial.read();
    if (command == '1') {
      digitalWrite(led, HIGH);
    }
    else if (command == '2') {
      digitalWrite(led, LOW);
    }
  }
}

Python:

import serial
import time

ser = serial.Serial("/dev/ttyUSB0", 115200)

while True:
    ABC = 1
    time.sleep(0.851)
    if ABC > 0:
        print("Sent")
        ser.write(b'1')
       
    else:
        print("Waiting")
        ser.write(b'2')

It's basically the same, with the simple fact that I placed ser = serial.Serial("/dev/ttyUSB0", 115200) behind while True.

I'm experimenting, if I put an input so that ABC is variable, and I send data immediately, it does work there in less than a second, but with While it doesn't work in less than a second.
Example:

while True:
    ABC = input('Enter number: ')
    if ABC > 0:
        print("Sent")
        ser.write(b'1')
       
    elif ABC < 0:
        print("Waiting")
        ser.write(b'2')

    else: 
        ABC = input('Enter number: ')

That is what I want, not to have to wait 0.851 seconds to send the information from Python.

I found the solution:
Put the time.sleep together with "ser"

...
ser = serial.Serial("/dev/ttyUSB0", 115200)
time.sleep(0.851)

while True
   ABC = 1
...

Can you explain to me why it does work there?

I will read all your documentation several times to understand even more.

THANK YOU VERY MUCH TO ALL.

raym3d:
Can you explain to me why it does work there?

I thought I explained it in Reply #19.

You have to allow time for the Arduino to go through its reset process when the PC opens the serial port.

If you look at my Tutorial you will see that I deal with that by getting the Arduino to send the message "Arduino is ready" from setup() and the Python program does nothing until it receives that message.

...R