Free() public method

Cancels and frees all oustanding transfers.
public Free ( ) : void
return void
Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            ErrorCode ec = ErrorCode.None;

            try
            {
                // Find and open the usb device.
                UsbRegDeviceList regList = UsbDevice.AllDevices.FindAll(MyUsbFinder);
                if (regList.Count == 0) throw new Exception("Device Not Found.");

                UsbInterfaceInfo usbInterfaceInfo = null;
                UsbEndpointInfo usbEndpointInfo = null;

                // Look through all conected devices with this vid and pid until
                // one is found that has and and endpoint that matches TRANFER_ENDPOINT.
                //
                foreach (UsbRegistry regDevice in regList)
                {
                    if (regDevice.Open(out MyUsbDevice))
                    {
                        if (MyUsbDevice.Configs.Count > 0)
                        {
                            // if TRANFER_ENDPOINT is 0x80 or 0x00, LookupEndpointInfo will return the 
                            // first read or write (respectively).
                            if (UsbEndpointBase.LookupEndpointInfo(MyUsbDevice.Configs[0],TRANFER_ENDPOINT, 
                                out usbInterfaceInfo, out usbEndpointInfo))
                                break;

                            MyUsbDevice.Close();
                            MyUsbDevice = null;
                        }
                    }
                }

                // If the device is open and ready
                if (MyUsbDevice == null) throw new Exception("Device Not Found.");

                // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                // it exposes an IUsbDevice interface. If not (WinUSB) the 
                // 'wholeUsbDevice' variable will be null indicating this is 
                // an interface of a device; it does not require or support 
                // configuration and interface selection.
                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used, 
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(usbInterfaceInfo.Descriptor.InterfaceID);
                }

                // open read endpoint.
                UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(
                    (ReadEndpointID) usbEndpointInfo.Descriptor.EndpointID,
                    0,
                    (EndpointType) (usbEndpointInfo.Descriptor.Attributes & 0x3));

                if (ReferenceEquals(reader, null))
                {
                    throw new Exception("Failed locating read endpoint.");
                }

                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);

                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");

                    // Show some information on the completed transfer.
                    showTransfer(handle, mTransferCount);
                } while (mTransferCount++ < TRANSFER_COUNT);

                // Cancels any oustanding transfers and free's the transfer queue handles.
                // NOTE: A transfer queue can be reused after it's freed.
                transferQeue.Free();

                Console.WriteLine("\r\nDone!\r\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
            }
            finally
            {
                if (MyUsbDevice != null)
                {
                    if (MyUsbDevice.IsOpen)
                    {
                        // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                        // it exposes an IUsbDevice interface. If not (WinUSB) the 
                        // 'wholeUsbDevice' variable will be null indicating this is 
                        // an interface of a device; it does not require or support 
                        // configuration and interface selection.
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            // Release interface #0.
                            wholeUsbDevice.ReleaseInterface(0);
                        }

                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;
                }

                // Wait for user input..
                Console.ReadKey();

                // Free usb resources
                UsbDevice.Exit();
            }
        }
Ejemplo n.º 3
0
        public void StartReading()
        {
            ErrorCode ec = new ErrorCode();
            writer = new StreamWriter("Data.txt");
            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);


                int seed;
                int i;
                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.
                    seed = 0;
                    for (i = 0; i < handle.Data.Length; i += 2)
                    {
                        InformationHolder.Instance().zedGraphData.Add(MainForm.graphXIndex++, (Int16)((handle.Data[i]) + (handle.Data[i + 1] << 8)));
                        //writer.Write((Int16)((rawAccelData[i]) + (rawAccelData[i + 1] << 8)) + ",");

                        //Only read one channel
                        if (i == seed + 94)
                        {
                            i += 100;
                            seed = i + 2;
                        }
                    }
                } 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();
                MainForm.SetConsoleTextboxThreadSafe("Done!\n");
            }
            catch (Exception ex)
            {
                MainForm.SetConsoleTextboxThreadSafe((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
            }
            writer.Close();
            _shouldStopUSB = false;
            Thread.CurrentThread.Abort();
        }