I have a custom HID device (sensor) transmitting data every 1ms. The dataare received at the PC without losing packets, and thisis confirmed by <g class="gr_ gr_41 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="41" id="41">analysing</g> USB traffic with USBlyzer.
I have written a simple code in C# using WPF to read data under Windows 10 at 1ms update rate. The 16-bit index is included in each packet to check that all packets havebeen received. Debugging info shows that the program reads all the
packets during first 10 seconds, sometimes for much longer. However, on some occasions, after first 10 seconds ofcorrect reading, the update rate drops and packets are now detected only every 5 to 10 ms.
Application priorityis set to high. When restarting the HID <g class="gr_ gr_37 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="37" id="37">initialisation</g>, thenormal reading speed returns back for at least next 10 seconds, sometimes for more than 1 hour.
Is it possible to suggest a way of reading all data without missing individual packets?
Below I included example of the code:
// HID initialization:
privateasyncTask<Boolean>
InitHid()
{
try
{
isHid = false;
string hidDeviceSelector =HidDevice.GetDeviceSelector(usagePage, usageId, vendorId, productId);
hidDeviceInformationCollection = awaitDeviceInformation.FindAllAsync(hidDeviceSelector);
if (hidDeviceInformationCollection.Count > 0)
{
hidDevice = awaitHidDevice.FromIdAsync(hidDeviceInformationCollection.ElementAt(0).Id,FileAccessMode.ReadWrite);
if (hidDevice !=null)
{
// construct a HID output report to send to the device
isHid = true;
Debug.WriteLine("InitHid
ok");
if (!isRegInputReportEventsHid)
{
// Registration Input Report Events from HID
hidDevice.InputReportReceived += HandleInputReportRecieved;
isRegInputReportEventsHid = true;
}
}
}
}
catch
{
isHid = false;
Debug.WriteLine("InitHid
false err");
}
return isHid;
}
// main code to receive data and <g class="gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="35" id="35">analyse</g> for missing packets:
privatevoid HandleInputReportRecieved(
HidDevice sender,
HidInputReportReceivedEventArgs args)
{
HidInputReport inputReport = args.Report;
IBuffer buffer = inputReport.Data;
DataReader dr =DataReader.FromBuffer(buffer);
byte[] bytes =newbyte[inputReport.Data.Length];
dr.ReadBytes(bytes);
int diffSensor = (unt16)((uint)bytes[4]
* 256 + (uint)bytes[5]);
if (testhid != (diffSensor-1))
{
Debug.WriteLine("Testhid
{0} difi {1}; ", testhid, diffSensor);
}
testhid = diffSensor;
}