python - 使用 PyFirmata 时如何解决 WriteFile 错误
问题描述
我正在使用 pyFirmata 为我的项目移动伺服电机。当我运行我的项目时,它会运行两次并出现以下错误。代码和结果如下所示。
from pyfirmata2 import Arduino, SERVO
import time
board = Arduino('COM3')
servo = board.get_pin('d:9:s')
board.digital[9].mode = SERVO
# def rotateServo(pin, angle):
# board.digital[9].write(angle)
# time.sleep(0.1)
current_angle = servo.read()
while True:
# for i in range (0,180):
# rotateServo(9, i)
print(current_angle)
if current_angle != 0 or current_angle == 0:
servo.write(0)
print("0")
time.sleep(2)
servo.read()
servo.write(90)
print("90")
time.sleep(2)
错误:
*C:\Python_Virtualenv\Scripts\python.exe "E:/Python Projects/servo.py"
0
0
90
0
Traceback (most recent call last):
File "E:\Python Projects\servo.py", line 18, in <module>
servo.write(0)
File "C:\Users\n1v3d\AppData\Local\Programs\Python\Python39\lib\site-packages\pyfirmata2\pyfirmata2.py", line 608, in write
self.board.sp.write(msg)
File "C:\Users\n1v3d\AppData\Local\Programs\Python\Python39\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))
Exception ignored in: <function Board.__del__ at 0x000001F25EB3D040>
Traceback (most recent call last):
File "C:\Users\n1v3d\AppData\Local\Programs\Python\Python39\lib\site-packages\pyfirmata2\pyfirmata2.py", line 148, in __del__
File "C:\Users\n1v3d\AppData\Local\Programs\Python\Python39\lib\site-packages\pyfirmata2\pyfirmata2.py", line 385, in exit
File "C:\Users\n1v3d\AppData\Local\Programs\Python\Python39\lib\site-packages\pyfirmata2\pyfirmata2.py", line 528, in _set_mode
File "C:\Users\n1v3d\AppData\Local\Programs\Python\Python39\lib\site-packages\serial\serialwin32.py", line 317, in write
serial.serialutil.SerialException: WriteFile failed (PermissionError(13, 'The device does not recognize the command.', None, 22))
Process finished with exit code 1*
请告诉我如何解决这个错误!
解决方案
推荐阅读
- etl - Talend:迭代和解析数组中的值到不同的点 tXMLMap
- python - 训练和验证数据集的拆分
- python - 如何使用matplot lib将变量转换为百分比图
- typescript - 如何从字符串数组构建 Typescript 类型?
- excel - 在两个链接列之间的两个工作表之间需要 Vlookup 帮助
- swift - 如何连续执行多个 SKSprintNode 的动画?
- javascript - 如何修复此井字游戏的未捕获参考错误
- arrays - 将字符串的字母分配到数组中
- jquery - jquery删除列表项为空
- azure - 我们可以使用单个 Azure AD 应用访问不同的 Azure AD 目录吗