public void readIso() { 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(); 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(); } catch (Exception ex) { form.setText("\r\n"); form.setText((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; } // Free usb resources UsbDevice.Exit(); } }
public static void Main(string[] args) { Error ec = Error.Success; using (UsbContext context = new UsbContext()) { try { UsbInterfaceInfo usbInterfaceInfo = null; UsbEndpointInfo usbEndpointInfo = null; // Find and open the usb device. using (var regList = context.FindAll(MyUsbFinder)) { if (regList.Count == 0) { throw new Exception("Device Not Found."); } // Look through all conected devices with this vid and pid until // one is found that has and and endpoint that matches TRANFER_ENDPOINT. // foreach (var regDevice in regList) { if (regDevice.TryOpen()) { if (regDevice.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)) { MyUsbDevice = regDevice.Clone(); MyUsbDevice.Open(); break; } regDevice.Close(); } } } } // 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.Number); } // open read endpoint. var reader = MyUsbDevice.OpenEndpointReader( (ReadEndpointID)usbEndpointInfo.EndpointAddress, 0, (EndpointType)(usbEndpointInfo.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.Number, 1); transferred = MyUsbDevice.ControlTransfer(setTestTypePacket, ctrlData, 0, 1); #endif TRANFER_SIZE -= (TRANFER_SIZE % usbEndpointInfo.MaxPacketSize); UsbTransferQueue transferQeue = new UsbTransferQueue(reader, TRANFER_MAX_OUTSTANDING_IO, TRANFER_SIZE, 5000, usbEndpointInfo.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 != Error.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 != Error.Success ? 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(); } } }
private static void StartIsochronous2(RichTextBox rtb, RichTextBox _rtb_header, RichTextBox _rtb_error_rate, TextBox tbStatus, TransferParams tParams) { ErrorCode ec = ErrorCode.None; UsbDevice MyUsbDevice = null; int interfaceID = 0; Program.quitFlag = false; try { UsbEndpointReader reader = GetReader(out MyUsbDevice, out interfaceID); int maxPacketSize = reader.EndpointInfo.Descriptor.MaxPacketSize; TRANSFER_SIZE = TRANSFER_MAX_OUTSTANDING_IO * maxPacketSize; //usbEndpointInfo.Descriptor.MaxPacketSize UsbTransferQueue transferQueue = new UsbTransferQueue(reader, TRANSFER_MAX_OUTSTANDING_IO, // # of buffers allocated maxPacketSize, // Transfer size 5000, // Timeout, milliseconds 0); //If you specify zero, this will revert to usbEndpointInfo.Descriptor.MaxPacketSize); transfersCompleted = 0; transfersReceived = 0; packetErrors = 0; mTotalPacketsHandled = 0; apparentPacketsTransmitted = 0; prevPacketCounter = -1; old_remnant = null; UsbTransferQueue.Handle handle; mStartTime = DateTime.Now; sb.Clear(); sbPreviousTransfer.Clear(); while (!Program.quitFlag) { // Begin submitting transfers until TRANFER_MAX_OUTSTANDING_IO has been reached. // then wait for the oldest outstanding transfer to complete. ec = transferQueue.Transfer(out handle); if (transfersCompleted == 0) { SafeAppendText(rtb, "Samples per transfer: " + handle.Context.IsoPacketSize + "\r\n\r\n"); } if (ec != ErrorCode.Success) { // Send any residual text to screen. SafeAppendText(rtb, sb.ToString()); sb.Clear(); throw new Exception("Failed to obtain data from JAGA Penny device"); } // Show some information on the completed transfer. transfersCompleted++; showTransfer(handle, transfersCompleted, tParams); // Count # packets received. tbStatus.BeginInvoke((MethodInvoker) delegate { tbStatus.Text = mTotalPacketsHandled.ToString(); }); // Write string that was obtained from showTransfer() if (sb.Length > 2000) { SafeAppendText(rtb, sb.ToString()); sb.Clear(); } if (sb_header.Length > 0) { SafeAppendText(_rtb_header, sb_header.ToString(), true); sb_header.Clear(); } if (sb_error_rate.Length > 0) { SafeAppendText(_rtb_error_rate, sb_error_rate.ToString(), true); sb_error_rate.Clear(); } Application.DoEvents(); } // Cancels any oustanding transfers and frees the transfer queue handles. // NOTE: A transfer queue can be reused after it's freed. transferQueue.Free(); SafeAppendText(rtb, "\r\nDone!\r\n"); } catch (Exception ex) { SafeAppendText(rtb, "\r\n" + (ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message + " : Error code " + System.Runtime.InteropServices.Marshal.GetLastWin32Error() + "\r\n"); } finally { // If things crash, then some internal data structures in MyUsbDevice will already be disposed, and Close() // will throw exception. Just ignore. try { 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(interfaceID); } MyUsbDevice.Close(); } MyUsbDevice = null; } } catch (Exception ex) { } // Free usb resources UsbDevice.Exit(); prevPacketCounter = -1; } }