PySerial not talking to Arduino PySerial not talking to Arduino windows windows

PySerial not talking to Arduino


Can not verify this but it could be that you try and read before there is any data there, thus you get no reply back.

To test this you could try and poll until there is data

value = Nonewhile not value:   value = sp.readline()print value

Edit

The Arduino will reset when you open a serial connection, any data written during bootup will likely go to bit heaven. You could use a sleep for 2 seconds (could not find the exact time it takes, will likely vary anyway) before you do any reads/writes.

Alternatively you could write to it until you get a response back, after you get a return you start doing "real work".


For the time being I am using a workaround. I have set the timeout to 1.5 seconds and put a readline call in before the first write.

So now the Python code looks like:

import serialdef main():    sp = serial.Serial()    sp.port = 'COM4'    sp.baudrate = 19200    sp.parity = serial.PARITY_NONE    sp.bytesize = serial.EIGHTBITS    sp.stopbits = serial.STOPBITS_ONE    sp.timeout = 1.5 #1.5 to give the hardware handshake time to happen    sp.xonxoff = False    sp.rtscts = False    sp.dsrdtr = False    sp.open()    sp.readline() #to give the hardware handshake time to happen    sp.write("GV\r\n".encode('ascii'))    value = sp.readline()    print value    sp.write("GI\r\n".encode('ascii'))    value = sp.readline()    print value    sp.close()if __name__ == "__main__":    main()


I've also encountered this problem recently and here's my solution:

import serialser = serial.Serial(4, timeout=2)ser.setRTS(True)ser.setRTS(False)while 1:    line = ser.readline()    print(line)ser.close

Turns out this will successfully reset the Arduino board.