无法在 Pyserial 中打开串行连接:“termios.error: (22, “无效参数”)”

发布于 2024-10-28 13:12:19 字数 1091 浏览 2 评论 0原文

我在执行以下代码时遇到问题:

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()

这昨天有效,但我不知道我更改了什么。现在我收到以下错误消息:

    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')

我是 Python 的初学者,无法解读上面的错误消息。我尝试重新安装 Pyserial,但这并没有解决错误。怎么了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ゞ花落谁相伴 2024-11-04 13:12:19

我遇到了同样的问题,尽管我使用的是 5 端口 USB 集线器,其中 3 个不同的 USB 都插入其中,但事实证明集线器无法提供足够的电力。一旦我获得了一个外部供电的USB(不仅仅是从我的计算机端口拔出线路),它就工作得很好。

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.

葬﹪忆之殇 2024-11-04 13:12:19

港口还在吗?我的意思是/dev/tty.FireFly-16CB-SPP。今天它可能有一个新名字......

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

风铃鹿 2024-11-04 13:12:19

试试这个:

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

你不必调用 open() ,除非你手动更改了端口的配置,例如:

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

完成后不要忘记 cles 端口

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

跨年 2024-11-04 13:12:19

重新安装Python并降级到2.6,这解决了一切。

Reinstalled Python and downgraded to 2.6 which solved everything.

埖埖迣鎅 2024-11-04 13:12:19

我有同样的问题。

只需运行 pySerial 示例 (http://sourceforge.net/projects/pyserial/develop) 中的 miniterm.py 即可。

它在 shell 中运行良好: python miniterm -p ttyUSB0 -D
但是当尝试将其加载到 Eclipse 中时。并从那里运行调试。
它给了我:

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')

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')
浮生未歇 2024-11-04 13:12:19

我在 OSX 上遇到了这个问题,问题最终是不支持波特率。将波特率更改为更常见的波特率并且成功了!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文