CDC virtual comport

Source: Internet
Author: User

Address: http://hi.baidu.com/techat/blog/item/93b3523039e7f595a8018e38.html

 

Now many computers do not have 232, especially laptop computers. This makes it very inconvenient for many debugging personnel to use the mobile phone. Maybe you can buy a USB to 232 conversion line on the market, but the anti-interference of these lines is not very good, in some places where the interference is large, there will be connection interruption, so it is often necessary to add an optical coupling isolator. Some high-end single-chip microcomputer generally has USB interfaces. If you can use these interfaces to create a USB virtual comport, it will be very practical, but the use of USB generally requires writing a host computer drive. Friends who do not have the basics of writing windows drivers are generally very embarrassed. In fact, Microsoft's CDC class contains this type of USB-to-com class, and Windows also comes with this drive. Just like the USB mouse and USB flash drive on the market, their driver function library is already available in windows, as long as we configure USB according to the Windows CDC description.
For more information about the CDC class description class, see the PDF file of Universal Serial Bus class definitions for communication devices. It is the program of all the content below.

The following describes the procedure:
1: Send the corresponding descriptor: the following content is sent.
Const usb_device_descriptor devicedescr =
{
// Device descriptor
0x12,
Desc_device,
Le_word (0x0101), // bcdusb

0x02, // bdeviceclass
0x00, // bdevicesubclass
0x00, // bdeviceprotocol

Max_packet_size0, // bmaxpacketsize

// Device ID
Le_word (0 xFFFF), // idvendor
Le_word (0x0005), // idproduct
Le_word (0x0100), // bcddevice

/* Do not use the original ID
0 x, 0x04, // device ID of Philips Company vendor id = Philips semiconductor, Inc.
0 x, 0x23, // product ID agreed upon by device manufacturing
0 x, 0x01, // device series No. device release number in binary-coded decimal
*/

0x01, // imanufacturer
0x02, // iproduct
0x03, // iserialnumber
0x01, // bnumconfigurations
};

Const usb_descriptor usb_descr =
{
// Configuration Descriptor
{
0x09,
Desc_configuration,
Le_word (67), // wtotallength
0x02, // bnuminterfaces
0x01, // bconfigurationvalue
0x00, // iconfiguration
0xc0, // bmattributes
0x32 // bmaxpower
},
// Control class Interface
{
0x09,
Desc_interface, // 4
0x00, // binterfacenumber
0x00, // balternatesetting
0x01, // bnumendpoints
0x02, // binterfaceclass
0x02, // binterfacesubclass
// 0x01, // binterfaceprotocol, Linux requires value of 1 for the cdc_acm Module
0x00, // binterfaceprotocol, Linux requires value of 1 for the cdc_acm Module
0x00 // iinterface
},

// Header functional Descriptor
0x05,
Cs_interface,
0x00,
Le_word (0x0110 ),
// Call management functional Descriptor
0x05,
Cs_interface,
0x01,
0x01, // bmcapabilities = device handles call management
0x01, // bdatainterface
// ACM functional Descriptor
0x04,
Cs_interface,
0x02,
0x02, // bmcapabilities
// Union functional Descriptor
0x05,
Cs_interface,
0x06,
0x00, // bmasterinterface
0x01, // bslaveinterface0
// Notification EP
0x07,
Desc_endpoint,
Int_in_ep, // bendpointaddress
0x03, // bmattributes = intr
Le_word (8), // wmaxpacketsize
0x0a, // binterval
// Data class interface Descriptor
0x09,
Desc_interface,
0x01, // binterfacenumber
0x00, // balternatesetting
0x02, // bnumendpoints
0x0a, // binterfaceclass = Data
0x00, // binterfacesubclass
0x00, // binterfaceprotocol
0x00, // iinterface
// Data EP out
0x07,
Desc_endpoint,
Bulk_out_ep, // bendpointaddress
0x02, // bmattributes = Bulk
Le_word (max_packet_size), // wmaxpacketsize
0x00, // binterval
// Data EP in
0x07,
Desc_endpoint,
Bulk_in_ep, // bendpointaddress
0x02, // bmattributes = Bulk
Le_word (max_packet_size), // wmaxpacketsize
0x00, // binterval

// String Descriptors
0x04,
Desc_string,
Le_word (0x0409 ),

0x0e,
Desc_string,
'L', 0, 'P', 0, 'C', 0, 'U', 0, 's', 0, 'B', 0,

0x14,
Desc_string,
'U', 0, 's', 0, 'B', 0, 's', 0, 'E', 0, 'R', 0, 'I ', 0, 'A', 0, 'l', 0,

0x12,
Desc_string,
'D', 0, 'E', 0, 'A', 0, 'D', 0, 'C', 0, '0', 0, 'D ', 0, 'E', 0,

// Terminating zero
0

};

Note that the device ID in usb_device_descriptor should not use the original manufacturer ID. According to the above, this value corresponds to the following in the INF file:
[Gserialdevicelist]
% Gserial % = gserialinstall, USB \ vid_ffff & pid_0005
PID and vid
Each USB device has a PID and a vid. Vid is the manufacturer's code and PID is the product code. Each code is a dual-byte integer. PID and vid cannot be set at will. They are allocated by the USB Standards Association, just like IP Address allocation. A usb pid/vid License costs $1500, especially for small companies. In this case, there are free PID/vid pairs, which are applicable to hid, CDC, and general-purpose devices. Users who use avrusb can use them for free, in this way, most applications no longer need to apply for PID/vid.
PID and vid areProgramAnd user programs will be used, it is a key parameter for Windows to identify USB devices, user programs also need to find the corresponding USB device through PID and vid.
It is used by windows to identify USB devices and find the ID of the corresponding drive path. If the enumeration is successful, the system prompts you to install the drive path.

It is used by windows to identify USB devices and find the ID of the corresponding drive path. If the enumeration is successful, the system prompts you to install the drive path.

To identify as a CDC device, use the ID provided above instead of the vendor ID. Maybe you don't want to change the ID of the original USB device, but change the [gserialdevicelist] of the INF file to the ID of the original manufacturer. In this way, I have tried to identify the device,

However, the com parameter setting item cannot appear in com8. (The following port settings will not appear), and com8 cannot be operated, that is, it cannot be used in the serial port assistant.

Only the configuration characters provided by the program can be recognized as COM and the ports that appear can be used. My explanation is that this ID corresponds to the free PID and vid of CDC, so windows can correctly configure the device.

For the enumeration process, you can refer to the information I have provided or search by yourself. I will not describe it here. During the debugging process, you can use bus_hound for step-by-step tracing and debugging, or add a printf Statement (through the 232 port) to the MCU program to track the communication data stream between the USB and the window.
Below is the second method I used to install the printf statement in the microcontroller firmware program and use the serial port assistant to intercept the data for your reference.
Step _ PACKET indicates that the step package is received; req indicates the request data sent from the host computer, ANS indicates that the data is returned. If ans = NULL, an empty package is returned.

Step _ PACKET
Req =, 6, 40, 0,
Ans =, FF, FF,
Ans =
Step _ PACKET
Req =,
Ans =
Ans =
Step _ PACKET
Req =, 6,
Ans =, FF, FF,
Ans =
Step _ PACKET
Req =, 6, 9, 0,
Ans =, 0, C0, 32,
Ans =
Step _ PACKET
Req =, 6, FF, 0,
Step _ PACKET
Req =, 6, FF, 0,
Ans =, 0, C0,, 81, 0, A, 9, 4, 2,,
Ans = C0, E, 3, 4C,, 0, 6C,
Ans =
Step _ PACKET
Req =, 6, FF, 0,
Step _ PACKET
Req =, 6, FF, 0,
Step _ PACKET
Req =, 6,
Ans =, FF, FF,
Ans =
Step _ PACKET
Req =, 6, 9, 1,
Ans =, 0, C0,, 81, 0, A, 9, 4, 2,,
Ans = C0, E, 3, 4C,, 0, 6C,
Ans =
Step _ PACKET
Req =,
Ans =
Ans =
Step _ PACKET
Req = A1, 0,
Ans =, 0, 7,
Ans =
Step _ PACKET
Req =, 22, 0,
Ans =
Ans =

2: INF file and SYS driver
The. inf file can be quickly customized using templates. The file format is very simple and can be quickly understood by most Windows programmers. Even if you are not familiar with it, you can quickly identify a file that consists of different parts. The title of each part is included in square brackets, there are one or more bodies composed of parameter names and parameter values in the corresponding section. You can customize only a few parts of the entire file, including:
• The [device list] section includes Vid/PID pairs and other information that are unique to companies and applications that obtain (license) from USB-IF.
• The [Strings] section contains the different strings and identifiers used by the operating system in the plug-and-play phase in different dialogs, And the strings and identifiers used to identify devices in the hardware manager.

The inf file provided in this Article does not need to be modified, but you can modify the • [Strings] section to change the device description string of the device list.
[Strings]
Linux = "spxwh"
Gserial = "FX2N PLC com"
Gserial_display_name = "FX2N PLC com"
The manufacturer is shown as spxwh, and the device is described as FX2N plc com (Mitsubishi PLC simulation comport)

. The sys file is from c: \ windows \ driver cache \ i386. cab File Export (Because windows already has the drive path of the CDC class), but for convenience, I have provided the USB ser directly. SYS. Do not change the name. It should have been set in the INF file.

3: Install the INF file
If everything goes smoothly (the first insertion will prompt you to install the drive path and install the provided inf file), the plug-and-play has completed the installation process, A new serial device appears in the hardware manager. The system automatically uses the next available port number to specify a name for it (for example, if com2 and COM1 have been installed, com3 will appear ). You can use a program that has been compiled to access the actual comx port to access this port (for example, all Super Terminal programs in Windows ). After the connection is completed, there is no difference in functions except for the improved communication speed.

ReferenceArticleAnd materials:
1. Universal Serial Bus class definitions for communication devices
2. A simple method for porting a serial application to a USB Interface
3. avrusb Technology Discussion

Click here to download ourdev_435236.rar (file size: 7.57 MB) (original file name: USB hid .rar)

See hid .doc in.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.