# USB 裝置綁定特定的 Device Port
written by 周文荏
contact: justinimbus.c@nycu.edu.tw
---
How about using /dev/serial/by-path/

It's also a fix ID of serial port, seems to be much easier
@OPhoat6bQ06L3fD9DBb-8Q
> [name=eason27271563]
### 使用情境
在我們 AUV 上的 Rpi 會需要接到 4 個裝置,分別為 2 個 1D sonar 、 Ping360 、 rosserial 通訊的 USB to ttl UART 模組,會分別對應到 4 個 USB 的 device port。
但在每次開機時,我們都無法得知每一個 device port 為哪一個裝置,如下圖。

如果透過程式去測試會相當耗時,也較難以確認是硬體 or 軟體的問題,因此我們需要:
1. 知道哪個 Device Port 為哪個裝置
2. 將這些裝置分別綁定對應的 Device Port
---
### Tutorial
- ##### Installation Requirements
```
$ sudo apt-get install udev
```
- ##### List USB Serial Devices
```
$ ls /dev/ttyUSB*
```
- ##### List the bus and port number of your USB device
```
$ dmesg | grep ttyUSB
```

> ##### Device listed:
> /dev/ttyUSB0 => ping360
> /dev/ttyUSB1 => USB to ttl Module (UART)
> /dev/ttyUSB2 => 1D sonar
> /dev/ttyUSB3 => 1D sonar
- ##### Get USB device path
Approach 1:
```
$ udevadm info --name=/dev/ttyUSB0 --attribute-walk
```

Approach 2:
```
$ dmesg | grep ttyUSB
```

> **Device path:**
>
> /dev/ttyUSB0 (ping360): 1-1.4
> /dev/ttyUSB1 (USB to ttl Module): 1-1.2.3
> /dev/ttyUSB2 (1D sonar): 1-1.1.2
> /dev/ttyUSB3 (1D sonar): 1-1.1.3
- ##### Add Device path into udev rules
```
$ sudo vim /etc/udev/rules.d/10-usb-serial.rules
```
```bash=
# in /etc/udev/rules.d/10-usb-serial.rules
SUBSYSTEM=="tty", KERNELS=="1-1.4", SYMLINK+="ttyUSB-ping360"
SUBSYSTEM=="tty", KERNELS=="1-1.2.3", SYMLINK+="ttyUSB-rosserial"
SUBSYSTEM=="tty", KERNELS=="1-1.1.2", SYMLINK+="ttyUSB-ping1D-1"
SUBSYSTEM=="tty", KERNELS=="1-1.1.3", SYMLINK+="ttyUSB-ping1D-2"
```
> **Parameters:**
> KERNELS==${Device Path}
>
> SYMLINK+=${Name set for device}
- ##### Reload udev rules to take effect
```
$ sudo udevadm trigger
```
- ##### Validate
```
$ ls -l /dev/ttyUSB*
```

- ##### Use the name you defined as port name
example:
```
$ rosrun rosserial_python serial_node.py
_baud:= 115200 _port:=/dev/ttyUSB-rosserial
```
### 注意事項
- 這些設定 需要裝置在固定的 USB 孔位
- 若更改孔位,則需重新設定