pySerial:一次打开多个端口 [英] pySerial: opening multiple ports at once
问题描述
编辑:发现问题:我尝试引用一个变量,但混淆了它的名称,所以我声明了一个新变量.事实证明 pySerial 不限于一次打开一个串行点.
EDIT: Found the problem: I tried referencing a variable, but mixed up its name, so instead I declared a new variable. Turns out pySerial is not limited to one open serial point at a time.
我正在尝试使用以下代码同时打开两个串行端口
I'm trying to open two serial ports at once using the following code
ser0 = serial.Serial(
port = port_list[0],
baudrate = 115200,
timeout = 0.1
)
ser1 = serial.Serial(
port = port_list[1],
baudrate = 115200,
timeout = 0.1
)
不过好像我开了第二个,第一个关了.使用 pySerial 一次打开一个串行端口是否存在固有限制?
But it seems that I open the second, the first one closes. Is there an inherent limit to one serial port open at a time using pySerial?
谢谢,T.G.
我应该先发布这个
while not (comm_port0_open and comm_port1_open):
print 'COM ports available:'
port_list = []
i = 0
for port in __EnumSerialPortsWin32():
port_list.append(port[0])
print '%i:' % i, port[0]
i+=1
print 'Connect to which port? (0, 1, 2, ...)'
comm_port_str = sys.stdin.readline()
try:
if len(comm_port_str)>0:
if comm_port0_open:
ser1 = serial.Serial(
port = port_list[int(comm_port_str)],
baudrate = 115200,
timeout = 0.1
)
comm1_port_open = True
print '%s opened' % port_list[int(comm_port_str)]
else:
ser0 = serial.Serial(
port = port_list[int(comm_port_str)],
baudrate = 115200,
timeout = 0.1
)
comm0_port_open = True
print '%s opened' % port_list[int(comm_port_str)]
else:
print 'Empty input'
except:
print 'Failed to open comm port, try again'
推荐答案
在您的代码中测试 comm_port0_open
和 comm_port1_open
,但将它们设置为 comm0_port_open = True
和 comm1_port_open
.不同的名字!
In your code you test comm_port0_open
and comm_port1_open
, but set them with comm0_port_open = True
and comm1_port_open
. Different names!
另外一点:不要使用裸的'except',它可以隐藏各种错误.
Another point: don't use bare 'except', it can hide all kinds of errors.
这篇关于pySerial:一次打开多个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!