public void StartReading()
{
bool channelOne;
ErrorCode ec = new ErrorCode();
try
{
reader.Reset();
// The benchmark device firmware works with this example but it must be put into PC read mode.
#if IS_BENCHMARK_DEVICE
int transferred;
byte[] ctrlData = new byte[1];
UsbSetupPacket setTestTypePacket =
new UsbSetupPacket((byte)(UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor),
0x0E, 0x01, usbInterfaceInfo.Descriptor.InterfaceID, 1);
MyUsbDevice.ControlTransfer(ref setTestTypePacket, ctrlData, 1, out transferred);
#endif
TRANFER_SIZE -= (TRANFER_SIZE % usbEndpointInfo.Descriptor.MaxPacketSize);
mTransferCount = 0;
UsbTransferQueue transferQeue = new UsbTransferQueue(reader,
TRANFER_MAX_OUTSTANDING_IO,
TRANFER_SIZE,
5000,
usbEndpointInfo.Descriptor.MaxPacketSize);
do
{
UsbTransferQueue.Handle handle;
// Begin submitting transfers until TRANFER_MAX_OUTSTANDING_IO has benn reached.
// then wait for the oldest outstanding transfer to complete.
//
ec = transferQeue.Transfer(out handle);
if (ec != ErrorCode.Success)
throw new Exception("Failed getting async result: " + ec.ToString());
// Show some information on the completed transfer.
int seed;
int i = AppSettings.InitialHeader;
seed = 0;
channelOne = true;
int footerCheck = 0;
for (int j = 192; j < handle.Data.Length + 3; j+=196)
{
footerCheck += handle.Data[j] + handle.Data[j + 1] + handle.Data[j + 2] + handle.Data[j + 3];
}
// We've seen the four byte 0 delimiters start at byte 48 and at byte 96. This should reasonably detect where.
bool delimAt96 = footerCheck == 0;
int ch1Counter = 0, ch2Counter = 0;
for (i = AppSettings.InitialHeader; i < handle.Data.Length; i += 2)
{
if (i != 0 && ((delimAt96 && i % 192 == 0) || (!delimAt96 && (i + 48 * 2) % 192 == 0) || (!delimAt96 && i == 48 * 2)))
{
i += 4;
break;
}
if (channelOne)
{
InformationHolder.HighGainContainer().Add(Controller.GraphListComboBoxIndex, ch1SampleCounter++, (Int16)((handle.Data[i]) + (handle.Data[i + 1] << 8)));
ch1Counter+=2;
}
else
{
InformationHolder.LowGainContainer().Add(Controller.GraphListComboBoxIndex, ch2SampleCounter++, (Int16)((handle.Data[i]) + (handle.Data[i + 1] << 8)));
ch2Counter+=2;
}
if (ch1Counter == AppSettings.ChannelOneOffset)
{
ch1Counter = 0;
channelOne = !channelOne;
i += AppSettings.ChannelTwoHeader;
}
if (ch2Counter == AppSettings.ChannelTwoOffset)
{
ch2Counter = 0;
channelOne = !channelOne;
i += AppSettings.ChannelOneHeader;
}
}
//_shouldStopUSB = true;
} while (!_shouldStopUSB);
//Send Parsed Data to MainForm
//MainForm.UpdateZedGraph();
// Cancels any oustanding transfers and free's the transfer queue handles.
// NOTE: A transfer queue can be reused after it's freed.
transferQeue.Free();
rawAccelData.Clear();
Controller.AnalyticsSetConsoleTextboxThreadSafe("Done!" + Environment.NewLine);
}
catch (Exception ex)
{
Controller.AnalyticsSetConsoleTextboxThreadSafe((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
_shouldStopUSB = false;
Thread.CurrentThread.Abort();
}