简体   繁体   English

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

[英]Disconnect and reconnect ttyUSB0 programmatically in Linux

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 ttyUSB0 back. 目前我需要一个完整的电源循环来恢复ttyUSB0

This is the solution: 这是解决方案:

  • Find the identity of your usb device. 找到您的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.1 is the identifier of my ttyUSB0 (it can be discovered also via dmesg ). 所以1-1:1.1是我的ttyUSB0的标识符(也可以通过dmesg发现)。

  • Then, disconnect the device (as root): 然后,断开设备(以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 

At this point I had the same device but with a different name, it was now ttyUSB1 instead of ttyUSB0. 此时我有相同的设备,但名称不同,现在是ttyUSB1而不是ttyUSB0。 - To avoid this I added a new rule in /etc/udev/rules.d/ by creating a new file named 99-usb-serial.rules with this line: - 为了避免这种情况,我在/etc/udev/rules.d/添加了一条新规则,创建了一个名为99-usb-serial.rules /etc/udev/rules.d/的新文件:

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

where idVendor , idProduct and serial must be the values of your device. 其中idVendoridProduct和串口必须是你的设备的价值。 This rule will create a new device called sameName linked to the ttyUSB* device normally generated from the OS. 此规则将创建一个名为sameName的新设备,该设备链接到通常从OS生成的ttyUSB*设备。

As @Robert Harvey Said, 作为@Robert Harvey Said,

You first need to find our driver that will help you 'unplug and plug' the usb. 您首先需要找到我们的驱动程序,它将帮助您“拔掉并插上”USB。 Type: ls /sys/bus/usb/drivers which should print something like this: btusb ftdi_sio hub usb usbfs usbhid usbserial_generic uvcvideo . 键入: ls /sys/bus/usb/drivers应该打印这样的内容: btusb ftdi_sio hub usb usbfs usbhid usbserial_generic uvcvideo These, are all the drivers for each usb device. 这些都是每个USB设备的驱动程序。 Now, lets say mine is ftdi_sio , which is a device i use to program my arduino (atmega328p chip). 现在,让我们说我的是ftdi_sio ,这是我用来编程我的arduino(atmega328p芯片)的设备。 I am not sure how Your/other usb devices name themselves there. 我不确定你/其他usb设备如何在那里命名。 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.0 is the device's characteristic code, and the bind and unbind command, which are the 'plug' and 'unplug' command respectively. 现在,您可以使用以下命令查看驱动程序命令: ls /sys/bus/usb/drivers/ftdi_sio/ ,它将打印如下内容: 1-4:1.0 bind module uevent unbind ,其中1-4:1.0是设备的特征代码,以及bindunbind命令,分别是'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/unbind and, 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM