USB Gadget CDC-ACM串口实例
2025-08-27
本文链接为:http://www.usbzh.com/article/detail-1568.html ,欢迎转载,转载请附上本文链接。
符合USB ACM规范的设备由Windows提供其驱动程序
usbser.sys
userser.inf
Linux Gadget对应的ACM驱动依赖为
root@ATK-IMX6U:/sys/kernel/config/usb_gadget/g1# lsmod
Module Size Used by
usb_f_acm 5204 2
u_serial 9712 1 usb_f_acm
libcomposite 44470 8 usb_f_acm
使用如下的脚本创建ACM串口设备
#!/bin/bash
modprobe libcomposite
modprobe usb_f_acm
mount -t configfs none /sys/kernel/config
mkdir -p /sys/kernel/config/usb_gadget/g1
cd /sys/kernel/config/usb_gadget/g1
# Set USB vendor and product ID
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0119> idProduct # Multifunction Composite Gadget
# Set device version
echo 0x0100 > bcdDevice
echo 0x0200 > bcdUSB
mkdir -p functions/acm.usb0
mkdir -p configs/c.1
mkdir -p configs/c.1/strings/0x409
echo "USBZH Serial Config" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower # 250mA
ln -s functions/acm.usb0 configs/c.1/
# Enable the gadget
udc=$(ls /sys/class/udc/)
echo "$udc" > UDC
使能后将USB OTG插入Windows系统后,在设备管理器中如下:

通过UsbTreeViewer查看设备信息:
=========================== USB Port1 ===========================
Connection Status : 0x01 (Device is connected)
Port Chain : 2-1-1
Properties : 0x00
IsUserConnectable : no
PortIsDebugCapable : no
PortHasMultiCompanions : no
PortConnectorIsTypeC : no
ConnectionIndex : 0x01 (Port 1)
========================== Summary =========================
Vendor ID : 0x1D6B (The Linux Foundation)
Product ID : 0x0119
Manufacturer String : ---
Product String : ---
Serial : ---
USB Version : 2.0
Port maximum Speed : High-Speed
Device maximum Speed : High-Speed
Device Connection Speed : High-Speed
Self powered : no
Demanded Current : 250 mA
Used Endpoints : 4
======================== USB Device ========================
+++++++++++++++++ Device Information ++++++++++++++++++
Device Description : USB Composite Device
Device Path : \\?\USB#VID_1D6B&PID_0119#6&1c528954&1&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed} (GUID_DEVINTERFACE_USB_DEVICE)
Kernel Name : \Device\USBPDO-9
Device ID : USB\VID_1D6B&PID_0119\6&1C528954&1&1
Hardware IDs : USB\VID_1D6B&PID_0119&REV_0100 USB\VID_1D6B&PID_0119
Driver KeyName : {36fc9e60-c465-11cf-8056-444553540000}\0014 (GUID_DEVCLASS_USB)
Driver : \SystemRoot\System32\drivers\usbccgp.sys (Version: 10.0.18362.1110 Date: 2020-10-17 Company: Microsoft Corporation)
Driver Inf : C:\Windows\inf\usb.inf
Legacy BusType : PNPBus
Class : USB
Class GUID : {36fc9e60-c465-11cf-8056-444553540000} (GUID_DEVCLASS_USB)
Service : usbccgp
Enumerator : USB
Location Info : Port_#0001.Hub_#0002
Address : 1
Location IDs : PCIROOT(0)#PCI(1400)#USBROOT(0)#USB(1)#USB(1), ACPI(_SB_)#ACPI(PCI0)#ACPI(XHC_)#ACPI(RHUB)#ACPI(HS01)#USB(1)
Container ID : {11702635-82b2-11f0-9ab0-00e04b3be8a5}
Manufacturer Info : (标准 USB 主控制器)
Capabilities : 0x84 (Removable, SurpriseRemovalOK)
Status : 0x0180600A (DN_DRIVER_LOADED, DN_STARTED, DN_DISABLEABLE, DN_REMOVABLE, DN_NT_ENUMERATOR, DN_NT_DRIVER)
Problem Code : 0
Address : 1
EnhancedPowerMgmtEnabled : 0
Power State : D0 (supported: D0, D3, wake from D0)
+++++++++++++++++ Registry USB Flags +++++++++++++++++
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\usbflags\1D6B01190100
osvc : REG_BINARY 00 00
---------------- Connection Information ---------------
Connection Index : 0x01 (Port 1)
Connection Status : 0x01 (DeviceConnected)
Current Config Value : 0x01 (Configuration 1)
Device Address : 0x1C (28)
Is Hub : 0x00 (no)
Device Bus Speed : 0x02 (High-Speed)
Number of open Pipes : 0x03 (3 pipes to data endpoints)
Pipe[0] : EndpointID=2 Direction=IN ScheduleOffset=0 Type=Interrupt wMaxPacketSize=0xA bInterval=9 -> 214 Bits/ms = 26750 Bytes/s
Pipe[1] : EndpointID=1 Direction=IN ScheduleOffset=0 Type=Bulk wMaxPacketSize=0x200 bInterval=0
Pipe[2] : EndpointID=1 Direction=OUT ScheduleOffset=0 Type=Bulk wMaxPacketSize=0x200 bInterval=0
Data (HexDump) : 01 00 00 00 12 01 00 02 00 00 00 40 6B 1D 19 01 ...........@k...
00 01 00 00 00 01 01 02 00 1C 00 03 00 00 00 01 ................
00 00 00 07 05 82 03 0A 00 09 00 00 00 00 07 05 ................
81 02 00 02 00 00 00 00 00 07 05 01 02 00 02 00 ................
00 00 00 00 ....
--------------- Connection Information V2 -------------
Connection Index : 0x01 (1)
Length : 0x10 (16 bytes)
SupportedUsbProtocols : 0x03
Usb110 : 1 (yes, port supports USB 1.1)
Usb200 : 1 (yes, port supports USB 2.0)
Usb300 : 0 (no, port not supports USB 3.0)
ReservedMBZ : 0x00
Flags : 0x00
DevIsOpAtSsOrHigher : 0 (Device is not operating at SuperSpeed or higher)
DevIsSsCapOrHigher : 0 (Device is not SuperSpeed capable or higher)
DevIsOpAtSsPlusOrHigher : 0 (Device is not operating at SuperSpeedPlus or higher)
DevIsSsPlusCapOrHigher : 0 (Device is not SuperSpeedPlus capable or higher)
ReservedMBZ : 0x00
Data (HexDump) : 01 00 00 00 10 00 00 00 03 00 00 00 00 00 00 00 ................
---------------------- Device Descriptor ----------------------
bLength : 0x12 (18 bytes)
bDescriptorType : 0x01 (Device Descriptor)
bcdUSB : 0x200 (USB Version 2.0)
bDeviceClass : 0x00 (defined by the interface descriptors)
bDeviceSubClass : 0x00
bDeviceProtocol : 0x00
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x1D6B (The Linux Foundation)
idProduct : 0x0119
bcdDevice : 0x0100
iManufacturer : 0x00 (No String Descriptor)
iProduct : 0x00 (No String Descriptor)
iSerialNumber : 0x00 (No String Descriptor)
bNumConfigurations : 0x01 (1 Configuration)
Data (HexDump) : 12 01 00 02 00 00 00 40 6B 1D 19 01 00 01 00 00 .......@k.......
00 01 ..
------------------ Configuration Descriptor -------------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x02 (Configuration Descriptor)
wTotalLength : 0x004B (75 bytes)
bNumInterfaces : 0x02 (2 Interfaces)
bConfigurationValue : 0x01 (Configuration 1)
iConfiguration : 0x01 (String Descriptor 1)
Language 0x0409 : "Serial Config"
bmAttributes : 0x80
D7: Reserved, set 1 : 0x01
D6: Self Powered : 0x00 (no)
D5: Remote Wakeup : 0x00 (no)
D4..0: Reserved, set 0 : 0x00
MaxPower : 0x7D (250 mA)
Data (HexDump) : 09 02 4B 00 02 01 01 80 7D 08 0B 00 02 02 02 01 ..K.....}.......
04 09 04 00 00 01 02 02 01 02 05 24 00 10 01 05 ...........$....
24 01 00 01 04 24 02 02 05 24 06 00 01 07 05 82 $....$...$......
03 0A 00 09 09 04 01 00 02 0A 00 00 03 07 05 81 ................
02 00 02 00 07 05 01 02 00 02 00 ...........
------------------- IAD Descriptor --------------------
bLength : 0x08 (8 bytes)
bDescriptorType : 0x0B (Interface Association Descriptor)
bFirstInterface : 0x00 (Interface 0)
bInterfaceCount : 0x02 (2 Interfaces)
bFunctionClass : 0x02 (Communications and CDC Control)
bFunctionSubClass : 0x02
bFunctionProtocol : 0x01
iFunction : 0x04 (String Descriptor 4)
Language 0x0409 : "CDC Serial"
Data (HexDump) : 08 0B 00 02 02 02 01 04 ........
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x00
bNumEndpoints : 0x01 (1 Endpoint)
bInterfaceClass : 0x02 (Communications and CDC Control)
bInterfaceSubClass : 0x02 (Abstract Control Model)
bInterfaceProtocol : 0x01 (AT Commands defined by ITU-T V.250 etc)
iInterface : 0x02 (String Descriptor 2)
Language 0x0409 : "CDC Abstract Control Model (ACM)"
Data (HexDump) : 09 04 00 00 01 02 02 01 02 .........
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x05 (5 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x00 (Header Functional Descriptor)
bcdCDC : 0x110 (CDC Version 1.10)
Data (HexDump) : 05 24 00 10 01 .$...
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x05 (5 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x01 (Call Management Functional Descriptor)
bmCapabilities : 0x00
D7..2 : 0x00 (Reserved)
D1 : 0x00 (sends/receives call management information only over the Communication Class interface)
D0 : 0x00 (does not handle call management itself)
bDataInterface : 0x01
Data (HexDump) : 05 24 01 00 01 .$...
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x04 (4 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x02 (Abstract Control Management Functional Descriptor)
bmCapabilities : 0x02
D7..4 : 0x00 (Reserved)
D3 : 0x00 (not supports the notification Network_Connection)
D2 : 0x00 (not supports the request Send_Break)
D1 : 0x01 (supports the request combination of Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State)
D0 : 0x00 (not supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature)
Data (HexDump) : 04 24 02 02 .$..
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x05 (5 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x06 (Union Functional Descriptor)
bControlInterface : 0x00
bSubordinateInterface[0] : 0x01
Data (HexDump) : 05 24 06 00 01 .$...
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x82 (Direction=IN EndpointID=2)
bmAttributes : 0x03 (TransferType=Interrupt)
wMaxPacketSize : 0x000A
Bits 15..13 : 0x00 (reserved, must be zero)
Bits 12..11 : 0x00 (0 additional transactions per microframe -> allows 1..1024 bytes per packet)
Bits 10..0 : 0x0A (10 bytes per packet)
bInterval : 0x09 (256 microframes -> 32 ms)
Data (HexDump) : 07 05 82 03 0A 00 09 .......
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x01 (Interface 1)
bAlternateSetting : 0x00
bNumEndpoints : 0x02 (2 Endpoints)
bInterfaceClass : 0x0A (CDC-Data)
bInterfaceSubClass : 0x00
bInterfaceProtocol : 0x00
iInterface : 0x03 (String Descriptor 3)
Language 0x0409 : "CDC ACM Data"
Data (HexDump) : 09 04 01 00 02 0A 00 00 03 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0200 (max 512 bytes)
bInterval : 0x00 (never NAKs)
Data (HexDump) : 07 05 81 02 00 02 00 .......
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x01 (Direction=OUT EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0200 (max 512 bytes)
bInterval : 0x00 (never NAKs)
Data (HexDump) : 07 05 01 02 00 02 00 .......
--------- Device Qualifier Descriptor (for Full-Speed) --------
bLength : 0x0A (10 bytes)
bDescriptorType : 0x06 (Device_qualifier Descriptor)
bcdUSB : 0x200 (USB Version 2.0)bDeviceClass : 0x00 (defined by the interface descriptors)
bDeviceSubClass : 0x00
bDeviceProtocol : 0x00
bMaxPacketSize0 : 0x40 (64 Bytes)
bNumConfigurations : 0x01 (1 other-speed configuration)
bReserved : 0x00
Data (HexDump) : 0A 06 00 02 00 00 00 40 01 00 .......@..
------------ Other Speed Configuration Descriptor -------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x07 (Other_speed_configuration Descriptor)
wTotalLength : 0x004B (75 bytes)
bNumInterfaces : 0x02 (2 Interfaces)
bConfigurationValue : 0x01 (Configuration 1)
iConfiguration : 0x01 (String Descriptor 1)
Language 0x0409 : "Serial Config"
bmAttributes : 0x80
D7: Reserved, set 1 : 0x01
D6: Self Powered : 0x00 (no)
D5: Remote Wakeup : 0x00 (no)
D4..0: Reserved, set 0 : 0x00
MaxPower : 0x7D (250 mA)
Data (HexDump) : 09 07 4B 00 02 01 01 80 7D 08 0B 00 02 02 02 01 ..K.....}.......
04 09 04 00 00 01 02 02 01 02 05 24 00 10 01 05 ...........$....
24 01 00 01 04 24 02 02 05 24 06 00 01 07 05 82 $....$...$......
03 0A 00 20 09 04 01 00 02 0A 00 00 03 07 05 81 ... ............
02 40 00 00 07 05 01 02 40 00 00 .@......@..
------------------- IAD Descriptor --------------------
bLength : 0x08 (8 bytes)
bDescriptorType : 0x0B (Interface Association Descriptor)
bFirstInterface : 0x00 (Interface 0)
bInterfaceCount : 0x02 (2 Interfaces)
bFunctionClass : 0x02 (Communications and CDC Control)
bFunctionSubClass : 0x02
bFunctionProtocol : 0x01
iFunction : 0x04 (String Descriptor 4)
Language 0x0409 : "CDC Serial"
Data (HexDump) : 08 0B 00 02 02 02 01 04 ........
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x00
bNumEndpoints : 0x01 (1 Endpoint)
bInterfaceClass : 0x02 (Communications and CDC Control)
bInterfaceSubClass : 0x02 (Abstract Control Model)
bInterfaceProtocol : 0x01 (AT Commands defined by ITU-T V.250 etc)
iInterface : 0x02 (String Descriptor 2)
Language 0x0409 : "CDC Abstract Control Model (ACM)"
Data (HexDump) : 09 04 00 00 01 02 02 01 02 .........
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x05 (5 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x00 (Header Functional Descriptor)
bcdCDC : 0x110 (CDC Version 1.10)
Data (HexDump) : 05 24 00 10 01 .$...
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x05 (5 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x01 (Call Management Functional Descriptor)
bmCapabilities : 0x00
D7..2 : 0x00 (Reserved)
D1 : 0x00 (sends/receives call management information only over the Communication Class interface)
D0 : 0x00 (does not handle call management itself)
bDataInterface : 0x01
Data (HexDump) : 05 24 01 00 01 .$...
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x04 (4 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x02 (Abstract Control Management Functional Descriptor)
bmCapabilities : 0x02
D7..4 : 0x00 (Reserved)
D3 : 0x00 (not supports the notification Network_Connection)
D2 : 0x00 (not supports the request Send_Break)
D1 : 0x01 (supports the request combination of Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State)
D0 : 0x00 (not supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature)
Data (HexDump) : 04 24 02 02 .$..
-------------- CDC Interface Descriptor ---------------
bFunctionLength : 0x05 (5 bytes)
bDescriptorType : 0x24 (Interface)
bDescriptorSubType : 0x06 (Union Functional Descriptor)
bControlInterface : 0x00
bSubordinateInterface[0] : 0x01
Data (HexDump) : 05 24 06 00 01 .$...
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x82 (Direction=IN EndpointID=2)
bmAttributes : 0x03 (TransferType=Interrupt)
wMaxPacketSize : 0x000A (10 bytes)
bInterval : 0x20 (32 ms)
Data (HexDump) : 07 05 82 03 0A 00 20 ......
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x01 (Interface 1)
bAlternateSetting : 0x00
bNumEndpoints : 0x02 (2 Endpoints)
bInterfaceClass : 0x0A (CDC-Data)
bInterfaceSubClass : 0x00
bInterfaceProtocol : 0x00
iInterface : 0x03 (String Descriptor 3)
Language 0x0409 : "CDC ACM Data"
Data (HexDump) : 09 04 01 00 02 0A 00 00 03 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0040 (64 bytes)
bInterval : 0x00 (ignored)
Data (HexDump) : 07 05 81 02 40 00 00 ....@..
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x01 (Direction=OUT EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0040 (64 bytes)
bInterval : 0x00 (ignored)
Data (HexDump) : 07 05 01 02 40 00 00 ....@..
-------------------- String Descriptors -------------------
------ String Descriptor 0 ------
bLength : 0x04 (4 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language ID[0] : 0x0409 (English - United States)
Data (HexDump) : 04 03 09 04 ....
------ String Descriptor 1 ------
bLength : 0x1C (28 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "Serial Config"
Data (HexDump) : 1C 03 53 00 65 00 72 00 69 00 61 00 6C 00 20 00 ..S.e.r.i.a.l. .
43 00 6F 00 6E 00 66 00 69 00 67 00 C.o.n.f.i.g.
------ String Descriptor 2 ------
bLength : 0x42 (66 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "CDC Abstract Control Model (ACM)"
Data (HexDump) : 42 03 43 00 44 00 43 00 20 00 41 00 62 00 73 00 B.C.D.C. .A.b.s.
74 00 72 00 61 00 63 00 74 00 20 00 43 00 6F 00 t.r.a.c.t. .C.o.
6E 00 74 00 72 00 6F 00 6C 00 20 00 4D 00 6F 00 n.t.r.o.l. .M.o.
64 00 65 00 6C 00 20 00 28 00 41 00 43 00 4D 00 d.e.l. .(.A.C.M.
29 00 ).
------ String Descriptor 3 ------
bLength : 0x1A (26 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "CDC ACM Data"
Data (HexDump) : 1A 03 43 00 44 00 43 00 20 00 41 00 43 00 4D 00 ..C.D.C. .A.C.M.
20 00 44 00 61 00 74 00 61 00 .D.a.t.a.
------ String Descriptor 4 ------
bLength : 0x16 (22 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "CDC Serial"
Data (HexDump) : 16 03 43 00 44 00 43 00 20 00 53 00 65 00 72 00 ..C.D.C. .S.e.r.
69 00 61 00 6C 00 i.a.l.
测试:
在Linux端可以看到如下设备
root@ATK-IMX6U:/dev# ls /dev/ttyGS*
/dev/ttyGS0
发送数据到Windows端:
root@ATK-IMX6U:/dev# echo "hello www.usbzh.com" > ttyGS0
在Windows端打开串口5,可以收到数据
Connecting to COM5...
Connected.
hello www.usbzh.com
本文链接为:http://www.usbzh.com/article/detail-1568.html ,欢迎转载,转载请附上本文链接。
USB Gadget





