1

I'm having issues with Arduino and RPI3 serial communication, it'll become unresponsive after a little time. Tried increasing BAUD rate and same issue, also happens on Windows as well so RPI is not at fault, and even after increasing thedelay the hang still happens.

RPI3 python code

import serialimport timeif __name__ == '__main__':  ser = serial.Serial('/dev/ttyACM0', 115200, timeout=1, writeTimeout=0)  ser.flush()  last = "on\n"  while True:    last = "on\n" if last == "off\n" else "off\n"    ser.write(last.encode())    print(last)    time.sleep(0.5)

Arduino code

#define BAUD_RATE 115200const int LED_PIN = 8;String command;void setup() {  Serial.begin(BAUD_RATE);  pinMode(LED_PIN, OUTPUT);  delay(100);}void loop() {  Serial.println("I was written in RPI");  if (Serial.available()) {    command = Serial.readStringUntil('\n');    if (command.equals("on")) {      digitalWrite(LED_PIN, HIGH);    } else if (command.equals("off")) {      digitalWrite(LED_PIN, LOW);    }  }  delay(50);}

after less than a minute I'll get this exception from python

BlockingIOError: [Errno 11] Resource temporarily unavailable

The Arduino TX and RX led's stay on when it becomes unresponsive rather than flashing like it normally does.

askedJan 4, 2022 at 2:41
Philip Rollins's user avatar
15
  • 1
    also happens on Windows as well so RPI is not at fault ... maybe, but it does not exclude python problemsCommentedJan 4, 2022 at 2:47
  • what error do you get on the RPi if you run LED blink sketch on the arduino?CommentedJan 4, 2022 at 2:52
  • 1
    Yes, still hangsCommentedJan 4, 2022 at 3:28
  • 1
    Does it help if you remove theSerial.println() from the Arduino code?CommentedJan 4, 2022 at 8:24
  • 3
    You are flooding the RPi/PC with"I was written in RPI" and don't read it. Does it work if you readser regularly?CommentedJan 4, 2022 at 9:06

1 Answer1

2

You are flooding the RPi/PC with"I was written in RPI" and don't read it. It will work if you readser regularly.

On the delays: A message over a serial line has a transmission time, in your case 20 characters at 115200 baud from Arduino to RPi/PC. With the common 8n1 protocol, characters are sent at 11520 per second. With 20 characters this will take nearly 0.002 seconds, or 2 ms. The receiver needs a smaller delay than the sender to avoid overflows.

If the Arduino were using the USB directly (AFAIK it does not, between its USB interface and the actual AVR is a "real" serial line), the baudrate does not matter. A virtual serial line over USB is always really fast, independent of the announced baudrate. I once used 921600 baud with success. ;-) The baudrate is only used if a real serial line is involved.

answeredJan 5, 2022 at 8:16
the busybee's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.