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 the delay the hang still happens.

RPI3 python code

import serial
import time

if __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 115200
const 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.

15
  • 1
    also happens on Windows as well so RPI is not at fault ... maybe, but it does not exclude python problems
    – jsotola
    Jan 4, 2022 at 2:47
  • what error do you get on the RPi if you run LED blink sketch on the arduino?
    – jsotola
    Jan 4, 2022 at 2:52
  • 1
    Yes, still hangs Jan 4, 2022 at 3:28
  • 1
    Does it help if you remove the Serial.println() from the Arduino code? Jan 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 read ser regularly? Jan 4, 2022 at 9:06

1 Answer 1

2

You are flooding the RPi/PC with "I was written in RPI" and don't read it. It will work if you read ser 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.