PySerial does not talk with Arduino

I wrote code to simulate some of the hardware that I work with, and uploaded it to the Arduino board. This code works. I know this because I get the expected response from HyperTerminal.

However, when I try to connect using PySerial, the connection is not an error, but I do not receive a response to the commands that I send.

Why could this be?

Python code

import serial def 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 = 0.5 sp.xonxoff = False sp.rtscts = False sp.dsrdtr = False sp.open() 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() 

NB: the Arduino code sends back \r\n at the end of the response to the command.

HyperTerminal Configuration:

COM4 configuration in HyperTerminal

Edit

I found that if I increase the timeout to 10 seconds and add sp.readline() before sending anything, then I get answers to both commands.

How long is a hardware handshake usually between PySerial and Arduino or USB RS-232 ports?

+7
source share
4 answers

It is not possible to verify this, but it may be that you try and read before there is any data, and you will not receive a response.

To check this, you can try and poll until the information appears

 value = None while not value: value = sp.readline() print value 

Edit

Arduino will reset when opening a serial connection, any data recorded at boot time will most likely go to bit. You can use the sleep for 2 seconds (could not find the exact time that is required, it will probably change anyway) before doing any read / write.

Alternatively, you can write to it until you get an answer, after you get the answer, you will begin to do the “real work”.

+4
source

I recently ran into this problem, and here is my solution:

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

Turns off this successfully reset the Arduino board.

+2
source

I am currently using a workaround. I set the timeout to 1.5 seconds and placed a readline call before the first write.

So now the Python code looks like this:

 import serial def 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() 
+1
source

Add a delay after opening the port, since Arduino is reset, and the bootloader starts listening to the new firmware. If something is sent at this point, the MCU remains stuck in the bootloader. A delay causes the bootloader to overload.

 sp.open() time.sleep(2) # 2 seconds or possibly a bit less sp.write("blahblah") 
0
source

All Articles