Disconnect and reconnect ttyUSB0 programmatically in Linux

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
    

So 1-1:1.1 is the identifier of my ttyUSB0(it can be discovered also via 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
    

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.rules with this line:

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. This rule will create a new device called sameName linked to the ttyUSB* device normally generated from the OS.