电脑可以识别自定义的Hid设备,可是给设备发数据时候总是卡死在写函数,writefile。部分代码如下UINT WriteReportThread(LPVOID pParam)//写线程
{
while(1)
{
ResetEvent(WriteOverlapped.hEvent);
WaitForSingleObject(WriteOverlapped.hEvent, INFINITE);
DataSending = false;
}
return 0;
}
BOOL CUSBDlg::SendData()//发送数据
{
BOOL Result;
UINT LastError;
UINT i;
CString Str,str_send;
DWORD Length = 0;
if(MyDevFind==FALSE)
{
AddToInfoOut(_T("设备未找到"));
return FALSE;
}
if(hWriteHandle==INVALID_HANDLE_VALUE)
{
AddToInfoOut(_T("无效的写报告句柄,可能是打开设备时失败"));
return FALSE;
}
if(DataSending==TRUE)
{
AddToInfoOut(_T("数据正在发送中,暂时不能发送"));
return FALSE;
}
memset(WriteReportBuffer, 0, 512);
WriteReportBuffer[0]=0x00;
CString str_id=itos(WriteReportBuffer[0],16).Right(2);
GetDlgItem(IDC_EDIT_REPORTID)->SetWindowText(str_id);
GetDlgItemText(IDC_ESend, str_send);
UINT count=str_send.GetLength();
for(i=1; i<1+count; i++)
{
WriteReportBuffer[i]=(UCHAR)str_send.GetAt(i-1);
}
WriteReportBuffer[1] = 0x40;
WriteReportBuffer[2] = 0xa2;
WriteReportBuffer[3] = 0x02;
AddToInfoOut(_T("发送信息:1字节报告ID+8字节实际数据"));
Str=_T("");
for(i=0; i<Capabilities.OutputReportByteLength; i++)
{
Str+=itos(WriteReportBuffer[i],16).Right(2)+_T(" ");
}
AddToInfoOut(Str, FALSE);
str_send.Insert(0, _T("发送信息"));
GetDlgItem(IDC_REC_STR)->SetWindowText(str_send);
UpdateWindow();
DataSending=TRUE;
Result=WriteFile(hWriteHandle,
&WriteReportBuffer,
Capabilities.OutputReportByteLength,
&Length,
(LPOVERLAPPED)&WriteOverlapped);
if(Result==FALSE)
{
LastError=GetLastError();
if((LastError==ERROR_IO_PENDING)||(LastError==ERROR_SUCCESS))
{
WaitForSingleObject(WriteOverlapped.hEvent, INFINITE);
DataSending = false;
return TRUE;
}
else
{
DataSending=FALSE;
AddToInfoOut(_T("错误代码:")+itos(LastError));
if(LastError==1)
{
AddToInfoOut(_T("该设备不支持WriteFile函数。"), FALSE);
}
return FALSE;
}
}
AddToInfoOut(_T("数据发送成功!"));
DataSending = FALSE;
return TRUE;
}
|