Question: Was in a Windows application and HID device communications, the problem of asynchronous read found lost data, found that the driver layer receives the data is not in the receive buffer waiting for application to read right away, but the loss of uncertain long after the data stored in the cache,
Specific operation is as follows: 1, open the file in asynchronous read way open: Hd=CreateFile (HidFunctionClassData - & gt; DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING FILE_FLAG_OVERLAPPED, NULL);
2, open the Settings after receiving the cache size, and the setting of the readout confirm set success: HidD_SetNumInputBuffers (hd, 128); HidD_GetNumInputBuffers (hd, & amp; InputBufSize);
4, launched a dedicated thread of data received, polling read;
The specific phenomenon: Under a machine every 15 ms upload a frame data, PC every 20 ms receiving time, found that start each receives a frame is lost a frame, after receive the uncertain long data not losing data, until receive the cache is full;
Analysis: If the driver layer of the received data into the receive buffer, so before receiving cache under data is not lost, so now, no doubt is the beginning of the data into the cache, or has been received.
Read function is as follows:
int HIDDeviceRead (HANDLE HANDLE, LPVOID lpBuffer, dwords dwSize) { If (handle==NULL | | handle==INVALID_HANDLE_VALUE) return 0; If (rOverLap hEvent==NULL | | rOverLap. HEvent==INVALID_HANDLE_VALUE) return 0; If (lpBuffer==NULL) return 0;
People found it difficult to control to asynchronously and PC 20 ms you read it once, and underneath is 15 ms a, reading speed is slower than the speed of hair, unavoidably cause problems, why not synchronously? Open a thread to read data block type, if there is no data ReadFile blocks where such guarantee as long as there is data can be received,
CodePudding user response:
Reasonable asynchronous frequency than sent to frequency should read? Otherwise it will lead to packet loss
CodePudding user response:
Add: once every 20 ms read here is a test cache doesn't work this deliberately set,