微信網(wǎng)站全稱廚師培訓(xùn)機(jī)構(gòu) 廚師短期培訓(xùn)班
Linux(Ubuntu 22.04)系統(tǒng)中固定串口
文章目錄
- 前言
- 正文
- 查看linux串口信息
- 修改udev固化串口
- 校驗(yàn)是否修改完成
- 注意
前言
在Linux系統(tǒng)中固定串口(通常指的是串行通信接口,如/dev/ttyS0或/dev/ttyUSB0)的原因有幾個(gè)方面:
- 穩(wěn)定性與可靠性:
固定串口可以確保每次重啟系統(tǒng)后,特定的硬件設(shè)備總是映射到相同的串行端口。這對(duì)于需要穩(wěn)定連接的應(yīng)用程序特別重要,比如工業(yè)自動(dòng)化、遠(yuǎn)程監(jiān)控和數(shù)據(jù)采集系統(tǒng)。 - 簡(jiǎn)化配置:
當(dāng)串口被固定后,應(yīng)用程序或服務(wù)的配置文件可以使用固定的設(shè)備路徑來(lái)引用相應(yīng)的串行端口。這使得配置過(guò)程更加簡(jiǎn)單且易于管理。 - 避免沖突:
有些情況下,不同的外部設(shè)備可能會(huì)連接到同一類型的串口上。固定串口可以確保每個(gè)設(shè)備都有一個(gè)明確的端口分配,從而避免端口沖突。 - 便于調(diào)試和維護(hù):
在開發(fā)過(guò)程中,如果串口是固定的,那么當(dāng)出現(xiàn)問(wèn)題時(shí),開發(fā)者可以更快地定位問(wèn)題所在的串口,進(jìn)而進(jìn)行調(diào)試。 - 自動(dòng)化腳本:
對(duì)于依賴于特定串口的自動(dòng)化腳本或任務(wù),固定串口可以讓這些腳本更加可靠。
正文
在Linux中,可以通過(guò)udev規(guī)則來(lái)實(shí)現(xiàn)串口的固定。udev是一個(gè)設(shè)備管理器,它可以基于設(shè)備的屬性來(lái)設(shè)置規(guī)則,從而將設(shè)備綁定到特定的設(shè)備節(jié)點(diǎn)上。例如,你可以創(chuàng)建一個(gè)udev規(guī)則文件,通過(guò)識(shí)別串口設(shè)備的唯一標(biāo)識(shí)符(如USB設(shè)備的ID_VENDOR和ID_MODEL),確保它始終被映射到同一個(gè)設(shè)備節(jié)點(diǎn)(如/dev/ttyUSB0)。這樣,在設(shè)備插入后,udev就會(huì)根據(jù)規(guī)則自動(dòng)為該設(shè)備分配預(yù)先定義好的設(shè)備節(jié)點(diǎn)名。
查看linux串口信息
方法一:使用lsusb查看信息
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 0573:1573 Zoran Co. Personal Media Division (Nogatech) USB Audio and HID
Bus 001 Device 003: ID 0bda:c821 Realtek Semiconductor Corp. Bluetooth Radio
# 下邊這個(gè)是usb串口設(shè)備
Bus 001 Device 002: ID 1a86:7523 QinHeng Electronics CH340 serial converter
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
這里idVendor是1a86,idProduct是7523
方法二:使用dmesg命令
$ sudo dmesg | grep tty
[ 0.114228] printk: console [tty0] enabled
[ 2.802954] usb 1-4: ch341-uart converter now attached to ttyUSB0
確定串口為ttyUSB0后使用udevadm,同樣可獲得idVendor、idProduct
$ udevadm info -a -p /sys/class/tty/ttyUSB0
# 下邊是詳細(xì)信息
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/ttyUSB0/tty/ttyUSB0':KERNEL=="ttyUSB0"SUBSYSTEM=="tty"DRIVER==""ATTR{power/async}=="disabled"ATTR{power/control}=="auto"ATTR{power/runtime_active_kids}=="0"ATTR{power/runtime_active_time}=="0"ATTR{power/runtime_enabled}=="disabled"ATTR{power/runtime_status}=="unsupported"ATTR{power/runtime_suspended_time}=="0"ATTR{power/runtime_usage}=="0"looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/ttyUSB0':KERNELS=="ttyUSB0"SUBSYSTEMS=="usb-serial"DRIVERS=="ch341-uart"ATTRS{port_number}=="0"ATTRS{power/async}=="enabled"ATTRS{power/control}=="auto"ATTRS{power/runtime_active_kids}=="0"ATTRS{power/runtime_active_time}=="0"ATTRS{power/runtime_enabled}=="disabled"ATTRS{power/runtime_status}=="unsupported"ATTRS{power/runtime_suspended_time}=="0"ATTRS{power/runtime_usage}=="0"...looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-4':KERNELS=="1-4"SUBSYSTEMS=="usb"DRIVERS=="usb"ATTRS{authorized}=="1"ATTRS{avoid_reset_quirk}=="0"ATTRS{bConfigurationValue}=="1"ATTRS{bDeviceClass}=="ff"ATTRS{bDeviceProtocol}=="00"ATTRS{bDeviceSubClass}=="00"ATTRS{bMaxPacketSize0}=="8"ATTRS{bMaxPower}=="98mA"ATTRS{bNumConfigurations}=="1"ATTRS{bNumInterfaces}==" 1"ATTRS{bcdDevice}=="0264"ATTRS{bmAttributes}=="80"ATTRS{busnum}=="1"ATTRS{configuration}==""ATTRS{devnum}=="2"ATTRS{devpath}=="4"# 這里idVendor是1a86,idProduct是7523ATTRS{idProduct}=="7523"ATTRS{idVendor}=="1a86"ATTRS{ltm_capable}=="no"ATTRS{maxchild}=="0"ATTRS{physical_location/dock}=="no"ATTRS{physical_location/horizontal_position}=="left"ATTRS{physical_location/lid}=="no"ATTRS{physical_location/panel}=="top"ATTRS{physical_location/vertical_position}=="upper"ATTRS{power/active_duration}=="9948388"ATTRS{power/async}=="enabled"ATTRS{power/autosuspend}=="2"ATTRS{power/autosuspend_delay_ms}=="2000"ATTRS{power/connected_duration}=="9948388"ATTRS{power/control}=="on"ATTRS{power/level}=="on"ATTRS{power/persist}=="1"ATTRS{power/runtime_active_kids}=="1"ATTRS{power/runtime_active_time}=="9948108"ATTRS{power/runtime_enabled}=="forbidden"ATTRS{power/runtime_status}=="active"ATTRS{power/runtime_suspended_time}=="0"ATTRS{power/runtime_usage}=="1"ATTRS{product}=="USB Serial"ATTRS{quirks}=="0x0"ATTRS{removable}=="fixed"ATTRS{rx_lanes}=="1"ATTRS{speed}=="12"ATTRS{tx_lanes}=="1"ATTRS{urbnum}=="469"ATTRS{version}==" 1.10"
修改udev固化串口
$ sudo touch /etc/udev/rules.d/70-serial_usb.rules$ sudo vim /etc/udev/rules.d/70-serial_usb.rules
ACTION=="add", KERNEL=="ttyUSB*", ATTRS{idProduct}=="7523", ATTRS{idVendor}=="1a86", MODE:="0777", SYMLINK+="serial_usb"
修改完成后重新加載生效:
$ sudo udevadm control --reload
重新插拔usb串口轉(zhuǎn)換器即可
校驗(yàn)是否修改完成
$ ls -al /dev/serial_usb lrwxrwxrwx 1 root root 7 7月 25 14:37 /dev/serial_usb -> ttyUSB0
可以看到自動(dòng)并聯(lián)到ttyUSB0,現(xiàn)在通過(guò)/dev/serial_usb
即可訪問(wèn)串口。
注意
如果sudo dmesg | grep tty
發(fā)現(xiàn)串口一連接就斷開可以參考如下12:
# 卸載沖突的brltty軟件
sudo apt-get remove brltty
理論上應(yīng)該可以正常使用了。
virtualbox 虛擬機(jī)ubuntu22 識(shí)別不了CH340串口_ubuntu 不識(shí)別ch340-CSDN博客 ??
Ubuntu22.04 | 串口設(shè)備/dev/ttyUSB0無(wú)法發(fā)現(xiàn)_ubuntu串口不顯示-CSDN博客 ??