Home > Back-end >  VC6 write a USB to read and write procedures, is actually I download, can compile operation, but can
VC6 write a USB to read and write procedures, is actually I download, can compile operation, but can

Time:09-23

INT ReadReportThread (LPVOID pParam)
{
CUsbDlg * pAppDlg;
DWORD Length;
UINT I;
Cstrings Str.

//remove parameters pParam, and converted into CMyUsbHidTestAppDlg pointer,
//call the member function for the following,
PAppDlg=(CUsbDlg *) pParam;


//this thread is an infinite loop, until the program exits, it withdrew from the
While (1)
{
//set up the event for the invalid state
ResetEvent (ReadOverlapped hEvent);

//if the device has been found
If (MyDevFound==TRUE)
{
If (hReadHandle==INVALID_HANDLE_VALUE)//if read handle invalid
{
PAppDlg - & gt; AddToInfOut (" invalid handle to read the report, may be opened the equipment failure ");
}
The else//otherwise, handle effective
{
//call the ReadFile function request 9 bytes of the report data
Int iRet=ReadFile (hReadHandle,
ReadReportBuffer,
512,
NULL,
& ReadOverlapped);

Int iLastError=GetLastError ();
Cstrings STR.
STR. The Format (" % d, % d ", iRet, iLastError);
AfxMessageBox (STR);
//an error here, the wait will have been waiting for, do not understand,
}

//wait event trigger
WaitForSingleObject (ReadOverlapped hEvent, INFINITE);

//if the device is pulled up during the process of waiting, can also lead to an event, but at the moment MyDevFound
//is set to false, so judge MyDevFound here is false, you can enter the next round of cycle,
If (MyDevFound==FALSE) continue;

//if the device is not unplug, is ReadFile function normal operation is completed,
//by the GetOverlappedResult function to get the actual number of bytes to read.
The GetOverlappedResult (hReadHandle, & amp; ReadOverlapped, & amp; Length, TRUE);

//if you don't to 0 bytes, will read according to the information box
If (Length!=0)
{
PAppDlg - & gt; AddToInfOut (" reads the report "+ pAppDlg - & gt; Itqs (Length) + "byte");
Str="";
for(i=0; I{
Str +=pAppDlg - & gt; Itqs (ReadReportBuffer [I], 16). The Right (2) + "",
}
PAppDlg - & gt; AddToInfOut (Str, FALSE);
}


}
The else
{
//blocking threads, until the next event triggered
WaitForSingleObject (ReadOverlapped hEvent, INFINITE);
}
}
return 0;
}
  • Related