Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以使用Python 2.6访问串行端口,但不能使用相同的代码和端口访问Python 2.7或3.5_Python_Python 2.7_Port_Pyserial - Fatal编程技术网

可以使用Python 2.6访问串行端口,但不能使用相同的代码和端口访问Python 2.7或3.5

可以使用Python 2.6访问串行端口,但不能使用相同的代码和端口访问Python 2.7或3.5,python,python-2.7,port,pyserial,Python,Python 2.7,Port,Pyserial,我能够使用Python2.6.6运行打开串行端口和双向交换数据的脚本。在安装了Python2.7和3.5之后,我收到了相同的错误(IOError:[Errno 110]连接超时),这是在2.6中无法执行的对象所需要的 我使用(makealtinstall)安装了2.7和3.5,然后是(easy_install-2.7-upyserial)和(easy_install-3.5-upyserial) 有人能猜出为什么2.6会打开端口并传递数据,但2.7和3.5都失败了吗 我在CentOS 6.8、内

我能够使用Python2.6.6运行打开串行端口和双向交换数据的脚本。在安装了Python2.7和3.5之后,我收到了相同的错误(IOError:[Errno 110]连接超时),这是在2.6中无法执行的对象所需要的

我使用(makealtinstall)安装了2.7和3.5,然后是(easy_install-2.7-upyserial)和(easy_install-3.5-upyserial)

有人能猜出为什么2.6会打开端口并传递数据,但2.7和3.5都失败了吗

我在CentOS 6.8、内核Linux 2.6.32-642.11.1.el6.x86_64上运行,使用USB到DB9串行电缆,在2.6中没有任何问题

python stest.py
Hello World from serial port running under Python 2.6.6.

python2.7 stest.py
Traceback (most recent call last):
  File "stest.py", line 3, in <module>
    ser = serial.Serial(port='/dev/ttyUSB0',timeout=0,xonxoff=False,rtscts=False,dsrdtr=False)
  File "build/bdist.linux-x86_64/egg/serial/serialutil.py", line 182, in __init__
  File "build/bdist.linux-x86_64/egg/serial/serialposix.py", line 267, in open
  File "build/bdist.linux-x86_64/egg/serial/serialposix.py", line 588, in _update_rts_state
IOError: [Errno 110] Connection timed out


python3.5 stest.py
Traceback (most recent call last):
  File "stest.py", line 3, in <module>
    ser = serial.Serial(port='/dev/ttyUSB0',timeout=0,xonxoff=False,rtscts=False,dsrdtr=False)
  File "/usr/local/lib/python3.5/site-packages/pyserial-3.2.1-py3.5.egg/serial/serialutil.py", line 236, in __init__
  File "/usr/local/lib/python3.5/site-packages/pyserial-3.2.1-py3.5.egg/serial/serialposix.py", line 288, in open
  File "/usr/local/lib/python3.5/site-packages/pyserial-3.2.1-py3.5.egg/serial/serialposix.py", line 605, in _update_rts_state
TimeoutError: [Errno 110] Connection timed out
如前所述,我确实尝试过多次打开端口。我从其他帖子上读到这可能是一个问题。我还将超时时间从0增加到5。还是没有快乐。以下是该版本(假设我了解进行多次尝试的方法):

我认为这可能与内核有关我可以通过将rtscts值从False更改为1来交换数据。不幸的是,这无助于我的最终目标,因为远程设备是三线的,不支持握手(RTS/CTS)。所以,我仍然没有解决办法。现在的问题是: 为什么(rtscts值为False)在2.7和3.5中失败,但在2.6.6中起作用?

工作代码(但对我不可用,因为我的设备不握手):

除了我的发现的发展之外,它确实是CentOS 6.8与pyserial的serialutil.py和serialposix.py的兼容性问题。我可以打开一个串行端口,在rtscts=1的情况下读取/写入数据。如果我在运行时重新配置端口并将rtscts设置为False,那么2.6.6可以很好地处理它。Pythons 2.7和3.5立即发出错误,并中止显示我文章开头显示的相同([Errno 110])错误。如果其他人愿意复制,下面是代码。最好是CentOS用户,因为这可能与操作系统有关

import serial

ser = serial.Serial(port='/dev/ttyUSB0',timeout=0,xonxoff=False,rtscts=1,dsrdtr=False)

# Python 2.6.6, 2.7, and 3.5 will send this following string.
ser.write(b"Hello from CentOS");

# Python 2.7 and 3.5 error-out after this change, but system Python (2.6.6) is okay.
ser.rts = False

while True:
    chunk = ser.read(24)
    if(chunk):
        print(str(chunk) + '\n')

ser.close()

对下一步有什么建议吗?我只是运气不好吗?

你加入拨号组了吗?并尝试:多次运行py2.6代码。是。我是拨号组的一员。如果没有这种访问,即使系统Python尝试也会失败。我甚至尝试了一个循环,尝试了(ser=serial)行20次,每次尝试都因IO错误而失败。如果我在失败后立即尝试系统Python(2.6),端口将打开。20倍循环运行大约需要15秒。我希望时间足够。
pySerial
用于串口操作。如何操作
USB端口
OS
可以将参数设置为USB主机。对于dsgdfg:我不确定是否理解您的评论。我正在成功地使用pyserial进行串行端口传输。我在Python2.6.6中成功地使用了它。我还可以通过minicom和Linux命令行成功地使用串行端口。只是Python2.7和3.5出错了。Py2.6可以操纵一些操作系统级别的操作,但其他版本不能。在使用python之前,请使用
OS
进行准备。如何在Py27-35(至USB主机)上设置串行接入参数?不能这样做,因为等待
准备使用的总线线路。当连接到设备时,如果序列号为XXXX,则对总线进行一些设置。您是否将自己添加到拨号组?并尝试:多次运行py2.6代码。是。我是拨号组的一员。如果没有这种访问,即使系统Python尝试也会失败。我甚至尝试了一个循环,尝试了(ser=serial)行20次,每次尝试都因IO错误而失败。如果我在失败后立即尝试系统Python(2.6),端口将打开。20倍循环运行大约需要15秒。我希望时间足够。
pySerial
用于串口操作。如何操作
USB端口
OS
可以将参数设置为USB主机。对于dsgdfg:我不确定是否理解您的评论。我正在成功地使用pyserial进行串行端口传输。我在Python2.6.6中成功地使用了它。我还可以通过minicom和Linux命令行成功地使用串行端口。只是Python2.7和3.5出错了。Py2.6可以操纵一些操作系统级别的操作,但其他版本不能。在使用python之前,请使用
OS
进行准备。如何在Py27-35(至USB主机)上设置串行接入参数?不能这样做,因为等待
准备使用的总线线路。如果序列号为XXXX,则在安装设备时,对总线线路进行一些设置。
import serial

ser = 0

for i in range (1, 20):
    print("Attempt" + str(i))
    try:
        ser = serial.Serial(port='/dev/ttyUSB0',timeout=5,xonxoff=False,rtscts=False,dsrdtr=False)
        if(ser):
            break
    except:
        print("Failed")


ser.write("Hello from CentOS");
while True:
    chunk = ser.read(24)
    if(chunk != ''):
        print(chunk + '\n')

ser.close()
#!/usr/bin/python3.5
import serial

ser = serial.Serial(port='/dev/ttyUSB0',timeout=0,xonxoff=False,rtscts=1,dsrdtr=False)
ser.write(b"Hello from CentOS");
while True:
  chunk = ser.read(24)
    if(chunk):
        print(str(chunk) + '\n')

ser.close()
import serial

ser = serial.Serial(port='/dev/ttyUSB0',timeout=0,xonxoff=False,rtscts=1,dsrdtr=False)

# Python 2.6.6, 2.7, and 3.5 will send this following string.
ser.write(b"Hello from CentOS");

# Python 2.7 and 3.5 error-out after this change, but system Python (2.6.6) is okay.
ser.rts = False

while True:
    chunk = ser.read(24)
    if(chunk):
        print(str(chunk) + '\n')

ser.close()