bash 在 Linux 中以编程方式断开和重新连接 ttyUSB0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21580750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:28:32  来源:igfitidea点击:

Disconnect and reconnect ttyUSB0 programmatically in Linux

linuxbashserial-porttty

提问by JosephITA

Trying to solve this problem(ttyUSB0 that works properly than stop working after about 1hr)I'm thinking on if disconnecting and reconnecting the usb device could be a good fix.

试图解决这个问题(ttyUSB0 正常工作而不是在大约 1 小时后停止工作)我在考虑是否断开并重新连接 USB 设备可能是一个很好的解决方案。

So, it is possibile to cut down power to the USB device and repower it programmatically (bash)?

那么,是否可以降低 USB 设备的电源并以编程方式 (bash) 为其重新供电?

# lsusb -t                                        
1-1:1.0: No such file or directory                                              
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=musb-hdrc/1p, 480M             
    |__ Port 1: Dev 2, If 0, Class=vend., Driver=, 12M                          
    |__ Port 1: Dev 2, If 1, Class=vend., Driver=cp210x, 12M

On am335x, kernel 3.2.0, ubuntu core armhf.

在 am335x 上,内核 3.2.0,ubuntu 核心 armhf。

[    1.784332] usb 1-1: cp210x converter now attached to ttyUSB0

At the moment I need a complete power cycle to have ttyUSB0back.

目前我需要一个完整的电源循环才能ttyUSB0恢复。

回答by Robert Harvey

This is the solution:

这是解决方案:

  • Find the identity of your usb device.

    # tree /sys/bus/usb/drivers/cp210x/
    /sys/bus/usb/drivers/cp210x/
    |-- 1-1:1.1 -> ../../../../devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1:1.1
    |-- bind
    |-- module -> ../../../../module/cp210x
    |-- remove_id
    |-- uevent
     -- unbind
    
  • 找到您的 USB 设备的身份。

    # tree /sys/bus/usb/drivers/cp210x/
    /sys/bus/usb/drivers/cp210x/
    |-- 1-1:1.1 -> ../../../../devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1:1.1
    |-- bind
    |-- module -> ../../../../module/cp210x
    |-- remove_id
    |-- uevent
     -- unbind
    

So 1-1:1.1is the identifier of my ttyUSB0(it can be discovered also via dmesg).

1-1:1.1我的标识符也是如此ttyUSB0(也可以通过 发现dmesg)。

  • Then, disconnect the device (as root):

    # echo -n "1-1:1.1" > /sys/bus/usb/drivers/cp210x/unbind
    
  • reconnect it

    # echo -n "1-1:1.1" > /sys/bus/usb/drivers/cp210x/bind
    
  • 然后,断开设备(以 root 身份):

    # echo -n "1-1:1.1" > /sys/bus/usb/drivers/cp210x/unbind
    
  • 重新连接

    # echo -n "1-1:1.1" > /sys/bus/usb/drivers/cp210x/bind
    

At this point I had the same device but with a different name, it was now ttyUSB1 instead of ttyUSB0. - To avoid this I added a new rule in /etc/udev/rules.d/by creating a new file named 99-usb-serial.ruleswith this line:

此时我有相同的设备,但名称不同,现在是 ttyUSB1 而不是 ttyUSB0。- 为了避免这种情况,我/etc/udev/rules.d/通过创建一个99-usb-serial.rules以这一行命名的新文件添加了一个新规则:

SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea70", ATTRS{serial}=="002DCFAF", SYMLINK+="sameName", MODE:="0666"

where idVendor, idProductand serial must be the values of your device. This rule will create a new device called sameNamelinked to the ttyUSB*device normally generated from the OS.

其中idVendor,idProduct和 serial 必须是您设备的值。此规则将创建一个名为sameName链接到ttyUSB*通常从操作系统生成的设备的新设备。

回答by Χριστιαν?δη? Βασ?λειο?

As @Robert Harvey Said,

正如@Robert Harvey 所说,

You first need to find our driver that will help you 'unplug and plug' the usb. Type: ls /sys/bus/usb/driverswhich should print something like this: btusb ftdi_sio hub usb usbfs usbhid usbserial_generic uvcvideo. These, are all the drivers for each usb device. Now, lets say mine is ftdi_sio, which is a device i use to program my arduino (atmega328p chip). I am not sure how Your/other usb devices name themselves there. Like, i dont know which of these is my mouse. Now, you can see the driver's commands using: ls /sys/bus/usb/drivers/ftdi_sio/, which will print something like: 1-4:1.0 bind module uevent unbind, Where 1-4:1.0is the device's characteristic code, and the bindand unbindcommand, which are the 'plug' and 'unplug' command respectively.

您首先需要找到我们的驱动程序,它将帮助您“拔出和插入”USB。类型:ls /sys/bus/usb/drivers它应该打印这样的东西:btusb ftdi_sio hub usb usbfs usbhid usbserial_generic uvcvideo。这些,是每个 USB 设备的所有驱动程序。现在,假设我的是ftdi_sio,这是我用来对我的 arduino(atmega328p 芯片)进行编程的设备。我不确定您/其他 USB 设备在那里如何命名。就像,我不知道哪一个是我的鼠标。现在,您可以使用以下命令查看驱动程序的命令: ls /sys/bus/usb/drivers/ftdi_sio/,它将打印如下内容:1-4:1.0 bind module uevent unbind1-4:1.0设备的特征代码在哪里,以及bindandunbind命令,分别是“plug”和“unplug”命令。

Now, if i want to unplug programatically the ftdi usb port, i will type: echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/unbindand, to plug it again: echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/bind

现在,如果我想以编程方式拔下 ftdi usb 端口,我将输入: echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/unbind然后再次插入: echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/bind

Now, we can combine all the commands together, with a ';':

现在,我们可以用“;”将所有命令组合在一起:

echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/unbind ; echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/bind

echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/unbind ; echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/bind