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.
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.
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:
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.