返回> 网站首页
vc下usb接口的实现
yoours2011-01-12 11:10:19
简介一边听听音乐,一边写写文章。
#include <setupApi.h>
#pragma comment(lib, "setupApi.lib")//加载lib文件
//-----------------Initilize USB port---------
unsigned int VendorID,ProductID;
char temp_buffer[100];
unsigned char DataBuffer[1],BufferLength; //接收
unsigned char SendDataBuffer[1],SendBufferLength; //发送
int HIDCounter=0;
GUID hidGuid;
HDEVINFO AllHIDDeviceInfo=NULL;
HANDLE HIDDevice=NULL,ReadHIDDevice=NULL;
HIDD_ATTRIBUTES Attributes;
PSP_DEVICE_INTERFACE_DETAIL_DATA ClassDeviceData;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG neededLength,requiredLength;
DWORD bytesRead;
bool InitUSB()
{
VendorID = 0x0000;
ProductID = 0x0000;
// 查找设备
// 本例程使用HID设备的API,它查找HID设备列表,找出与Vendor ID 和 Product ID匹配的设备
ClassDeviceData =NULL;
HIDDevice =NULL;
deviceInfoData.cbSize =sizeof(deviceInfoData);
// 从操作系统获取HIDs 的GUID
HidD_GetHidGuid(&hidGuid);
// 获取所有HIDs的设备信息
AllHIDDeviceInfo=SetupDiGetClassDevs(&hidGuid,NULL,NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
HIDCounter=0;
while (TRUE)
{
// 这个API将发现的设备信息写入 deviceInfoData
// HIDCounter 允许这个API重复调用所有HID设备
// 如果API调用返回0,没有更多的HID设备发现
if (!SetupDiEnumDeviceInterfaces(AllHIDDeviceInfo,0,&hidGuid,
HIDCounter,&deviceInfoData))
{
// 没有发现与Vendor ID 和 Product ID匹配的HID设备
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
return FALSE;
}
else
{
// 发现一个HID设备,获取设备的详细信息
// 第一次调用SetupDiGetDeviceInterfaceDetail得到ClassDeviceData
// 的大小,但返回错误
SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData,
NULL,0,&requiredLength,NULL);
neededLength =requiredLength;
ClassDeviceData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(neededLength);
ClassDeviceData-> cbSize =sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
//第二次调用SetupDiGetDeviceInterfaceDetail
// 使用 合适的neededLength.
if (!SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData,
ClassDeviceData,neededLength,&requiredLength,NULL))
{
free(ClassDeviceData);
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
return FALSE;
}
// 建立HID设备的句柄
HIDDevice=CreateFile(ClassDeviceData-> DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL);
// 获取 attributes 以便得到Vendor ID 和 Product ID
HidD_GetAttributes(HIDDevice,&Attributes);
if ((Attributes.VendorID == VendorID) &&
(Attributes.ProductID == ProductID))
{
// 找到了匹配的Vendor ID 和 Product ID的HID设备
// 建立ReadHIDDevice设备的句柄以便读取信息
ReadHIDDevice=CreateFile(ClassDeviceData-> DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL);
free(ClassDeviceData);
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
//初始化成功
return TRUE;
}
else
{
CloseHandle(HIDDevice);
}
free(ClassDeviceData);
HIDCounter = HIDCounter+1;
}
}
}
//Read from usb prot,that is listen to usb port ,if it has character,system would display it on user interface
bool ReadUSB()
{
BufferLength=1;
memset(DataBuffer,0,1);
if(!ReadFile(ReadHIDDevice,DataBuffer,BufferLength,&bytesRead,NULL)) //失败
return false; //读数失败立即返回
else
return true;
}
//Wirte a character or a string to usb port
bool WriteUSB()
{
CString str;
SendBufferLength=1;
SendDataBuffer[0]=65;
str=SendDataBuffer;
if (!WriteFile(ReadHIDDevice,SendDataBuffer,SendBufferLength,&bytesRead,NULL))
{
return false;
//失败
}
else
{
AfxMessageBox( "Success! ");
return true;
//成功
}
}
//Thread callback function
void CUsblistenDlg::OnSend()
{
// TODO: Add your control notification handler code here
SendDataBuffer[0]= 'C ';
WriteUSB();
}
void CUsblistenDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
//CString str;
//ReadUSB();
//str=DataBuffer;
//if(DataBuffer[0]!=0)
//{
// CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST1);
// listbox-> AddString(str);
//}
//else
//{
// CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST1);
// listbox-> AddString( "No Charcters is sent to usb port ");
//}
WriteUSB();
CDialog::OnTimer(nIDEvent);
}
文章评论
1574人参与,0条评论