• CH340系列Linux驱动安装


    CH340系列Linux驱动安装


    如需转载请标明出处:http://blog.csdn.net/itas109
    技术交流:129518033

    环境:

    系统:Ubuntu 20.04
    linux内核版本:5.15.0
    CH340版本:CH340G
    CH340 linux驱动版本:1.5 (2018-03-18)
    
    • 1
    • 2
    • 3
    • 4

    前言

    CH340系列USB转串口芯片是南京沁恒微电子股份有限公司的一款国产芯片,其价格和稳定性都非常不错。其驱动也支持windows、linux、macos和android等系统。

    1. 下载驱动

    https://www.wch.cn/download/CH341SER_LINUX_ZIP.html

    CH340系列USB转串口LINUX驱动程序,支持CH340G,CH340T,CH340C,CH340N, CH340K, CH340E,CH340B,CH341A,CH341F, CH341T,CH341B,CH341C,CH341U等。

    2. 编译CH340驱动源码

    2.1 修改源码

    • 头文件增加#include
    • 注释掉591行// wait_queue_t wait;
    • 修改938行ch34x_write_room函数返回值为unsigned int
    • 修改964行ch34x_chars_in_buffer函数返回值为unsigned int
    ---
     ch34x.c | 8 ++++----
     1 file changed, 4 insertions(+), 4 deletions(-)
    
    diff --git a/ch34x.c b/ch34x.c
    index 2fcf7f0..95d8acf 100644
    --- a/ch34x.c
    +++ b/ch34x.c
    @@ -30,7 +30,7 @@
     //#include 
     #include 
     #include 
    -
    +#include 
     #define DRIVER_DESC		"WCH CH34x USB to serial adaptor driver"
     #define DRIVER_AUTHOR	""
     
    @@ -588,7 +588,7 @@ static void ch34x_close( struct usb_serial_port *port,
     	unsigned int c_cflag;
     	int bps;
     	long timeout;
    -	wait_queue_t wait;
    +	// wait_queue_t wait;
     
     #if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
     	dbg_ch34x("%s - port:%d", __func__, port->number);
    @@ -935,7 +935,7 @@ static int ch34x_write( struct usb_serial_port *port,
     }
     
     #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
    -static int ch34x_write_room( struct tty_struct *tty )
    +static unsigned int ch34x_write_room( struct tty_struct *tty )
     {
     	struct usb_serial_port *port = tty->driver_data;
     #else
    @@ -961,7 +961,7 @@ static int ch34x_write_room( struct usb_serial_port *port )
     }
     
     #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
    -static int ch34x_chars_in_buffer( struct tty_struct *tty )
    +static unsigned int ch34x_chars_in_buffer( struct tty_struct *tty )
     {
     	struct usb_serial_port *port = tty->driver_data;
     #else
    -- 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    2.2 编译

    $ make
    make -C /lib/modules/5.15.0-52-generic/build  M=/home/dev/CH341SER_LINUX  
    make[1]: 进入目录“/usr/src/linux-headers-5.15.0-52-generic”
      CC [M]  /home/dev/CH341SER_LINUX/ch34x.o
      MODPOST /home/dev/CH341SER_LINUX/Module.symvers
      CC [M]  /home/dev/CH341SER_LINUX/ch34x.mod.o
      LD [M]  /home/dev/CH341SER_LINUX/ch34x.ko
      BTF [M] /home/dev/CH341SER_LINUX/ch34x.ko
    Skipping BTF generation for /home/dev/CH341SER_LINUX/ch34x.ko due to unavailability of vmlinux
    make[1]: 离开目录“/usr/src/linux-headers-5.15.0-52-generic”
    
    $ ls
    ch34x.c   ch34x.mod    ch34x.mod.o  Makefile       Module.symvers
    ch34x.ko  ch34x.mod.c  ch34x.o      modules.order  readme.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.3 安装

    sudo make load
    sudo cp ch34x.ko /lib/modules/`uname -r`/kernel/drivers/usb/serial
    sudo depmod
    
    • 1
    • 2
    • 3

    2.4 查看usb设备

    $ lsusb 
    Bus 002 Device 008: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
    ...
    
    • 1
    • 2
    • 3

    3. 使用CSerialPort进行回环测试

    $ ./CSerialPortDemoNoGui 
    Version: https://github.com/itas109/CSerialPort - V4.2.1.221107
    
    availableFriendlyPorts: 
    1 - /dev/ttyUSB0 
    2 - /dev/ttyS0  
    
    Please Input The Index Of Port(1 - 2)
    1
    Port Name: /dev/ttyUSB0
    Open /dev/ttyUSB0 Success
    /dev/ttyUSB0 - Count: 1, Length: 12, Str: 12345itas109, Hex: 0x31 0x32 0x33 0x34 0x35 0x69 0x74 0x61 0x73 0x31 0x30 0x39
    /dev/ttyUSB0 - Count: 2, Length: 12, Str: 12345itas109, Hex: 0x31 0x32 0x33 0x34 0x35 0x69 0x74 0x61 0x73 0x31 0x30 0x39 
    /dev/ttyUSB0 - Count: 3, Length: 12, Str: 12345itas109, Hex: 0x31 0x32 0x33 0x34 0x35 0x69 0x74 0x61 0x73 0x31 0x30 0x39
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    License

    License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎

    如需转载请标明出处:http://blog.csdn.net/itas109
    技术交流:129518033


    Reference:

    1.https://www.wch.cn/product/CH340.html

  • 相关阅读:
    超越视觉极限:深度学习图像超分辨率算法清单【第二部分】
    阿里面试官问的 Redis 太刁钻,索性整理出所有大厂的 Redis 面试题,拿下麻烦的面试官
    分布式事务
    高颜值测试报告- XTestRunner
    Mybatis多表连接查询——一对多
    ​面试经典150题——LRU 缓存
    MBA形式逻辑四大基本考点
    GBase 8a MPP集群VC管理之访问VC
    mqtt-ros模拟发布一个自定义消息类型
    C++入门基础05:表达式(表达式基础、算术运算符与赋值运算符、逻辑关系运算符、成员访问运算符与条件运算符、位运算符、移位运算符与类型转换)
  • 原文地址:https://blog.csdn.net/itas109/article/details/127826051