ttyusb - yuhannah/skills_map GitHub Wiki

Linux 下串口和串口驱动

ttyS :PC上的串口 ttySAC:板子上Linux的串口 ttyUSB:usb口转串口 ttyS*:外插串口卡

驱动相关说明

如果直接使用串口线,而没有用到USB转串口设备,就不需要安装驱动。 如果使用了USB转串口,一般情况下也不需要安装驱动了,目前linux系统已经包含了该驱动,可以自动识别,亦可通过以下命令查看以便确认是否支持。

若系统中没有相关驱动,且安装驱动失败,一个解决方案是重装系统。利用新系统自带的驱动。

串口相关说明

一般ttyS0对应com1,ttyS1对应com2

以下在root用户下操作:

  1. 查看串口的驱动信息
root@yu-virtual-machine:~# cat /proc/tty/driver/serial
serinfo:1.0 driver revision:
0: uart:16550A port:000003F8 irq:4 tx:8400 rx:2112 CTS|DSR|CD
1: uart:16550A port:000002F8 irq:3 tx:0 rx:0 CTS|DSR|CD
2: uart:unknown port:000003E8 irq:4
...
31: uart:unknown port:00000000 irq:0
root@yu-virtual-machine:~#
  1. 查看模块装载的情况
root@ubuntu:~# lsmod |grep cp210x
cp210x                 24576  0 
usbserial              53248  1 cp210x
root@ubuntu:~#

如果看到类似于上述信息,则表明能正确识别该设备,否则需要安装该设备的驱动。

  1. 查看系统的一些信息引用

    root@ubuntu:~# dmesg | tail -f
    [  810.244722] usb 2-2.1: Product: CP2102 USB to UART Bridge Controller
    [  810.244723] usb 2-2.1: Manufacturer: Silicon Labs
    [  810.244723] usb 2-2.1: SerialNumber: 0001
    [  810.781097] usbcore: registered new interface driver usbserial
    [  810.781775] usbcore: registered new interface driver usbserial_generic
    [  810.782331] usbserial: USB Serial support registered for generic
    [  810.798624] usbcore: registered new interface driver cp210x
    [  810.798912] usbserial: USB Serial support registered for cp210x
    [  810.799154] cp210x 2-2.1:1.0: cp210x converter detected
    [  810.799338] usb 2-2.1: cp210x converter now attached to ttyUSB0
    root@ubuntu:~#

    如果看到类似上述信息,可知 usb 转串口芯片 CP2102 的驱动已经注册加载,对应系统设备是 /dev/ttyUSB0

  2. 查看串口的波特率

root@ubuntu:~# stty -a -F /dev/ttyUSB0
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
root@ubuntu:~#
  1. 查看串口的权限信息和名称
root@yu-virtual-machine:~# ls -l /dev/tty*
crw-rw-rw- 1 root tty     5,  0  6月  6 14:07 /dev/tty
crw--w---- 1 root tty     4,  0  6月  6 13:49 /dev/tty0
…(此处省略)
crw--w---- 1 root tty     4,  9  6月  6 13:49 /dev/tty9
crw------- 1 root root    5,  3  6月  6 13:49 /dev/ttyprintk
crw-rw---- 1 root dialout 4, 64  6月  6 14:53 /dev/ttyS0
…(此处省略)
crw-rw---- 1 root dialout 4, 73  6月  6 13:49 /dev/ttyS9
crw-rw---- 1 root dialout 188,  0 Jun  6 02:14 /dev/ttyUSB0
root@yu-virtual-machine:~#

所有当前使用的串口设备都会在此显示。如果有USB串口,就会有类似 ttyUSB* 的一行。

  1. 查看串口设备:
root@ubuntu:~# dmesg | grep ttyUSB
[  810.799338] usb 2-2.1: cp210x converter now attached to ttyUSB0
root@ubuntu:~#

插上 USB 转串口,在终端输入以上命令,如果出现连接成功信息,则说明 ubuntu 系统已经识别该设备了。并且可以看见相对应的芯片型号。cp210x 对应的 ttyUSB0。

  1. 查看串口是否可用,对串口发送数据
root@yu-virtual-machine:~# echo lyjie126 > /dev/ttyS0

比如对com1口,echo lyjie126 > /dev/ttyS0

串口不通时,查看是否用的交叉线。

⚠️ **GitHub.com Fallback** ⚠️