使用 pySerial 在 Python 下重新连接到 USB 端口时,如何避免在 ttyUSB0 和 ttyUSB1 之间翻转? [英] How can I avoid flipping between ttyUSB0 and ttyUSB1 when reconnecting to USB port under Python using pySerial?

查看:109
本文介绍了使用 pySerial 在 Python 下重新连接到 USB 端口时,如何避免在 ttyUSB0 和 ttyUSB1 之间翻转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个串行 Python 程序,Linux 环境(Raspbian/Raspberry Pi),它通过 USB 到串行适配器使用串行端口.我需要处理用户拔掉USB适配器然后重新插入的情况.

I have a serial Python program, Linux environment (Raspbian / Raspberry Pi), that uses a serial port via a USB-to-serial adapter. I need to handle a situation when the user unplugs the USB adapter and then reinsert it.

问题是,在重新连接时,ttyUSB0 变成了 ttyUSB1,因此不再找到该端口.但是,如果我停止 Python 程序(键盘中断)并再次拔下并重新插入 USB 适配器,则端口会返回到 ttyUSB0(这样我就可以重新开始).这只有在 Python 程序停止时才会发生.

The problem is that, on reconnect, the ttyUSB0 becomes ttyUSB1 and so the port is no longer found. However, if I stop the Python program (keyboard interrupt) and again unplug and reinsert the USB adapter, then the port goes back to ttyUSB0 (and so I can start over again). This can happen only when the Python program is stopped.

为了在 ttyUSB0 不再被找到时使用 ttyUSB1,我在触发器模式下测试了该程序(它似乎正在运行),反之亦然, 使用 ttyUSB0 以防 ttyUSB1 不再被找到,等等,但这对我来说似乎是一个奇怪的解决方案.

I tested the program in a flip-flop mode (and it seems to be working) in order to use ttyUSB1 when ttyUSB0 is no longer found and then vice versa, use ttyUSB0 back in case ttyUSB1 is no longer found, etc., but this looks like a weird solution to me.

是否有更好的方法强制 pySerial忘记"?是否曾经连接到 ttyUSB0 以防出错并在程序仍在运行时将当前端口释放给系统?

Is there a better way to force pySerial to "forget" it has ever been connected to ttyUSB0 in case of error and release the current port to the system while the program is still running?

这是一个有效的触发器测试程序:

Here is a working flip-flop test program:

import serial
import time

p = "/dev/ttyUSB0"

while True:
    error_flag = False
    try:
        s = serial.Serial(port=p, baudrate=9600, bytesize=8, parity="N", stopbits=1, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None)
    except Exception as e:
        error_flag = True
        if "ttyUSB0" in str(e):
            p = "/dev/ttyUSB1"
            print ("port is now", p)
        elif "ttyUSB1" in str(e):
            p = "/dev/ttyUSB0"
            print ("port is now", p)
        else:
            print (e)   # none of the above

    # if not error_flag, do whatever, etc.

    time.sleep(1)

推荐答案

您可以尝试创建一个 udev 规则,该规则将创建到该 USB 设备的 符号链接,然后您就可以使用诸如 /dev/myUSB 之类的东西,对于那个特定的 USB 设备来说,它总是保持不变.

You could try creating a udev rule that would create a symbolic link to that USB device and then you would be able to use something like /dev/myUSB that would always stay the same for that specific USB device.

首先,您需要找到 USB 驱动器的一些识别信息.输入 lsusb 应该会显示一些如下所示的信息:

First, you will need to find some identifying information for the USB drive. Typing in lsusb should display some information that looks like:

Bus 001 Device 004: ID 0403:6001 Future Technology Devices International

在此示例中,0403 是供应商 ID,6001 是产品 ID.

In this example 0403 is the Vendor Id and 6001 is the Product Id.

创建一个名为 99_usbdevice.rules 的文件(我认为名称不重要,重要的是目录):

Create a file named 99_usbdevice.rules (I don't think the name matters, just the directory):

sudo nano /etc/udev/rules.d/99_usbdevices.rules

请注意,上面的目录可能特定于 Raspbian.

Note that the directory above may be specific to Raspbian.

将下面的行复制/粘贴到文件中并保存:

Copy/paste the line below into the file and save it:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="myUSB"

重新启动您的 Raspberry Pi 或拔下 USB 并重新插入.现在应该有一个 /dev/myUSB 条目,您可以像使用 ttyUSB# 条目一样使用它.

Restart your Raspberry Pi or unplug the USB and reinsert it. There should now be a /dev/myUSB entry that you can use the same way you would the ttyUSB# entry.

这篇关于使用 pySerial 在 Python 下重新连接到 USB 端口时,如何避免在 ttyUSB0 和 ttyUSB1 之间翻转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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