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.