5

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?

floppy380
  • 245
  • 1
  • 4
  • 10

1 Answers1

3

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().

gre_gor
  • 1,682
  • 4
  • 18
  • 28
Juraj
  • 18,264
  • 4
  • 31
  • 49