【发布时间】:2022-06-22 02:43:15
【问题描述】:
我正在使用 Python 和 Arduino 控制伺服器的位置。我在 python 中输入角度并将其发送到 Arduino,然后将伺服器移动到该特定角度。 Arduino代码:
#include <Servo.h>
Servo servo;
void setup() {
servo.attach(9);
Serial.begin(9600);
}
void loop() {
while(Serial.available())
{
int pos = Serial.parseInt();
if(pos >= 0)
{
servo.write(pos);
}
else
{
continue;
}
}
}
python 代码:
import serial
port = serial.Serial('COM3',9600)
while(port.isOpen()):
int_data = int(input("Enter servo position: "))
str_data = str(int_data)
byte_data = str_data.encode()
port.write(byte_data)
30 秒内一切正常,我可以控制伺服,但突然出现错误:WriteFile failed (PermissionError(13, 'the device does not recognize the command ', None, 22))
【问题讨论】:
标签: python arduino serial-port pyserial servo