VC + + Write USB interface Communication program __c++

Source: Internet
Author: User
Tags guid identifier win32

Using VC + + to write USB interface communication program

Absrtact: In the Visual C + + 6.0 environment, the Windows API (application program Interface) function is used to communicate with the USB interface with the HID device class, and some code of the communication procedure is given.

Key words: Universal Serial Bus Human Interface Device API visualc++

1 Introduction

Before the advent of USB, the typical interface of the computer has parallel port (print port), serial port, mouse mouth, keyboard port, monitor port, game port and various card interface (such as sound card, network card), and these interfaces correspond to have a variety of different cables. In terms of transmission speed, these interfaces have a low speed problem; In technology, this design can easily generate I/O conflicts. Interrupt (IRQ) is not enough, and for each new peripheral must design a new interface card and other shortcomings. Today's computer peripherals are in pursuit of high speed and high-throughput. USB interface adapted to this requirement, and its fast, easy to use, low cost advantages, quickly get a lot of PC manufacturers and semiconductor manufacturers strongly support, peripheral to USB transition is called the inevitable trend.

But if the host PC does not know how to communicate with USB peripherals, then this USB peripheral is not useful at all, and the human-machine interface Device (HID) class is one of the first USB device types that Windows fully supports. On a PC running Windows98 or later, the application can use the operating system's built-in driver and hid communications, but unlike HID communications, it is not as simple as opening a port, setting several parameters, and then reading and writing data. Before an application can exchange data with HID, it needs to find the device to obtain information about its reports, and in order to do so, the application must access the Communication API function to exchange data between the upper-level application and the device driver at the lower level. Applications can use any programming language that can access API functions, and C + + is a powerful language that can access API functions, and this article will write a Windows program that communicates with USB devices in a visualc++6.0 environment.

2 USB Introduction

USB is a new generation interface standard introduced by seven world-famous computer and communication companies such as Intel,compaq,digital,ibm,microsoft,nec,northern Telecom, all known as Universal serial Buses (Universal serial bus). It is a serial communication standard developed to solve the contradiction between the increasing PC peripherals and the limited motherboard slots and ports, especially when the USB2.0 specification of the transmission rate up to 480mbit/s is available, the USB application is more extensive and has subordinate advantages:

(1) suitable for a variety of peripherals, so that it does not need for different peripherals to prepare different interfaces and protocols;

(2) Windows can automatically detect the USB device hot plug, and automatic configuration;

(3) PC machine IRQ line is very scarce, and USB devices do not need to set the port and IRQ, so no matter from the convenience of users, or from the occupation of resources to see, USB has excellent;

(4) When the access to a USB device, the full speed USB interface to 12mbit/s, taking into account the state, control and error information, the maximum theoretical speed can still reach 9.6mbit/s, which is not comparable to other serial interface protocol, and USB also support 1.5mbit/s low-speed transmission;

But USB also has disadvantages, such as: protocol complex, write device driver to consider a lot of details to ensure the transparency of some features of USB, but by calling Win32 API functions to communicate with the device, or with the built-in driver communication, there is no need to understand the complex protocol.

3 VC + + to realize the communication with the USB interface example

Under Windows, any communication with USB peripherals needs to be device-driven, which knows how to communicate with the system's USB drive and access device applications, and Windows contains the various information required by the application and HID communications and does not need to install device drivers. Win32 Application Interface (API) functions enable device drivers to communicate with applications, and applications do not need to communicate with USB devices to understand complex USB protocols.

This simplifies the process of communicating with the hardware by using visualc++ to write the API functions for application calls.

3.1 Establishment of the project

Action steps are as follows:

(1) Open the File menu in the vc++6.0 work platform, select the new menu command, select the Project option in the dialog box, select MFC AppWizard (EXE) in the Left list box, and enter the project name Usbport in the program Name text edit box. Enter the project path in the Location text edit box and click the OK button to enter the MFC AppWizard.

(2) In the MFC APPWIZARD-STEP1 window, select Dialog based option, should not change the default value of other options

(3) in the MFC Appwizard-step2 of 4 window, select the About box and 3D controls check box

(4) in MFC APPWIZARD-STEP3 of 4 to STEP4 of 4 do not change the default settings for each option

(5) Enter the new Project Information window, and if the check is correct, click the OK button to generate all the files required by the application.

The project Usbport based on the dialog box is generated by the above operation.

3.2 Looking for USB devices

Before an application can exchange data with HID, it must first locate the device and obtain information about its report. First find what HID is connected to the system, and then retrieve the information to know the attributes that meet the requirements.

(1) Add member functions. Click the ClassView tab, select the Cusbportdlg class, right-click to add the Onsearch message response function, and increase the private type member variable, which is the string variable strpath and Strlog, and the boolean type variable Bfounddevice.

(2) Onsearch function call API function, HID class device is identified by the GUID type value, call function Hidd_gethidguid to obtain HID device identification: hidd_gethidguid (&guidhid);

Where Guidhid is a pointer to the GUID type, and when the function returns, it points to the GUID identifier of the HID class, a 16-byte structure that identifies the communication interface and the class object, which is defined as:

typedef struct _GUID

{

DWORD Data1;

WORD Data2;

WORD Data3;

BYTE Data4[8];

}guid;

Call function Hidd_gethidguid to get specific HID device properties

BOOL hidd_getattributes (Hcom,&strattrib);

Where hcom is the handle to the selected device, the device to which the handle is concerned, Strattrib is a pointer to the Hidd_attributes type, and the property of the specified device is obtained when the function returns.

The hidd_attributes structure is defined as:

typedef struct _HIDD_ATTRIBUTES

{

ULONG size;//the size of this hidd_attributes variable, in bytes

Vendor identification of USHORT vendorid;//fatal HID Device

Product identification for USHORT productid;//fatal HID Device

Version number of the USHORT Versionnumber;//hid device

}hidd_attributes,*phidd_attributes;

Other hardware-related API functions are also called in the Onsearch function, which are defined in Setupapi.h. Call the Setupdigetclassdevs function to obtain information about a class of hardware devices:

Hdevinfo Hdevinfo = Setupdigetclassdevs (

&guidhid,//This type of device configuration or interface class GUID

null,//a specific string that is used to select a device that meets the criteria

0,//the Top-level form (Top_level Window) handle associated with obtaining information

digcf_present | digcf_deviceinterface//gives the way to set up the information set

);

Call the Setupdienumdeviceinterfaces function to get a device interface information repeatedly called to get all the device interface information. To find a specific device, you can call the function within a loop statement until you find the expected device or function to return a value of false.

Define the function:

BOOL bsuccess = Setupdienumdeviceinterface (

hdevinfo,//an interface handle of interest

null,//a pointer to a SP_DEVINFO_DATA type structure that defines a specific interface

&gudihid,//determines the GUID identifier of the interface

0,//is concerned about the index number, starting with 0

&strinterfacedata,//a pointer to a sp_devinfo_interface_data type,

What it points to is the purpose of calling the function, and when the function returns, Strinterfacedata

The structure to point to has information about the relevant interfaces.

);

Where the structure sp_devinfo_data is defined as:

typedef struct _sp_devinfo_data{

DWORD cbsize;//Specify the size of the structure

GUID identification of the classguid;//device

DWORD devinst;//used to access a handle to a device

ULONG_PTR Reserved;
}sp_devinfo_data,*psp_devinfo_data;

Structure Sp_device_interface_data defined as

typedef struct _sp_device_interface_data{

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.