首页 > 解决方案 > 使用 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*

请告诉我如何解决这个错误!

标签: python

解决方案


推荐阅读