Serial I/O between Uno and Python on Raspberry Pi - Strange Behavior, I think

While developing a project that requires serial communication between Raspberry Pi Python and and Arduino Uno, I found something that I don't think is right.

The code running on the Uno is:

String y;

void setup() {
 Serial.begin(115200);
 Serial.setTimeout(1);
}
void First(){
  Serial.print("Received a");
  }

void Second(){
  Serial.print("Received b");
  }

void Junk(){
  Serial.print("Invalid command");
  }
  
void loop() {
 y = "";
 while (!Serial.available());
 y = Serial.readString();
 switch (y[0]) {
  case 'a':
    First();
    break;
  case 'b':
    Second();
    break;
  default:
    Junk();
    break;}
}

The python code is:

# Importing Libraries
import serial

arduino = serial.Serial('/dev/ttyACM0',baudrate=115200, timeout=.1)

def writeArduino(x):
    out = x.encode('utf-8')
    arduino.write(out)
    return

def readArduino():
    #wait for  data from Arduino
    count = 0
    while arduino.inWaiting() == 0:
        count = count + 1
        
    y = arduino.readline().decode('ascii')
    return y

**input()**
while True:
    x = 'a'
    writeArduino(x)
    z = readArduino()
    print(z)
    x = 'b'
    writeArduino(x)
    z = readArduino()
    print(z)
    x = 'c'
    writeArduino(x)
    z = readArduino()
    print(z)

If the input statement is included, the communication is as expected...a, b, and c are bounced back and forth. However, if the input statement is commented out, the program hangs forever.

I'm wondering what to make of this and welcome thoughts others might have.

Thank you.

something to dig into:

when you open the Serial port, the Arduino reboots. It takes a tiny bit of time for the Arduino to be ready, to run the setup and configure the Serial port.

without the input() statement, your python code just fires away the 'a' and then goes into an infinite loop (with readArduino()) to expect something back from the Arduino. but if the Arduino did not receive anything (because the 'a' was never received), you never talk back and thus the python code stays stuck in the infinite loop.

➜ my take : your input() statement is slowing down the python code enough for the Arduino to boot and be ready to receive the 'a' and thus then answer correctly

you could verify by just adding say a 2s delay at the start of the python script

# Importing Libraries
import serial
import time

arduino = serial.Serial('/dev/ttyACM0',baudrate=115200, timeout=.1)

def writeArduino(x):
    out = x.encode('utf-8')
    arduino.write(out)
    return

def readArduino():
    #wait for  data from Arduino
    count = 0
    while arduino.inWaiting() == 0:
        count = count + 1
        
    y = arduino.readline().decode('ascii')
    return y

time.sleep(2)

while True:
    x = 'a'
    writeArduino(x)
    z = readArduino()
    print(z)
    x = 'b'
    writeArduino(x)
    z = readArduino()
    print(z)
    x = 'c'
    writeArduino(x)
    z = readArduino()
    print(z)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.