I'm controlling the position of a servo with Python and an Arduino. I type in the angle in python and it sends to the Arduino which then moves the servo to that specific angle. Arduino code:
#include <Servo.h>
Servo servo;
void setup()
{
servo.attach(9);
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
int pos = Serial.parseInt();
servo.write(pos);
}
}
Python code:
import serial
port = serial.Serial('COM9' ,9600, timeout=1)
while True:
data = input("Enter servo position: ")
byte_data = data.encode()
port.write(byte_data)
Everything works fine for a while and I can control the servo, but then it suddenly gives me this error:
File "C:\Users\User\Projects\p1\venv\lib\site-packages\serial\serialwin32.py", line 317, in write
raise SerialException("WriteFile failed ({!r})".format(ctypes.WinError()))
serial.serialutil.SerialException: WriteFile failed (PermissionError(13, 'the device does not recognize the command', None, 22))
It happens pretty randomly, and I have no idea why.