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 it would be stuck on print ser.readline() which I'm assuming means for some reason python is not writing to the serial port and so it's not getting anything back. I would have to quit the python program and run it again to get it to get a reply from arduino and it would continue to work until I re-upload the arduino then the same thing happens. 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.
arduino
String str;
void setup() {
// Turn the Serial Protocol ON
Serial.begin(115200);
}
void loop() {
if (Serial.available()) {
str = Serial.readStringUntil('n'); // Read the serial input
Serial.println(str); // sends ascii code
}
}
Python
import serial
ser = serial.Serial('/dev/ttyACM1', 115200)
for i in range(0,4):
str = "test stringn"
ser.write(str)
print ser.readline()
Aucun commentaire:
Enregistrer un commentaire