pySerial:一次打开多个端口
编辑:发现问题:我尝试引用一个变量,但混淆了它的名称,所以我声明了一个新变量。事实证明 pySerial 不限于一次打开一个串行点。
我试图使用以下代码同时打开两个串行端口
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 一次打开一个串行端口是否存在固有限制?
谢谢, TG
编辑:我应该一开始就发布这个
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'
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
)
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?
Thanks,
T.G.
EDIT: I should have posted this to begin with
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有看到代码的上下文,这只是一个猜测。
串行端口在垃圾收集时将被关闭,并且
__del__
已运行。在 CPython 中,如果您的ser0
引用计数在该代码块运行后下降到零,但不知何故ser1
没有,它会出现一个端口关闭的情况当另一个打开时。但发布更多代码!
Without seeing the context of the code, this is only a guess.
Serial ports will be closed when they are garbage collected and
__del__
is run. Uner CPython, if yourser0
reference count drops to zero after that block of code is run, but somehow theser1
doesn't, it will give the appearance of one port closing when the other opens.But post more code!
声明 com 端口打开时引用的变量与 while 条件中检查的变量不匹配。哎呀。
The variables referenced when declaring the com ports open do not match the ones checked in the while condition. Oops.
在代码中,您测试
comm_port0_open
和comm_port1_open
,但使用comm0_port_open = True
和comm1_port_open
设置它们。不同的名字!另一点:不要使用裸露的“例外”,它可以隐藏各种错误。
In your code you test
comm_port0_open
andcomm_port1_open
, but set them withcomm0_port_open = True
andcomm1_port_open
. Different names!Another point: don't use bare 'except', it can hide all kinds of errors.