Arduino serial: Cannot allocate memory: '/dev/ttyACM0'

Viewed 285

I run a script on RPI (Raspbian GNU/Linux 11) with python 3.9.2 with following line:

import serial

serial.Serial('/dev/ttyACM0', 9600)

Which raises error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 322, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
OSError: [Errno 12] Cannot allocate memory: '/dev/ttyACM0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 244, in __init__
    self.open()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 325, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 12] could not open port /dev/ttyACM0: [Errno 12] Cannot allocate memory: '/dev/ttyACM0'

Same happens while using screen (v. 4.08.00):

~$ screen /dev/ttyACM0
[screen is terminating]
Cannot open line '/dev/ttyACM0' for R/W: Cannot allocate memory  

I've checked if this is an issue with RPI's lack of memory, but it doesn't seem like it:

free
               total        used        free      shared  buff/cache   available
Mem:         3930028      618276     1271488       77368     2040264     3076624

Just to make sure I checked and it seems that Arduino's symlink is still present.

ls -l /dev/serial/by-id/
total 0
lrwxrwxrwx 1 root root 13 Apr 26 18:02 usb-Arduino__www.arduino.cc__0043_85036313430351014161-if00 -> ../../ttyACM0

When I reboot the Arduino - problem is fixed but occurs again in some time. I didn't notice pattern here, sometimes it's a month, sometimes 2 weeks...

Any ideas where should I even look to fix this?


OS: Linux raspberrypi 5.15.32-v7l+ #1538 SMP Thu Mar 31 19:39:41 BST 2022 armv7l GNU/Linux

Arduino Sketch: github

void setup() {
  Serial.begin(9600); // open serial port, set the baud rate as 9600 bps
}

void loop() {
  int val;
  val = analogRead(0); // connect sensor to Analog 0
  Serial.println(val); // print the value to serial port
  delay(10); // 1000 = every second
}
0 Answers
Related