/// <summary> /// Open a new instance of a USB device using the finder criteria. (Innocuous if device instance already exists). /// This will also enable the Transmit button as well as setup the asynchronous reader and event. /// </summary> void OpenUsbDevice() { if (m_usbDevice != null) { return; } m_usbDevice = LibUsbDotNet.UsbDevice.OpenUsbDevice(m_usbFinder); if (m_usbDevice == null) { Log("No device found with VID={0:X4} PID={1:X4}", m_usbFinder.Vid, m_usbFinder.Pid); return; } transmitButton.Enabled = true; m_usbReader = m_usbDevice.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep01); m_usbReader.DataReceived += new EventHandler <LibUsbDotNet.Main.EndpointDataEventArgs>(UsbReader_DataReceived); m_usbReader.DataReceivedEnabled = true; // UsbRegistryInfo is null in Linux, so only include it if defined. // string sSymbolicInfo = m_usbDevice.UsbRegistryInfo == null ? string.Empty : string.Format(" ({0})", m_usbDevice.UsbRegistryInfo.SymbolicName); Log("Opened device VID={0:X4} PID={1:X4}{2}.", m_usbDevice.Info.Descriptor.VendorID, m_usbDevice.Info.Descriptor.ProductID, sSymbolicInfo); }
/// <summary> /// Gracefully dispose and close the USB device and the related reader. /// </summary> void CloseUsbDevice() { transmitButton.Enabled = false; if (m_usbReader != null) { m_usbReader.Abort(); m_usbReader.Dispose(); m_usbReader = null; } if (null == m_usbDevice) { return; } m_usbDevice.Close(); m_usbDevice = null; Log("Closed device."); }