Can't open serial connection in Pyserial: “termios.error: (22, 'Invalid argument') ”

牧云@^-^@ 提交于 2019-12-01 19:51:39

问题


I'm having problem executing the following code:

import serial

ser = serial.Serial(
    port='/dev/tty.FireFly-16CB-SPP',
    baudrate=115200,
    #parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

ser.open()
ser.isOpen()

This worked yesterday, and I don't know what I changed. Now I get the following error message:

    Traceback (most recent call last):
  File "main.py", line 32, in <module>
    bytesize=serial.EIGHTBITS
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialutil.py", line 260, in __init__
    self.open()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py", line 280, in open
    self._reconfigurePort()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py", line 409, in _reconfigurePort
    termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
termios.error: (22, 'Invalid argument')

I'm a complete beginner to Python, and can't decipher the error message above. I've tried reinstalling Pyserial, but that didn't fix the error. What is wrong?


回答1:


Is the port still there? I meen /dev/tty.FireFly-16CB-SPP. It could have a new name today...




回答2:


Try this :

ser = serial.Serial('/dev/tty.FireFly-16CB-SPP',115200)
print "port is open" if ser.isOpen() else "port is closed"

you dont have to call open() unless you changed the configuration of the port manually eg:

ser = serial.Serial()
ser.baudrate = 19200
ser.port = 0
ser.open()

and don't forget to cles the port when you are done with it




回答3:


Reinstalled Python and downgraded to 2.6 which solved everything.




回答4:


I have the same problem.

Just run the miniterm.py from pySerial examples (http://sourceforge.net/projects/pyserial/develop) .

It ran fine from shell: python miniterm -p ttyUSB0 -D but when tried to load it into Eclipse. and run debug from there. it gave me:

pydev debugger: starting
Traceback (most recent call last):
  File "/eclipse/plugins/org.python.pydev.debug_2.0.0.2011040403/pysrc/pydevd.py", line 1134, in <module>
    debugger.run(setup['file'], None, None)
  File "/eclipse/plugins/org.python.pydev.debug_2.0.0.2011040403/pysrc/pydevd.py", line 918, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "/root/workspace/pyserial/src/examples/miniterm.py", line 120, in <module>
    console.setup()
  File "/root/workspace/pyserial/src/examples/miniterm.py", line 101, in setup
    self.old = termios.tcgetattr(self.fd)
termios.error: (22, 'Invalid argument')



回答5:


I had this problem on OSX, and the problem ended up being the baud rate was not supported. Changed the baud rate to something more common and it worked!




回答6:


I had the same issue, though I was using a 5 port USB hub with 3 different USBs all plugged into it, and it turned out the hub wasn't providing enough power. Once I got an externally powered USB (one that wasn't just pulling off the line from my computer's port) it worked just fine.



来源:https://stackoverflow.com/questions/5496195/cant-open-serial-connection-in-pyserial-termios-error-22-invalid-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!