Pyserial无法在虚拟端口上正常运行 [英] Pyserial does not play well with virtual port

查看:298
本文介绍了Pyserial无法在虚拟端口上正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动机

我想开始学习如何使用python库 Pyserial .似乎是一个非常不错的库,可为很多人使用.我想将其用于即将进行的项目中,在该项目中我必须实现串行通信的自动化.

I want to start leraning how to use the python library Pyserial. It seems like a really nice library that works for a lot of people. I want to use it for an upcoming project in which I have to automate serial communications.

环境

我正在运行Ubuntu 15.04.我正在使用Python 2.7.

I'm running Ubuntu 15.04. I'm using Python 2.7.

设置虚拟端口

我目前没有可以通过串行端口与之通信的设备.我正在使用 socat 应用程序创建两个虚拟端口,这些虚拟端口使用9600的波特率.

I don't currently have a device that I can communicate with over a serial port. I'm using the socat application to create two virtual ports that are connected to each other with a baudrate of 9600.

$ socat -d -d pty,raw,echo=0,b9600 pty,raw,echo=0,b9600
2016/01/16 12:57:51 socat[18255] N PTY is /dev/pts/2
2016/01/16 12:57:51 socat[18255] N PTY is /dev/pts/4
2016/01/16 12:57:51 socat[18255] N starting data transfer loop with FDs [5,5] and [7,7]
$ echo "hello" > /dev/pts/2
$ cat /dev/pts/4
hello

太好了!似乎端口可以正常工作!

Great! It seems like the ports work!

简单的pyserial脚本

我使用pip安装pyserial

I install pyserial using pip

$ sudo pip install pyserial

然后我写了一点serialtest.py

Then I wrote a little serialtest.py

#!/usr/bin/env python
import serial

ser = serial.Serial('/dev/pts/2', 9600)

这是serialtest.py的全部

That is the entirety of serialtest.py

运行脚本并遇到错误

$ python serialtest.py 
Traceback (most recent call last):
  File "serialtest.py", line 4, in <module>
    ser = serial.Serial('/dev/pts/2')
  File "/home/sbl/.local/lib/python2.7/site-packages/serial/serialutil.py", line 180, in __init__
    self.open()
  File "/home/sbl/.local/lib/python2.7/site-packages/serial/serialposix.py", line 311, in open
    self._update_dtr_state()
  File "/home/sbl/.local/lib/python2.7/site-packages/serial/serialposix.py", line 605, in _update_dtr_state
    fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
IOError: [Errno 22] Invalid argument

这是怎么回事?

调试失败

这个家伙说他在使用python 2.6时取得了成功.我无法让Pyserial使用2.6.

This guy said he had success when using python 2.6. I couldn't get Pyserial to work with 2.6.

这家伙遇到麻烦了.我用命令$stty -F /dev/pts/2仔细检查了波特率,并确认它实际上是9600的波特率.

This guy was having trouble with is baudrate. I double check my baudrate with the command $stty -F /dev/pts/2 and confirmed that it was, in fact, at a baudrate of 9600.

这个家伙也声称有波特率问题,并将其归因于他的内核.那是在2012年,所以我认为这不再重要了.

This guy also claims to have problems with baudrate and attributes it to his kernel. That was back in 2012, so I don't think it's relevant anymore.

我的问题

如何使我的serialtest.py脚本正常运行?

How can I get my serialtest.py script to run without error?

推荐答案

为使本问答完整,这是一个解决方案(在Austin Philips的链接中找到):

To make this Q&A complete, this is a solution (as found in the link by Austin Philips):

#!/usr/bin/env python
import serial

ser = serial.Serial('/dev/pts/2', 9600, rtscts=True,dsrdtr=True)

有关更多说明,请参见 PySerial Github问题.

See this PySerial Github issue for more explanation.

这篇关于Pyserial无法在虚拟端口上正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆