Home > Software engineering >  Using MFC to do usb communication, the received data of the top part is right, appear behind a pile
Using MFC to do usb communication, the received data of the top part is right, appear behind a pile

Time:11-20



//////////////////////////////////////////////////////////////////////////
Read the report//thread
//is due to the use of asynchronous invocation, and therefore the call ReadFile
//function provides an Overlapped structure, this structure contains an event
//handle, the event is held in a state of no signal at ordinary times, and wait for events function
//will be suspended and the thread is blocked, when the data returned correctly, events are
//triggers, threads run back, and check whether return the amount of data and report ID is
//sure, thus setting of each switch on the interface state, because the function is not
//CMYUSBDlg class (that is, the project of the main window class) in the member function,
//so I can't directly call CMYUSBDlg class member functions,
//, when creating the thread through the pParam parameter passing a this pointer, the parameter pParam
//forced into CMYUSBDlg class pointer can access CMYUSBDlg class
The member functions of a//,
DWORD IsReadReportThread (LPVOID pParam)
{
CMYUSBDlg * pUsbDlg;
DWORD dwLength;
UINT I;
Cstrings strTmp;
Unsigned char ucReadReportBuffer [64]={0};
//memset (ucReadReportBuffer, 0, 64);
//remove parameters pParam, and converted into CMYUSBDlg pointer,
//call the member function for the following,
PUsbDlg=(CMYUSBDlg *) pParam;

//this thread is a dead circulation, until the program exits, it withdrew from the
While (1)
{
//set up the event for the invalid state
ResetEvent (lpReadOverlapped hEvent);

//if the device has been found
If (bMyDevFound)
{
If (hReadHandle!=INVALID_HANDLE_VALUE)//if read handle invalid
{
# ifdef MY_DEBUG
//pUsbDlg - & gt; GainToInfOutPut (" IsReadReportThread thread: effectively handle to read the report ");
# endif
//call the ReadFile function request 9 bytes of the report data
ReadFile (
HReadHandle,
UcReadReportBuffer,
64,
NULL,
& LpReadOverlapped
);
}
The else
# ifdef MY_DEBUG
PUsbDlg - & gt; GainToInfOutPut (" IsReadReportThread thread: invalid handle to read the report ");
# endif
//wait event trigger
WaitForSingleObject (lpReadOverlapped hEvent, INFINITE);

//if the device is pulled up during the process of waiting, can also lead to an event, but at the moment bMyDevFound
//is set to false, so judge bMyDevFound here is false, you can enter the next round of cycle,
If (bMyDevFound==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,
& LpReadOverlapped,
& DwLength,
TRUE
);
//if you don't to 0 bytes, will read according to the information box
If (dwLength!=0)
{
//pUsbDlg - & gt; GainToInfOutPut (" read data "+ pUsbDlg - & gt; Itqs (dwLength) + "byte");
StrTmp="";
For (I=1; i<2; I++)
{
//because itqs, the default is four, so take the right two
StrTmp +=pUsbDlg - & gt; Itqs (ucReadReportBuffer [I], 16). The Right (2) + "",
}
PUsbDlg - & gt; GainToInfOutPut (strTmp, FALSE);

}

CodePudding user response:

Finally, string end have 0
If your data is "1234", the buffer must be five, namely 0 x31, 0 x32, 0 x33, 0 x34, 0 x00

CodePudding user response:

All memory to zero

CodePudding user response:

How to do ah, tried several times all not line

CodePudding user response:

ReadFile may success directly, in addition to set breakpoints in commissioning and see
 
BOOL bRdOK=ReadFile (hReadHandle ucReadReportBuffer, 64, & amp; DwLength, & amp; LpReadOverlapped);
If ((! BRdOk) & amp; & GetLastError ()==ERROR_IO_PENDING)
{
BRdOK=the GetOverlappedResult (hReadHandle, & amp; LpReadOverlapped, & amp; DwLength, TRUE);
}
If (bRdOK)
{
StrTemp. The Format (_T (" Read % u Bytes: "), dwLength);
For (DWORD I=0; i{
Cstrings s; S. ormat (_T (" % 02 x "), (UTIN) ucReadReportBuffer [I]);
StrTemp +=s;
}
If (pUsbDlg) pUsbDlg - & gt; GainToInfOutPut (strTmp, FALSE);
}


CodePudding user response:

Can guarantee a Chinese character is not divided into 2 times to read?

CodePudding user response:

Perm is not really the general memory buffer is not enough, such as an array of problems
  • Related