pySerial:一次打开多个端口

发布于 2024-11-18 22:45:18 字数 1899 浏览 3 评论 0原文

编辑:发现问题:我尝试引用一个变量,但混淆了它的名称,所以我声明了一个新变量。事实证明 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 技术交流群。

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

发布评论

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

评论(3

风苍溪 2024-11-25 22:45:18

如果没有看到代码的上下文,这只是一个猜测。

串行端口在垃圾收集时将被关闭,并且 __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 your ser0 reference count drops to zero after that block of code is run, but somehow the ser1 doesn't, it will give the appearance of one port closing when the other opens.

But post more code!

童话 2024-11-25 22:45:18

声明 com 端口打开时引用的变量与 while 条件中检查的变量不匹配。哎呀。

The variables referenced when declaring the com ports open do not match the ones checked in the while condition. Oops.

岁月如刀 2024-11-25 22:45:18

在代码中,您测试 comm_port0_opencomm_port1_open,但使用 comm0_port_open = Truecomm1_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!

Another point: don't use bare 'except', it can hide all kinds of errors.

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