USB编程实例
The following is a header file and the implementation file that demonstrate the API calls needed to connect to a US B device. These files can also be used from within an application to connect to a usb device.
These file are known to compile under Visual C++ 5.0 on Windows 2000 with the Windows DDK installed. Make sure that the path to the file 'setupapi.h' is in your header file path (Project->Settings). You will also need to add 'se tupapi.lib' and 'hid.lib' to list of libraries and ensure the path to these files is added to the library path. (Project->Set ting...)
The files:
-usb.h(1 kb) ---------------------------------------------------------------
/*
file: usb.h
Author: Alan Macek
Date: March 1, 2001
This file contains the declarations for connecting to USB HID devices.
You are free to use this code for anything you want but please send me
(al@https://www.360docs.net/doc/6114027139.html,) an email telling me what you are using it for and
how it works out. You are NOT ALLOWED to use this code until you send
me an email.
This code comes with absolutely no warranty at all.
*/
#include
#ifdef __cplusplus
extern "C" {
#endif
/* Both of these functions return INVALID_HANDLE_VALUE on an error, if there is no error then the HANDLE must be closed using 'CloseHandle' */
/* Connects to the ith USB HID device connected to the computer */
HANDLE connectToIthUSBHIDDevice (DWORD i);
/* Connects to the USB HID described by the combination of vendor id, product id
If the attribute is null, it will connect to first device satisfying the remaining
attributes. */
HANDLE connectToUSBHIDDevice (DWORD *vendorID, DWORD *productID, DWORD *vers ionNumber);
#ifdef __cplusplus
}
#endif
usb.c(4 kb) ----------------------------
/*
file: usb.c
Author: Alan Macek
Date: March 1, 2000
This file contains the implementation for connecting to USB Hid devices
You are free to use this code for anything you want but please send me
(al@https://www.360docs.net/doc/6114027139.html,) an email telling me what you are using it for and
how it works out. You are NOT ALLOWED to use this code until you send me an email.
This code comes with absolutely no warranty at all.
*/
#include "usb.h"
#include
#include
HANDLE connectToIthUSBHIDDevice (DWORD deviceIndex)
{
GUID hidGUID;
HDEVINFO hardwareDeviceInfoSet;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
ULONG requiredSize;
HANDLE deviceHandle = INVALID_HANDLE_VALUE;
DWORD result;
//Get the HID GUID value - used as mask to get list of devices
HidD_GetHidGuid (&hidGUID);
//Get a list of devices matching the criteria (hid interface, present)
hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices. deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
//Go through the list and get the interface data
result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
NULL, //infoData,
&hidGUID, //interfaceClassGuid,
deviceIndex,
&deviceInterfaceData);
/* Failed to get a device - possibly the index is larger than the number of devices */
if (result == FALSE)
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
return INVALID_HANDLE_VALUE;
}
//Get the details with null values to get the required size of the buffer
SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
NULL, //interfaceDetail,
0, //interfaceDetailSize,
&requiredSize,
0); //infoData))
//Allocate the buffer
deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize); deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
//Fill the buffer with the device details
if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
deviceDetail,
requiredSize,
&requiredSize,
NULL))
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
return INVALID_HANDLE_VALUE;
}
//Open file on the device
deviceHandle = CreateFile (deviceDetail->DevicePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0,
NULL); // No template file
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
return deviceHandle;
}
HANDLE connectToUSBHIDDevice (DWORD *vendorID, DWORD *productID, DWORD *vers ionNumber)
{
HANDLE deviceHandle = INVALID_HANDLE_VALUE;
DWORD index = 0;
HIDD_ATTRIBUTES deviceAttributes;
BOOL matched = FALSE;
while (!matched && (deviceHandle = connectToIthUSBHIDDevice (index)) != INVALID_HA NDLE_VALUE)
{
if (!HidD_GetAttributes (deviceHandle, &deviceAttributes))
return INVALID_HANDLE_VALUE;
if ((vendorID == 0 || deviceAttributes.VendorID == *vendorID) &&
(productID == 0 || deviceAttributes.ProductID == *productID) &&
(versionNumber == 0 || deviceAttributes.VersionNumber == *versionNumber))
return deviceHandle; /* matched */
CloseHandle (deviceHandle); /* not a match - close and try again */
index++;
}
return INVALID_HANDLE_VALUE;
}
You can either download the complete Windows DDK (which used to be available free from th e Microsoft Website at https://www.360docs.net/doc/6114027139.html,/ddk/W2kDDK.htm or download just the few fi les actually needed for the sample code. These are from the Microsoft DDK which used to be freely available.
?HID.LIB(28 kb)
?HIDSDI.H(6 kb)
?SetupAPI.H(138 kb)
?SetupAPI.LIB(340 kb)
?下一篇文章:ir-usb.c fix for 2.5.6-pre3