python – Raspberry Pi Serialport Issue

I am trying to control a servo motor via an arduino. This Arduino should get the instructions for the motor by a raspberry pi (via serialport)

Code C Arduino:

#include <Servo.h>


Servo servodown;


void setup() {
  servodown.attach(8);
  Serial.begin(9600);

 

}

void loop() {
  
 if (Serial.available() > 0) {
  String receivedString = Serial.readStringUntil('\n');  

  int nr = receivedString.toInt();


  if (nr >= 0 && nr <= 360) {
    Serial.print("Drehe auf ");
    Serial.println(nr, DEC);
    servodown.write(nr/2);
  } else {
    Serial.println("Digga deine Zahl ist nicht gültig");
  }
}
}

Code Python (Raspberry)

import serial
import time


print("Programm startet...")
degrees = input("Gradzahl bitte!")
if __name__ == "__main__":
        ser = serial.Serial("/dev/ttyACM1", 9600, timeout=1)
        ser.reset_input_buffer()

        while True:
                ser.write(str(degrees).encode("utf-8"))
                line = ser.readline().decode("utf-8").rstrip()
                print(line)
                time.sleep(1)

Every Time i run the code i´m getting an error because the ttyACM1 isn´t founded.
It´s named ttyACM0 now. So i change the name in the code. Same error… Now it´s named ttyACM1.
Error:

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    ser.write(str(degrees).encode("utf-8"))
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 571, in write
    raise SerialException('write failed: {}'.format(e))
serial.serialutil.SerialException: write failed: [Errno 5] Input/output error

or sometimes

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    ser = serial.Serial("/dev/ttyACM0", 9600, timeout=1)
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'

Connection is stable, changing cable, restarting, updating python, using sudo etc…..

Read more here: Source link