Refer to the header file in the application's document as follows, and copy the Hidsdi,hidpi.h,hidusage.h,hid.lib,setupapi.lib into the Engineering folder, then invoke the API function to fulfill the requirements.
extern "C" {
//Declare the C libraries used
#include "hidsdi.h" //Must link in hid.lib
#include <setu Papi.h> //must link in setupapi.lib
}
This paper introduces the use of Windows API function to communicate with USB interface of HID device class under visual c++6.0 environment, and gives some code of communication program.
Under Windows, any communication with USB peripherals needs to be driven by a device that knows how to communicate with the system's USB drive and access device applications, Windows
Contains all the information required by the application and HID communications, and no more device drivers are required. WIN32 API functions that enable device drivers to communicate with applications, and applications do not need to communicate with USB devices to understand complex USB protocols.
Using Visual C + + to write applications to invoke API functions simplifies the process of communicating with hardware.
1. Find USB Device
Before an application can exchange data with HID, it must first locate the device and obtain information about its report.
1) hidd_gethidguid (&guidhid) to obtain the identity of the HID device, the HID class device is identified by the GUID type value. A GUID is a 16-byte structure that identifies a communication interface and a class object, defined as:
typedef struct _GUID //size is
{
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE data4[8];
} GUID;
2 also invokes other hardware-related API functions that are defined in setupapi.h.
Setupdigetclassdevs function is used to obtain information of a class of hardware devices, device information set handle Hdevinfo
Hdevinfo Hdevinfo=setupdigetclassdevs (
&guidhid,//This type of configuration or interface class GUID
NULL, //specific string, used to select the device that meets the criteria
0, //top-level form handle digcf_present| related to access information
Digcf_deviceinterface //gives the way to set the information
);
3) Setupdienumdeviceinterface function Gets the device interface information repeatedly called to get all the device interface information Strinterfacedata, 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 , defined as:
Bool Bsuccess=setupdienumdeviceinterface (
hdevinfo,/ /interested interface handle
NULL,// pointer to SP_DEVINFO_DATA type structure , the structure defines the specific interface
&guidhid,//determines the GUID identifier index of the interface,// The index number of interest, with 0 as the starting
&strinterfacedata, //point to Sp_device_interface_data type of pointer, he pointed to the purpose of calling the function, when the function returned, Strinterfacedata point to the structure of the relevant interface information
;
Where the SP_DEVINFO_DATA structure is defined as:
typedef struct sp_devinfo_data{
DWORD cbsize; Specifies the size of the struct
GUID calssguid//Device GUID identification
DWORD devinst; HANDLE ULONG_PTR Reserved used to access the device
;
Sp_devinfo_data,*psp_devinfo_data;
The SP_DEVICE_INTERFACE_DATA structure is defined as follows:
typedef struct sp_device_interface_data{
DWORD cbsize; is the size GUID interfaceclassguid of the sp_device_interface_data structure
;//Specifies the GUID identifier of the interface
DWORD Flags; The state of the interface
ulong_ptr Reserved
} Sp_device_interface_data,*psp_device_interface_data;
4) Setupdigetdeviceinterfacedetail ()
Long Result=setupdigetdeviceinterfacedetail (
hdevinfo,// device information set handle
&strinterfacedata,//device interface information
NULL, //device path
0,// output buffer size
&length,
null);
Called again in order to get Strinterfacedetaildata
Long Result=setupdigetdeviceinterfacedetail (
hdevinfo,
&strinterfacedata,
Strinterfacedetaildata,
Length,
&required,
NULL
);
2. Exchanging data with USB device
In Windows, read-write ports and read-write files both invoke the same API functions, open or create ports with CreateFile, read data from ports with ReadFile, and write data to the port with WriteFile
1) The device is turned on and off
To open or create a device using the API function CreateFile:
HANDLE Hcom=createfile (
strinterfacedetaildata->devicepath,//Specify open device name
generic_read| Generic_write,// allow read/write
0,/ /Exclusive mode
NULL, //Safe Mode
open_existing, //Open
File_ Attribute_nornal,// file attribute
NULL, //Temp file handle
);
If the call succeeds, the function returns the handle to the file, and if the call fails, returns INVALID_HANDLE_VALUE, which should be opened exclusively when the communication device is opened, and the CloseHandle (hcom) function should be called off when the device handle is no longer in use.
2) Bool result = Hidd_getattributes (hcom, &strattrib);
Where hcom is a handle to the selected device, Strattribute is a pointer to the Hidd_attributes type, and the property of the specified device is obtained when the function returns
typedef struct _HIDD_ATTRIBUTES {
ULONG Size; = sizeof (struct _hidd_attributes)
USHORT VendorID; Vendor IDs of this HID device USHORT ProductID;
USHORT versionnumber;
Additional fields would be added to the "end of" this structure.
} Hidd_attributes, *phidd_attributes;
3 The reading and writing of the equipment, read and write communication devices can be implemented in a synchronous manner
HANDLE hcom;
void *pbuffer;
DWORD ilength;
DWORD preadfact;
Bool ReadFile (hcom,pbuffer,ilength,&preadfact,null);
Read the data in the memory pbuffer, pbuffer to apply for memory space, ilength for the length of data to be read, preadfact the actual length of data stored.
Note that before you read and write a device, you should first call the Clearcommerror function to clear the error flag, which is responsible for reporting the current state of the specified error device, and call the Prugecomm function to change the way in which read and write is in progress
3. Example of function module
1) Open the device
In four modules, opening the device is the most complex. Because it needs to enumerate all the devices on the device tree through its device class to get the matching device, and then get the device name
BOOL Deviceopen (handle&handle, Word wvid, Word wpid) {bool BRet = FALSE;
GUID Hidguid;
Hdevinfo Hardwaredeviceinfo;
Sp_interface_device_data Deviceinfodata;
Psp_interface_device_detail_data functionclassdevicedata = NULL;
ULONG predictedlength = 0;
ULONG requiredlength = 0;
CloseHandle (handle);
handle = Invalid_handle_value;
deviceinfodata.cbsize = sizeof (Sp_interface_device_data);
Hidd_gethidguid (&HIDGUID); Hardwaredeviceinfo = Setupdigetclassdevs (&hidguid, Null,null, digcf_present|
Digcf_deviceinterface)); for (int i=0; i<128; i++) {if (!
Setupdienumdeviceinterfaces (Hardwaredeviceinfo, 0,&hidguid, I, &deviceinfodata)) continue;
Setupdigetdeviceinterfacedetail (Hardwaredeviceinfo, &deviceinfodata,null, 0, &requiredlength, NULL);
Predictedlength = Requiredlength;
Functionclassdevicedata = (psp_interface_device_detail_data) malloc (predictedlength); if (!functionclassdeVicedata) continue;
functionclassdevicedata->cbsize = sizeof (Sp_interface_device_detail_data); if (! Setupdigetdeviceinterfacedetail (Hardwaredeviceinfo,&deviceinfodata, Functionclassdevicedata, PredictedLength
, &requiredlength, NULL)) break; Handle = CreateFile (functionclassdevicedata->devicepath,generic_read| Generic_write, file_share_read|
File_share_write, null,open_existing, 0, NULL);
if (handle!= invalid_handle_value) {hidd_attributes Attri;
Hidd_getattributes (handle, &attri); if (Attri. VendorID = = Wvid) && (Attri.
ProductID = = wpid)) {bRet = TRUE;
Break
} closehandle (handle);
handle = Invalid_handle_value;
} setupdidestroydeviceinfolist (Hardwaredeviceinfo);
return bRet; }
2) Turn off the device
It is simpler to turn off the device simply by using the function CloseHandle directly
void Deviceclose (Handle&handle)
{
CloseHandle (HANDLE);
handle = Invalid_handle_value;
}
3) Write Data
Suppose the HID is 8 bytes and the first byte is an ID
BOOL Devicewrite (Handlehandle, lpcvoid lpbuffer, DWORD dwsize)
{
BYTE wbuffer[8] = {0};
DWORD Dwret;
BOOL BRet;
Wbuffer[0] = 0x01;
WBUFFER[1] = 0x00;
memcpy (&wbuffer[2], lpbuffer, min (6, dwsize));
BRet = WriteFile (handle, Wbuffer, 8, &dwret, NULL);
return bRet;
}
4) reading data
BOOL Deviceread (Handlehandle, LPVoid lpbuffer, DWORD dwsize)
{
BYTE rbuffer[8] = {0};
DWORD Dwret;
BOOL BRet;
Rbuffer[0] = 0x01;
RBUFFER[1] = 0xFF;
BRet = WriteFile (handle, Rbuffer, 8, &dwret, NULL);
BRet &= ReadFile (handle, Rbuffer, 8, &dwret, NULL);
memcpy (lpbuffer, &rbuffer[1], min (7, dwsize));
return bRet;
}
Reference Link: http://www.doc88.com/p-283792820785.html