bakalolo bakalolo - 1 year ago 113
Python Question

Python serial write doesn't work FIRST run

I have 2 programs to test serial communication, an simple arduino program that echoes whatever is on the serial port and a python program that writes to the serial port and prints the reply.

I'm having an issue where whenever I upload the arduino program and try to run the python the first time after I uploaded, it would be stuck on

print ser.readline()
which I'm assuming means for some reason python is not writing to the serial port. I would have to quit the python program and run it again to get it to get a reply from arduino. The program would continue to work until I re-upload the arduino then once again python wouldn't work on first run. Also if I open and close the serial monitor before I run the python program it will work the first run. Does anyone know what is the issue? This is on Ubuntu.


String str;

void setup() {
// Turn the Serial Protocol ON

void loop() {
if (Serial.available()) {
str = Serial.readStringUntil('\n'); // Read the serial input
Serial.println(str); // sends ascii code



import serial

ser = serial.Serial('/dev/ttyACM1', 115200)

for i in range(0,4):
str = "test string\n"
print ser.readline()

Answer Source

I added


after opening the serial port and the issue was fixed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download