如何解决Ubuntu 22.04中ch34x串口无法使用的问题?

问题背景

在去年RDC2022上很幸运抽中了纪念板柿饼M7。在此感谢RT-Thread!!!本文介绍了Ubuntu22.04下开发RT Smart遇到的无法使用ch34x串口问题。


问题描述

Ubuntu22.04无法使用ch34x串口
使用lsusb命令可以看到有Bus 001 Device 005: ID 1a86:7523 QinHeng Electronics CH340 serial converter是能识别出ch34x设备


原因分析

  1. Ubuntu22.04自带的ch34x驱动版本过老
    使用ls /lib/modules/$(uname -r)/kernel/drivers/usb/serial命令查看Ubutnu自带驱动,找到ch341.ko发现是2013年的文件…(无语~)先把旧驱动删除sudo rm ch341.ko
    找到一篇相似的文章ubuntu20.4系统安装CH340驱动并使用串口调试助手看来不是我一个人遇到相同的问题…
    2.brltty进程占用了串口
    使用sudo dmesg | grep tty命令可以看到
    [ 0.223411] printk: console [tty0] enabled
    [ 44.099146] usb 1-1: ch341-uart converter now attached to ttyUSB0
    [ 44.673749] usb 1-1: usbfs: interface 0 claimed by ch34x while ‘brltty’ sets config #1
    [ 44.675063] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0

解决过程

WCH官网下载最新的驱动CH341SER_LINUX.ZIP

解压驱动

unzip CH341SER_LINUX_ZIP 得到三个文件ch34x.c Makefile readme.txt

// ChangeLog 
// 1.0 - 1.1   modified to solve transmition between ch341 and ch341
// 1.1 - 1.2   Support high Linux kernel
Instructions

Note: 1.Please run followed executable programs as root privilege
      2.Current Driver support versions of linux kernel range from 2.6.25 to 3.13.x
      3.Current Driver support 32bits and 64bits linux systems

Usage:
	(load or unload linux driver of CH34x)
	//compile 
	#make
	//load ch34x chips driver
	#make load
	//unload ch34x chips driver
	#make unload
// 1.2 - 1.3 Fix some bugs			

编译安装驱动

readme.txt里面介绍到驱动的加载方式,但注意到第7行写着支持的内核版本从2.6.25 to 3.13.x隐约感觉问题不妙
敲入make命令后,果然出现了错误(这年头官网的驱动也不靠谱哇)

make -C /lib/modules/5.15.0-58-generic/build  M=/home/$USER/Documents/CH341SER_LINUX  
make[1]: Entering directory '/usr/src/linux-headers-5.15.0-58-generic'
  CC [M]  /home/$USER/Documents/CH341SER_LINUX/ch34x.o
/home/$USER/Documents/CH341SER_LINUX/ch34x.c: In function ‘ch34x_close’:
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:591:9: error: unknown type name ‘wait_queue_t’; did you mean ‘wait_event’?
  591 |         wait_queue_t wait;
      |         ^~~~~~~~~~~~
      |         wait_event
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:591:22: warning: unused variable ‘wait’ [-Wunused-variable]
  591 |         wait_queue_t wait;
      |                      ^~~~
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:590:14: warning: unused variable ‘timeout’ [-Wunused-variable]
  590 |         long timeout;
      |              ^~~~~~~
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:589:13: warning: unused variable ‘bps’ [-Wunused-variable]
  589 |         int bps;
      |             ^~~
/home/$USER/Documents/CH341SER_LINUX/ch34x.c: At top level:
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:1297:27: error: initialization of ‘unsigned int (*)(struct tty_struct *)’ from incompatible pointer type ‘int (*)(struct tty_struct *)’ [-Werror=incompatible-pointer-types]
 1297 |         .write_room     = ch34x_write_room,
      |                           ^~~~~~~~~~~~~~~~
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:1297:27: note: (near initialization for ‘ch34x_device.write_room’)
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:1298:28: error: initialization of ‘unsigned int (*)(struct tty_struct *)’ from incompatible pointer type ‘int (*)(struct tty_struct *)’ [-Werror=incompatible-pointer-types]
 1298 |         .chars_in_buffer = ch34x_chars_in_buffer,
      |                            ^~~~~~~~~~~~~~~~~~~~~
/home/$USER/Documents/CH341SER_LINUX/ch34x.c:1298:28: note: (near initialization for ‘ch34x_device.chars_in_buffer’)
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:297: /home/$USER/Documents/CH341SER_LINUX/ch34x.o] Error 1
make[1]: *** [Makefile:1902: /home/$USER/Documents/CH341SER_LINUX] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.15.0-58-generic'
make: *** [Makefile:5: default] Error 2

uname -r 可查看操作系统的发行版号,获得Ubuntu22.04的Linux版本是5.15.0-58-generic
Linux kernel source code v5.15查看ch341.c的源代码,直接复制到CH341SER_LINUX/ch34x.c中,重新make,此次通过。
在目录执行sudo make load命令,出现以下错误

modprobe usbserial
insmod ch34x.ko
insmod: ERROR: could not insert module ch34x.ko: File exists
make: *** [Makefile:10: load] Error 1

将目录中生成ch34x.ko文件复制到/lib/modules/$(uname -r)/kernel/drivers/usb/serial下,使用lsmod查看模块,发现已识别。

Module                  Size  Used by
ch34x                  24576  0
usbserial              57344  1 ch34x

测试

正常到达这步就可以与开发板通信了,但是配置完minicomminicom: cannot open /dev/ttyUSB0: No such file or directory
这时查看/dev/ttyUSB*发现没有此文件

zsh: no matches found: /dev/ttyUSB*

使用dmesg查找原因sudo dmesg | grep tty

[    0.223411] printk: console [tty0] enabled
[   44.099146] usb 1-1: ch341-uart converter now attached to ttyUSB0
[   44.673749] usb 1-1: usbfs: interface 0 claimed by ch34x while 'brltty' sets config #1
[   44.675063] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0

这里出现了brltty进程,通过官网得知:

BRLTTY is a background process (daemon) providing access to the Linux/Unix console (when in text mode) for a blind person using a refreshable braille display.
brltty是一个后台进程(守护进程),为盲人提供对Linux/Unix控制台的访问(当处于文本模式时),使用可刷新盲文显示。

直接删除sudo apt remove brltty

重新连接开发板ls /dev/ttyUSB*这时已经出现了/dev/ttyUSB0这个设备!!!
此时sudo minicom -c on可正常使用minicom与开发板进行通信


物联沃分享整理
物联沃-IOTWORD物联网 » 如何解决Ubuntu 22.04中ch34x串口无法使用的问题?

发表评论