Linux内核中usb设备侧驱动程序分成3个层次:UDC驱动程序、Gadget APIGadget驱动程序。UDC驱动程序(USB控制器)直接访问硬件,控制USB设备和主机间的底层通信,向上层提供与硬件相关操作的回调函数。Gadget APIUDC驱动程序回调函数的简单包装,这部分程序内核都已经写好。Gadget驱动程序具体控制USB设备功能的实现,使设备表现出“U盘”、“虚拟串口”等特性。

简单看个usb 虚拟串口例子

Overview
--------
The gadget serial driver is a Linux USB gadget driver, a USB device
side driver.  It runs on a Linux system that has USB device side
hardware; for example, a PDA, an embedded Linux system, or a PC
with a USB development card.
 
The gadget serial driver talks over USB to either a CDC ACM driver
or a generic USB serial driver running on a host PC.
 
   Host
  --------------------------------------

| Host-Side   CDC ACM       USB Host   |

| Operating |   or        | Controller |   USB

| System    | Generic USB | Driver     |--------

| (Linux or | Serial      | and        |        |

| Windows)    Driver        USB Stack  |        |

--------------------------------------         |

|

|

|

Gadget                                         |

--------------------------------------         |

| Gadget                   USB Periph. |        |

| Device-Side |  Gadget  | Controller  |        |

| Linux       |  Serial  | Driver      |--------

| Operating   |  Driver  | and         |

| System                   USB Stack   |

--------------------------------------

On the device-side Linux system, the gadget serial driver looks
like a serial device.
 
On the host-side system, the gadget serial device looks like a
CDC ACM compliant class device or a simple vendor specific device
with bulk in and bulk out endpoints, and it is treated similarly
to other serial devices.
 
The host side driver can potentially be any ACM compliant driver
or any driver that can talk to a device with a simple bulk in/out
interface.  Gadget serial has been tested with the Linux ACM driver,
the Windows usbser.sys ACM driver, and the Linux USB generic serial
driver.
 
With the gadget serial driver and the host side ACM or generic
serial driver running, you should be able to communicate between
the host and the gadget side systems as if they were connected by a
serial cable.
 
The gadget serial driver only provides simple unreliable data
communication.  It does not yet handle flow control or many other
features of normal serial devices
.

内核版本:3.15                    硬件:ATMEL  SAMA5D3            编译环境:ubuntu 12.04

1、配置内核

这里之所以都选择为模块的形式,是为了调试方便,有些模块,比如U盘加载时还需要提供介质,就是说加载模块时还需要参数,否则加载不上

Device Drivers  --->

[*] USB support  --->

<*>   USB Gadget Support  --->

<M>   USB Gadget Drivers                                                                            
                             < >     USB functions configurable through configfs                                             
                           < >     Gadget Zero (DEVELOPMENT)                                                                    
                             < >     Ethernet Gadget (with CDC Ethernet support)                                                
                              < >     Network Control Model (NCM) support                                                         
                             < >     Gadget Filesystem                                                                            
                             < >     Function Filesystem                                                                      
                             < >     Mass Storage Gadget                                                                           
                             < >     USB Gadget Target Fabric Module                                                            
                               <M>     Serial Gadget (with CDC ACM and CDC OBEX support)                                          
                               < >     Printer Gadget

保存退出,编译内核。在drivers/usb/gadget目录下会生成以下驱动文件

-rw-rw-r-- 1 a_tu a_tu  9710  3月  5 15:14 ./g_serial.ko
-rw-rw-r-- 1 a_tu a_tu 49319  3月  5 15:14 ./libcomposite.ko
-rw-rw-r-- 1 a_tu a_tu 11473  3月  5 15:14 ./usb_f_acm.ko
-rw-rw-r-- 1 a_tu a_tu  9485  3月  5 15:14 ./usb_f_obex.ko
-rw-rw-r-- 1 a_tu a_tu  8011  3月  5 15:14 ./usb_f_serial.ko
-rw-rw-r-- 1 a_tu a_tu 15970  3月  5 15:14 ./u_serial.ko

2、操作开发板


把编译好的内核烧入开发板,加载生成的驱动文件,注意加载顺序,否则会出现错误。

insmod  u_serial.ko
insmod  libcomposite.ko
insmod   usb_f_serial.ko
insmod  usb_f_obex.ko
insmod  usb_f_acm.ko
insmod  g_serial.ko

出现内核信息如下:

[   27.310000] --------file=composite.c-------usb_composite_probe------1830
[   27.320000] ------------402
[   27.320000] -----usb_gadget_probe_driver------411
[   27.330000] -------udc_bind_to_driver-----342
[   27.330000] g_serial gadget: Gadget Serial v2.4
[   27.340000] g_serial gadget: g_serial ready

[   18.960000] g_serial gadget: high-speed config #2: CDC ACM config

说明加载成功。

此时你打开设备管理器会发现多了一个串口设备,

ELMO GMAS(COM10)

Win7操作系统可以自动加载基于udc标准的serial驱动。

对于WinXP操作系统,要使用ACM串口,需要WindowsXP机器上有gserial.inf和usbser.sys这两个文件,其中gserial.inf的配置如下:

[Version]

  Signature='$Windows NT$'

  Class=Ports

  ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}

  Provider=%LINUX%

  DriverVer=08/17/2004,0.0.2.0

  ; Copyright (C) 2004 Al Borchers (alborchers@steinerpoint.com)



  [Manufacturer]

  %LINUX%=GSerialDeviceList



  [GSerialDeviceList]

  %GSERIAL%=GSerialInstall, USB\VID_0525&PID_A4A7



  [DestinationDirs]

  DefaultDestDir=10,System32\Drivers



  [GSerialInstall]

  CopyFiles=GSerialCopyFiles

  AddReg=GSerialAddReg



  [GSerialCopyFiles]

  usbser.sys



  [GSerialAddReg]

  HKR,,DevLoader,,*ntkern

  HKR,,NTMPDriver,,usbser.sys

  HKR,,EnumPropPages32,,'MsPorts.dll,SerialPortPropPageProvider'



  [GSerialInstall.Services]

  AddService = usbser,0x0002,GSerialService



  [GSerialService]

  DisplayName = %GSERIAL_DISPLAY_NAME%

  ServiceType = 1 ; SERVICE_KERNEL_DRIVER

  StartType = 3 ; SERVICE_DEMAND_START

  ErrorControl = 1 ; SERVICE_ERROR_NORMAL

  ServiceBinary = %10%\System32\Drivers\usbser.sys

  LoadOrderGroup = Base



  [Strings]

  LINUX = 'Linux'

  GSERIAL = 'Gadget Serial'

  GSERIAL_DISPLAY_NAME = 'USB Gadget Serial Driver'

安装成功后,在设备管理器中可以看到 “Gadget Serial (COM11) '这个端口。

开发板的/dev/下会出现/dev/ttyGS0这个设备。当然如果你怕这个设备重名,可以更改这个设备节点。操作如下:

cat /proc/devices

Character devices:

1 mem

2 pty

3 ttyp

4 /dev/vc/0

4 tty

4 ttyS

5 /dev/tty

5 /dev/console

5 /dev/ptmx

7 vcs

10 misc

13 input

90 mtd

128 ptm

136 pts

153 spi

251 ttyGS

mknod /dev/usb_serial c 254 0

以后可以直接按照串口通讯的方式打开该节点 /dev/usb_serial,进行数据读写操作。

3、通信测试


开发板上执行命令  cat /dev/usb_serial(当然你也可以编写程序,打开设备读写),这里只是简单的测试设备通信是否正常 ,pc机上打开串口调试助手,打开com10,通过字符串输入框发送数据。此时发现开发板没有收到数据,而串口助手却收到自己发送的数据。这让我很是纳闷,折腾了2天,终于睡醒了。

又测试了另一通路:开发板给pc发。echo 12345 > /dev/usb_serial

这是完全正常的,通过串口助手收到的数据来看。每次收到的数据都有换行现象,会不会是数据没有从缓冲区中刷出来?我就换了数据格式,发送简单文件

111111111111111111
222222222222222222
222222222222222222
444444444444444444

此时开发板收到数据

# cat /dev/usb_serial
111111111111111111
222222222222222222
222222222222222222
444444444444444444

原来真的是 "\n" 在作怪!!!,如果你的文件只有一行,且没有换行。那么你的开发板是收不到数据的,这些数据并没有丢失,而是存储在串口的缓冲区中,直到遇到 "\n",才会把数据一股脑发出来。

至此测试完毕。

本人水平有限,文章仅代表个人观点,如有错误,请指正!!!

Linux USB虚拟串口设备相关推荐

  1. linux内核配置usb虚拟串口,Linux USB虚拟串口设备

    Linux内核中usb设备侧驱动程序分成3个层次:UDC驱动程序.Gadget API和Gadget驱动程序.UDC驱动程序(USB控制器)直接访问硬件,控制USB设备和主机间的底层通信,向上层提供与 ...

  2. linux USB虚拟串口设备名的修改

    在LINUX 中,会经常用到USB转串口的设备.而LINUX中通用的做法是根据插入USB设备的先后顺序依次注册ttyUSB0\1\2\3..等设备,但是这样就有一个问题了,例如我有多个USB的口,要接 ...

  3. linux系统串口透传,基于CC2540的USB虚拟串口透传方案

    1.USB虚拟串口代替物理串口的可行性 首先,越来越多带USB接口的器件涌现出来,如带USB接口的单片机,或独立的USB接口器件,而且这些器件的成本已经很接近于使用RS232电平转换芯片所带来的成本. ...

  4. LINUX驱动开发(三)USB虚拟网卡

    1. 简述 之前见过其他项目组的开发板,发现他们没有网口,但是可以通过IP进行ssh连接和scp文件传输到开发板,百度发现他们用的是USB虚拟网卡,于是我在我的开发板进行测试. 主机:windows ...

  5. 让Linux支持usb虚拟网卡。

    介绍: Usb作为device端插入pc,在pc中会出现对用的usb虚拟网卡,可以像普通网卡一样传递数据,socket编程. 让平台支持usb虚拟网卡,需要Linux内核支持UDC(usb 设备控制器 ...

  6. linux平台实现USB虚拟总线驱动一(原理以及开发流程)

    by fanxiushu 2019-11-07 转载或引用请注明原始作者. 之前的文章阐述过在windows平台下,如何实现USB虚拟总线驱动, 以及如何在windows平台采集真实USB设备的数据, ...

  7. 【USB设备设计】-- CDC 设备开发(虚拟串口设备)

    ​在嵌入式系统中,串行异步通信接口(UART)使用很频繁的接口,跟主机建立通信往往会用到USB转串口的设备,本章将介绍如何将USB虚拟成串口设备. 前期准备 1.带USB 功能的MCU (笔者使用的N ...

  8. ubuntu linux支持rndis,BBB 通过USB虚拟的RNDIS与PC的vmware ubuntu 进行网络通信

    很多时候,BBB的网络接口需要连接其他的设备进行.在工作的时候就无法跟电脑指向相互传输文件,此时可以用usb虚拟出一个网卡,usb模拟网卡的步骤可以查看看: 之前也有介绍BBB如何使用 RNDIS与w ...

  9. 扫描枪设置虚拟串口linux,顶码扫描枪全能王TP20Y怎么设置USB虚拟串口模式?

    顶码全能王TP20Y是一款极具性价比的二维影像手持扫描枪,可以读取所有一维.二维条码,外形小巧,价格实惠,卓越的工业保护设计,坚固耐用,适用于超市.便利店的零售收银.自动化办公与文件管理.邮件与包裹的 ...

最新文章

  1. 进击的新版NavMesh系统:看我飞檐走壁
  2. java cookie的有效期_Java Web cookie的有效期
  3. 23种设计模式C++源码与UML实现--享元模式
  4. c 语言 指针 指向数组,C 指向数组的指针
  5. HTML页面显示缩略所有空格为一格的方法
  6. linux菜单系统,Linux修改grub菜单
  7. 【机器学习】 - 数据预处理之数据归一化(标准化)与实战分析,正则化
  8. 漫画 | Kubernetes带你一帆风顺去远航
  9. MySQL怎样存储IP地址
  10. multiprocessing(python 版)
  11. 非刚性人脸跟踪 —— 面部特征检测器
  12. C++ Vectors
  13. lopatkin俄大神精简中文系统Windows 10 Enterprise 2016 LTSB 14393.577 x86-x64 ZH-CN PIP
  14. element click intercepted
  15. makefile中wildcard的理解
  16. Linux内核常见分配函数
  17. 教你如何编辑修改PDF文件内容
  18. APOLLO UDACITY自动驾驶课程笔记——感知、预测
  19. layui后台管理首页配置
  20. 快速查找论文的源代码网站资料

热门文章

  1. MapboxMap 之设置 Style
  2. C语言数据结构复制粘贴报错可能原因
  3. 算法竞赛进阶指南——队列学习笔记
  4. Vmare horizon client 5.0安装过程中自动取消
  5. 【开发工具】【readelf】查看ELF格式文件工具(readelf)的使用
  6. matlab bs2rv.m,matlab遗传算法工具箱gatbx(直接可用版)
  7. 《算法通关之路》-数学之美
  8. 网易云音乐在Ubuntu下出现部分音乐无法播放的解决方法
  9. 【前端学习之HTMLCSS进阶篇】-- CSS第一篇 -- @规则与web字体图标
  10. 信息学奥赛一本通OJ勘误