最近要测一个设备,搜了搜,发现 WSL2 已经有主机串口使用方案了,社区牛人真不少。终于不用每次使用 socat 转发了。
注:以下 Windows 命令需要管理员权限
Windows 安装 USBIPD-WIN 项目
winget install --interactive --exact dorssel.usbipd-win
太慢,下载不了,还是手工下载安装吧。。下载地址
WSL2 安装 linux-tools/hwdata
sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip `ls /usr/lib/linux-tools/*/usbip | tail -n1` 20
Windows 列出所有连接的 USB 设备
usbipd wsl list
BUSID VID:PID DEVICE STATE
1-2 2717:ff40 Redmi 9A Not attached
1-3 27c6:5125 Goodix Fingerprint USB Device Not attached
1-6 13d3:5488 HD Camera, Camera DFU Device Not attached
1-10 8087:0026 英特尔(R) 无线 Bluetooth(R) Not attached
2-1 0bda:8152 Realtek USB FE Family Controller Not attached
2-2 25a4:9311 USB C Video Adaptor Not attached
2-3 1ea7:0064 USB 输入设备 Not attached
2-4 0403:6001 USB Serial Converter Not attached
启动 WSL2
wsl -u ljd
Windows 关联 USB 串口到 WSL2
usbipd wsl attach --busid 2-4
2-4 为上面列出的 BUSID。偶尔会附加不上,反复多试试。
wsl2 中查看 usb 设备
lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
查找 USB 设备对应的设备文件
dmesg | grep -5 FT232
[ 17.078734] usb 1-1: new full-speed USB device number 2 using vhci_hcd
[ 17.168559] vhci_hcd: vhci_device speed not set
[ 17.238554] usb 1-1: SetAddress Request (2) to port 0
[ 17.289763] usb 1-1: New USB device found, idVendor=0403, idProduct=6001, bcdDevice= 6.00
[ 17.292305] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 17.292748] usb 1-1: Product: FT232R USB UART
[ 17.292968] usb 1-1: Manufacturer: FTDI
[ 17.293191] usb 1-1: SerialNumber: AB63OS0E
[ 17.300507] ftdi_sio 1-1:1.0: FTDI USB Serial Device converter detected
[ 17.301155] usb 1-1: Detected FT232RL
[ 17.302566] usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0
[ 49.581429] hv_balloon: Max. dynamic memory size: 8090 MB
串口设备读写权限
sudo chmod a+rw /dev/ttyUSB0
使用完成后,Windows 下分离,或直接物理断开 USB 设备
usbipd wsl detach --busid 2-4
时兼容时不兼容,不知道什么情况。
参考
微软 WSL 使用说明《连接USB设备》
isbipd wiki WSL-support