【问题标题】:PySerial Input/Output ErrorPySerial 输入/输出错误
【发布时间】:2018-03-27 01:39:20
【问题描述】:

我有一个使用 SSC-32 伺服控制器运行的四足机器人。伺服控制器有一个串行端口,所以我购买了一条 USB 转串行电缆向它发送命令。因为我最精通 Python,所以我决定使用 PySerial 来打开和关闭 Serial 端口。我在我的 MAC 上运行 Linux VM。

显示的错误消息如下所示。还包括我的代码。

如您所见,访问端口有困难。但是,当我拔下并重新插入端口时,错误消息消失了,我能够成功发送命令。下次我尝试运行代码时,会出现错误消息。我怀疑端口难以关闭,因为当我拔下电缆时,我手动关闭它,结果代码可以正常工作。

为了尝试解决问题,我尝试删除超时并更改超时时间。此外,我添加了一个 while 循环,以确保端口正在关闭。初步解决方案都没有奏效。

如果您有任何想法,请告诉我。

提前致谢,

克里斯托弗

错误信息:

Traceback (most recent call last):
File "BrainBotTestCode_2.py", line 5, in <module>
sp = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)
File "/home/christophercaligiuri/.local/lib/python2.7/site-       packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/home/christophercaligiuri/.local/lib/python2.7/site-  packages/serial/serialposix.py", line 286, in open
self._update_dtr_state()
File "/home/christophercaligiuri/.local/lib/python2.7/site-  packages/serial/serialposix.py", line 634, in _update_dtr_state
fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
IOError: [Errno 5] Input/output error

代码可以在here或以下找到:

import serial
import time

#open a serial port
sp = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)

#sets up the move forward module 
def moveForward(speed, distance):
    #commands to move forward with the parameters
    numberOfTimes = (distance/5)*2


    for x in range (0,numberOfTimes):
        #lifts up first leg 
        sp.write(("#25 P1600 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#24 P1200 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#25 P2215 T%s\r" %speed).encode())

        time.sleep(.5)

        #lifts up second leg 
        sp.write(("#1 P1535 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#0 P1950 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#1 P2150 T%i\r" %speed).encode())

        time.sleep(.5)

        #lifts up third leg 
        sp.write(("#17 P2500 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#16 P1000 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#17 P1600 T%i\r" %speed).encode())
        time.sleep(.5)

        time.sleep(.5)

        #lifts up forth leg 
        sp.write(("#9 P1600 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#8 P1900 T%i\r" %speed).encode())
        time.sleep(.5)
        sp.write(("#9 P2250 T%i\r" %speed).encode())
        time.sleep(.5)

        time.sleep(.5)

        #moves body forward
        sp.write(("#0 P1425 #8 P1500 #16 P1500 #24 P1600 #26 P1250 T400\r").encode())

        time.sleep(2)

#sets up the move backward module
def moveBackward(speed, distance):
    #commands to move backward with the parameters
    numberOfTimes = distance

    #!!run for x in range command when distance is refined!! 
    #lifts up first leg 
    sp.write(("#25 P1600 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#24 P2200 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#25 P2215 T%s\r" %speed).encode())

    time.sleep(.5)

    #lifts up second leg 
    sp.write(("#1 P1535 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#0 P1000 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#1 P2150 T%i\r" %speed).encode())

    time.sleep(.5)

    #lifts up third leg 
    sp.write(("#17 P2500 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#16 P1900 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#17 P1600 T%i\r" %speed).encode())
    time.sleep(.5)

    time.sleep(.5)

    #lifts up forth leg 
    sp.write(("#9 P1600 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#8 P900 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#9 P2250 T%i\r" %speed).encode())
    time.sleep(.5)

    time.sleep(.5)

    #moves body 
    sp.write(("#0 P1425 #8 P1500 #16 P1500 #24 P1600 #26 P1250 T400\r").encode())

    time.sleep(2)

#sets up the move backward module
def turn(speed, degrees):
    #commands to move backward with the parameters
    numberOfTimes = degrees

    #!!run for x in range command when angle is refined!! 
    #lifts up first leg 
    sp.write(("#25 P1600 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#24 P2200 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#25 P2215 T%s\r" %speed).encode())

    time.sleep(.5)

    #lifts up second leg 
    sp.write(("#1 P1535 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#0 P1000 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#1 P2150 T%i\r" %speed).encode())

    time.sleep(.5)

    #lifts up third leg 
    sp.write(("#17 P2500 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#16 P1900 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#17 P1600 T%i\r" %speed).encode())
    time.sleep(.5)

    time.sleep(.5)

    #lifts up forth leg 
    sp.write(("#9 P1600 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#8 P900 T%i\r" %speed).encode())
    time.sleep(.5)
    sp.write(("#9 P2250 T%i\r" %speed).encode())
    time.sleep(.5)

    time.sleep(.5)

    #moves body 
    sp.write(("#0 P1425 #8 P1500 #16 P1500 #24 P1600 #26 P1250 T400\r").encode())

    time.sleep(2)

#the defult, rest position 
def defultPosition():

    # set the servos to the inital position
    sp.write("#0 P1425 #1 P2150 #2 P1625 #8 P1500 #9 P2300 #10 P1500 #16 P1500 #17 P1600 #18 P1475 #24 P1600 #25 P2215 #26 P1450 T.5\r".encode())

#runs all the modules and gets user input
while True: 
    defultPosition()
    command = raw_input("Enter move forward, move backward, turn or cancel: ")
    defultPosition()
    if command == "cancel":
        break 
    if command == ("move forward") or (command == "move backward"):
        speedInput = input("Enter the desired speed: ")
        distanceInput = input("Enter the number of inches you wish the robot to move (must be a factor of 5): ")
    if command == "turn":
        degrees = input("Enter the number of degrees for the robot to move: ")

    print ("\nINPUTED COMMAND: %s \n" % command)

    if command == "move forward":
        #run the moveForward module

        print "Initiating command\n"

        moveForward(speedInput, distanceInput)

        print "Finsihed command; restarting and waiting for another input \n"

    if command == "move backward":
        #run the moveBackward module

        print "Initiating command\n"

        moveBackward(speedInput, distanceInput)

        print "Finished command; restarting and waiting for another input \n"

    if command == "turn":
        #runs the turn module

        print "Initiating command\n"

        turn(speedInput, degrees)

        print "Finished command; restarting and waiting for another input \n" 

#close serial port
sp.close()
isClosed = sp.is_open
while isClosed == True:
    sp.close
    isClosed = sp.is_open
    print isClosed

【问题讨论】:

  • 你的端口的虚拟化共享是否正确?可以用其他软件测试一下端口吗?
  • 是的,我相当肯定端口的共享是正确的,因为连接独立于 MAC 操作系统——或者至少我是这样认为的。你知道我将如何测试端口的共享吗?我已经在 Windows 计算机上测试了该端口并且它工作正常。
  • 建议将终端仿真器连接到有问题的串行端口,以查看其行为是否符合预期。
  • 我能够在不同的 VM 程序上测试该程序并且它工作正常。感谢您的帮助!

标签: python pyserial


【解决方案1】:

我确定我必须使用以下命令配置端口:sudo chmod 0666 /dev/ttyUSB0。这能够解决我遇到的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多