USB接口读写数据的C++程序,为什么CreateFile返回值错误,代码如下:
#include “stdafx.h”
#include “USBPort.h”
#include “USBPortDlg.h”
#include “setupapi.h”
//#include “hidsdi.h”
#include “hid\hidusage.h”
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern “C” void __stdcall HidD_GetHidGuid(OUT LPGUID hidGuid);
extern “C” BOOLEAN __stdcall HidD_GetAttributes(IN HANDLE device, OUT HIDD_ATTRIBUTES *attributes);
extern “C” BOOLEAN __stdcall HidD_GetManufacturerString(IN HANDLE device, OUT void *buffer, IN ULONG bufferLen);
extern “C” BOOLEAN __stdcall HidD_GetProductString(IN HANDLE device, OUT void *buffer, IN ULONG bufferLen);
extern “C” BOOLEAN __stdcall HidD_GetSerialNumberString(IN HANDLE device, OUT void *buffer, IN ULONG bufferLen);
extern “C” BOOLEAN __stdcall HidD_GetFeature(IN HANDLE device, OUT void *reportBuffer, IN ULONG bufferLen);
extern “C” BOOLEAN __stdcall HidD_SetFeature(IN HANDLE device, IN void *reportBuffer, IN ULONG bufferLen);
extern “C” BOOLEAN __stdcall HidD_GetNumInputBuffers(IN HANDLE device, OUT ULONG *numBuffers);
extern “C” BOOLEAN __stdcall HidD_SetNumInputBuffers(IN HANDLE device, OUT ULONG numBuffers);
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CUSBPortDlg dialog
CUSBPortDlg::CUSBPortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUSBPortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUSBPortDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUSBPortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUSBPortDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUSBPortDlg, CDialog)
//{{AFX_MSG_MAP(CUSBPortDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_FIND, OnSearch)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CUSBPortDlg message handlers
BOOL CUSBPortDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add “About…” menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application””s main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
GetDlgItem(IDC_FIND)->SetFocus();
return FALSE; // return TRUE unless you set the focus to a control
}
void CUSBPortDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUSBPortDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() – cxIcon + 1) / 2;
int y = (rect.Height() – cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUSBPortDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUSBPortDlg::OnSearch()
{
// TODO: Add your control notification handler code here
m_strLog.Empty();
m_strLog = _T(“正在查找系统中HID类的GUID标识…\r\n”);
SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
// 查找本系统中HID类的GUID标识
GUID guidHID;
HidD_GetHidGuid(&guidHID);
static GUID GUID_DEVINTERFACE_USB_DEVICE =
{ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
m_strLog += _T(“HID类的GUID标识为\r\n”);
CString strShow;
strShow.Format(“%08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n”,
guidHID.Data1,guidHID.Data2,guidHID.Data3,guidHID.Data4[0],
guidHID.Data4[1],guidHID.Data4[2],guidHID.Data4[3],guidHID.Data4[4],
guidHID.Data4[5],guidHID.Data4[6],guidHID.Data4[7]);
m_strLog += strShow;
SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
// 准备查找符合HID规范的USB设备
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE,NULL,0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
UINT nIndex = 0;
m_strLog += _T(“正在查找可用的USB设备…\r\n”);
SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
// 查找USB设备接口
SP_DEVICE_INTERFACE_DATA strtInterfaceData;
strtInterfaceData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
BOOL bSuccess = SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&GUID_DEVINTERFACE_USB_DEVICE,0,
&strtInterfaceData);
if (!bSuccess)
{
m_strLog += _T(“抱歉,未找到可用的USB设备”);
SetupDiDestroyDeviceInfoList(hDevInfo);
return;
}
DWORD returnsize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,
NULL,0,&returnsize,NULL);
// 若找到了设备,则读取设备路径名
SP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;/*reinterpret_cast*/
strtDetailData.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,
&strtDetailData,returnsize,&returnsize,NULL))
{
AfxMessageBox(_T(“查找设备路径时出错!”));
SetupDiDestroyDeviceInfoList(hDevInfo);
return;
}
// 找到了设备路径
m_strPath = strtDetailData.DevicePath;
strShow.Format(“可以通过路径%s访问设备\r\n”,m_strPath);
m_strLog += strShow;
SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
// 开放与设备的通信
HANDLE hCom = CreateFile(m_strPath,GENERIC_READ | GENERIC_WRITE,
0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hCom = INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T(“无法打开通信端口”));
SetupDiDestroyDeviceInfoList(hDevInfo);
return;
}
// 查询设备标识
HIDD_ATTRIBUTES strtAttrib;
m_strPath += _T(“正在读取设备的标识…\r\n”);
SetDlgItemText(IDC_EDIT_REPORT,m_strPath);
if (!HidD_GetAttributes(hCom,&strtAttrib))
{
AfxMessageBox(_T(“查询设备状态时出错!”));
CloseHandle(hCom);
SetupDiDestroyDeviceInfoList(hDevInfo);
}
// 将有关该设备的标识显示出来
m_strPath += _T(“已读取,见右边所示\r\n”);
SetDlgItemText(IDC_EDIT_REPORT,m_strPath);
// 显示供应商标识
strShow.Format(“0x%x”,strtAttrib.VendorID);
SetDlgItemText(IDC_ID_VENDOR,strShow);
// 显示产品标识
strShow.Format(“0x%x”,strtAttrib.ProductID);
SetDlgItemText(IDC_ID_PRODUCT,strShow);
// 显示产品版本号
strShow.Format(“%d”,strtAttrib.VersionNumber);
SetDlgItemText(IDC_ID_VERSION,strShow);
// 释放资源
CloseHandle(hCom);
SetupDiDestroyDeviceInfoList(hDevInfo);
}
40分
有时不写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]=”MyLog1.log”;
char logfilename2[]=”MyLog2.log”;
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt||0==pszFmt[0]) return;
_vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,”%04d-%02d-%02d”,now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,”%02d:%02d:%02d”,now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,”%03d”,tb.millitm);
printf(“%s %s.%s %s”,datestr,timestr,mss,logstr);
flog=fopen(logfilename1,”a”);
if (NULL!=flog) {
fprintf(flog,”%s %s.%s %s”,datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,…) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log(“This is a Log %04d from FILE:%s LINE:%d\n”,i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
谢谢 问题找到了。
是 if?(hCom?=?INVALID_HANDLE_VALUE)中条件判断应该为==号的。但是后面还是有问题。createfile成功返回了。但是HidD_GetAttributes(hCom,&strtAttrib)返回出错。。。
下载个HIDAPI这个库 自己看看吧