I want to send string 50 to Arduino through serial port, the Arduino UNO seems it communicates with port COM25 but it doesnt receive the string properly:
import serial # if you have not already done so
ser = serial.Serial('COM25')
ser.baudrate = 9600
ser.write(b'50')
ser.close()
Btw Arduino code is correct I checked it with other serial com programs. How can I fix the above code?
1 Answer 1
The AVR Arduinos (Uno, Nano, Mega) have auto-reset function. At opening of USB connection the circuit around USB resets the MCU. After reset the bootloader waits a second for a new upload. If the upload doesn't happen the bootloader starts the current sketch.
The serial.Serial()
command in python opens the USB connection. With that the Arduino is reset and waits in bootloader while you send the data. The data doesn't arrive in your sketch. Add a two seconds wait time after Python's serial.Serial()
.
-
Thanks for this answer. This was driving me nuts. Alternatively, I added a line in Arduino setup() to send a byte and waited for that in python to get things started.SaTa– SaTa2019年10月13日 16:01:30 +00:00Commented Oct 13, 2019 at 16:01
serial.Serial()
resets Uno and it spends some time in bootloader after then. add a delay/sleep after serial.Serial