Contains non-driver specific USB device communication members.
This class is compatible with WinUSB, LibUsb-Win32, and linux libusb v1.x. Platform independent applications should only use usb device members from this class. If more functionality is required, it is up to the application to handle multi-driver and/or multi-platfrom requirements.
        /// <summary>
        /// Open the connection
        /// </summary>
        /// <returns>whether the connection was opened successfully</returns>
        public async Task<bool> OpenAsync()
        {
            dev = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(0x10C4, 0xEAC9));
            if (dev == null)
            {
                dev = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(0x10C4, 0xEACA));
                if (dev == null)
                    return false;
            }

            IUsbDevice wholeUsbDevice = dev 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(0);
            }


            writer = dev.OpenEndpointWriter(WriteEndpointID.Ep01, EndpointType.Interrupt);
            reader = dev.OpenEndpointReader(ReadEndpointID.Ep01, 64, EndpointType.Interrupt);

            return true;
        }
        /// <summary>
        ///     Creates and initializes an instance of a Crazyradio USB dongle driver.
        /// </summary>
        /// <param name="crazyradioUsbDevice"> The UsbDevice to use in this driver. </param>
        public CrazyradioDriver(UsbDevice crazyradioUsbDevice)
        {
            Log.Debug("Received UsbDevice to use in CrazyradioDriver.");

            if (crazyradioUsbDevice == null)
            {
                Log.Error("UsbDevice is null.");
                throw new ArgumentNullException("crazyradioUsbDevice");
            }

            _crazyradioUsbDevice = crazyradioUsbDevice;

            if (IsCrazyradioUsbDongle(_crazyradioUsbDevice))
            {
                Log.Debug("UsbDevice is in fact a Crazyradio USB dongle.");

                CheckFirmwareVersion();
            }
            else
            {
                const string message = "UsbDevice is not a Crazyradio USB dongle.";
                Log.Error(message);
                throw new CrazyradioDriverException(message);
            }

            if (_crazyradioUsbDevice.IsOpen)
            {
                Log.Debug("UsbDevice is open. Closing until user opens to prevent inconsistent driver state.");

                _crazyradioUsbDevice.Close();
            }
        }
        private bool GetDelcomBuildLight()
        {
            try
            {
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

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

                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(0);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Пример #4
1
        public Receiver(USBManager manager, UsbDevice device)
        {
            //m_manager = manager;
            //m_reader = device.OpenEndpointReader(readEndpoint);                     // On ouvre le canal de lecture
            //m_engaged = true;
            //readThread = new Thread(this.doRead);

            //// Start to read
            //readThread.Start();
        }
Пример #5
0
        public UsbTool()
        {
            if(MyUsbDevice == null)
            {
                // Find and open the usb device.
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                // 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)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                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.

                    wholeUsbDevice.SetConfiguration(1);

                    wholeUsbDevice.ClaimInterface(0);
                    wholeUsbDevice.ClaimInterface(1);
                }
            }
        }
Пример #6
0
 public CHDKPTPDevice(UsbDevice dev)
     : base(dev)
 {
     this.CHDKVersionMajor = -1;
     this.CHDKVersionMinor = -1;
     this.CHDKSupported = false;
 }
Пример #7
0
        public void CloseDevice()
        {
            if(_usbDevice == null || !_usbDevice.IsOpen)
                return;
            if(_epReader != null) {
                _epReader.Dispose();
                _epReader = null;
            }
            if(_epWriter != null) {
                _epWriter.Abort();
                _epWriter.Dispose();
                _epWriter = null;
            }

            // If this is a "whole" usb device (libusb-win32, linux libusb)
            // it will have an IUsbDevice interface. If not (WinUSB) the
            // variable will be null indicating this is an interface of a
            // device.
            var wholeUsbDevice = _usbDevice as IUsbDevice;
            if(!ReferenceEquals(wholeUsbDevice, null))
                wholeUsbDevice.ReleaseInterface(0); // Release interface #0.

            _usbDevice.Close();
            _usbDevice = null;
        }
Пример #8
0
        /**
         * Finds all of the connected Playstation controllers.
         */
        public static UsbDevice[] getControllers()
        {
            List<UsbDevice> controllersL = new List<UsbDevice>();
            //UsbDevice[] controllers = new UsbDevice[4];
            UsbDevice[] devices = new UsbDevice[20];

            UsbRegDeviceList allDevices = UsbDevice.AllDevices;
            Console.WriteLine("numDevices: " + allDevices.Count);
            int i = 0;
            foreach (UsbRegistry usbRegistry in allDevices)
            {
                //Console.WriteLine("device"+i);
                if (usbRegistry.Open(out devices[i]))
                {
                    Console.WriteLine(devices[i].Info.ToString());
                    if (devices[i].Info.ToString().Contains("PLAYSTATION"))
                    {
                        /*int index = 0;
                        for (int j = 0; j < 4; j++)
                        {
                            if (controllers[j] == null)
                            {
                                index = j;
                                break;
                            }
                        }
                        controllers[index] = devices[i];*/
                        controllersL.Add(devices[i]);
                    }
                }
                i++;
            }

            return controllersL.ToArray();
        }
Пример #9
0
        // Connect to the arm
        public bool Connect()
        {
            // Vendor ID/Product ID here
            UsbDeviceFinder USBFinder = new UsbDeviceFinder(0x1267, 0x0);

            // Try to open the device
            RobotArm = UsbDevice.OpenUsbDevice(USBFinder);

            // Did we connect OK?
            if ((RobotArm == null))
                return false;

            // If this is a "whole" usb device (libusb-win32, linux libusb)
            // it will have an IUsbDevice interface. If not (WinUSB) the
            // variable will be null indicating this is an interface of a
            // device.
            IUsbDevice wholeUsbDevice = RobotArm 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(0);
            }

            // Connected and have interface to the arm
            return true;
        }
Пример #10
0
		public LibUsb_AsyncUsbStream (string port)
		{
			var splited = port.Split ('-');
			var finder = new UsbDeviceFinder (int.Parse (splited [0]), int.Parse (splited [1]));
			device = UsbDevice.OpenUsbDevice (finder);
			if (device == null) {
				throw new Exception ("Failed to find device:" + port);
			}
			if (!device.IsOpen) {
				throw new Exception ("Device is not open:" + port);
			}

			var usbDevice = device as IUsbDevice;
			var interfaceInfo = device.Configs [0].InterfaceInfoList [0];

			if (usbDevice != null) {
				usbDevice.SetConfiguration (device.Configs [0].Descriptor.ConfigID);

				usbDevice.ClaimInterface (interfaceInfo.Descriptor.InterfaceID);
				deviceInterfaceId = interfaceInfo.Descriptor.InterfaceID;
			}

			foreach (var ep in interfaceInfo.EndpointInfoList) {
				if ((ep.Descriptor.EndpointID & 0x80) > 0) {
					reader = device.OpenEndpointReader ((ReadEndpointID)ep.Descriptor.EndpointID);
					reader.DataReceived += HandleDataReceived;
					reader.DataReceivedEnabled = true;
				} else {
					writer = device.OpenEndpointWriter ((WriteEndpointID)ep.Descriptor.EndpointID);
				}
			}
		}
Пример #11
0
        /// <summary>
        /// Try to connect to the device
        /// </summary>
        /// <returns>1 for success, 0 for fail</returns>
        public int connect()
        {
            try
            {
                // Find and open the usb device.
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                // 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)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                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(0);
                }

                return 1; // Device found!
            }
            catch
            {
                return 0; // Failed to find one.
            }
        }
Пример #12
0
        public override void Close()
        {
            try {
                write_lock.WaitOne ();

                if (ep_reader != null) {
                    // detach read event
                    ep_reader.DataReceivedEnabled = false;
                    ep_reader.DataReceived -= (read_usb);
                }

                ep_reader = null;
                ep_writer = null;

                if (IsOpen ()) {
                    // close devices
                    usb_device.Close ();
                    wholeUsbDevice.ReleaseInterface (1);
                    wholeUsbDevice.Close ();
                }

                // release devices
                usb_device = null;
                wholeUsbDevice = null;
                UsbDevice.Exit();
            } catch (Exception) {
                // Ignore everything
            } finally {
                write_lock.ReleaseMutex();
            }
        }
Пример #13
0
 public void open()
 {
     // Find and open the usb device.
     MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
     if (MyUsbDevice == null) throw new Exception("Device Not Found.");
     UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
 }
Пример #14
0
 public void Close()
 {
     if (usbDevice == null)
         return;
     usbDevice.Close();
     usbDevice = null;
 }
Пример #15
0
        private static void InitDevice()
        {
            MyUsbFinder = new UsbDeviceFinder(0x045E, 0x02B0);
            MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

            // 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)
            // it will have an IUsbDevice interface. If not (WinUSB) the
            // variable will be null indicating this is an interface of a
            // device.
            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(0);
            }
        }
Пример #16
0
        /**
         * Finds all of the connected Playstation controllers.
         */
        public static UsbDevice[] getControllers()
        {
            UsbDevice[] controllers = new UsbDevice[4];
            UsbDevice[] devices = new UsbDevice[20];

            UsbRegDeviceList allDevices = UsbDevice.AllDevices;
            int i = 0;
            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out devices[i]))
                {
                    if (devices[i].Info.ToString().Contains("PLAYSTATION"))
                    {
                        int index = 0;
                        for (int j = 0; j < 4; j++)
                        {
                            if (controllers[j] == null)
                            {
                                index = j;
                                break;
                            }
                        }
                        controllers[index] = devices[i];
                    }
                }
                i++;
            }

            return controllers;
        }
        private void DeviceFound(LibUsbDotNet.UsbDevice scopeUsbDevice)
        {
            string serial = null;

            try
            {
                SmartScopeUsbInterfaceLibUsb f = new SmartScopeUsbInterfaceLibUsb(scopeUsbDevice);
                //FIXME: should use ScopeUsbDevice.serial but not set with smartscope
                serial = scopeUsbDevice.Info.SerialString;
                if (serial == "" || serial == null)
                {
                    throw new ScopeIOException("This device doesn't have a serial number, can't work with that");
                }
                if (interfaces.ContainsKey(serial))
                {
                    Common.Logger.Warn("Can't re-register device with this serial " + serial);
                    throw new ScopeIOException("This device was already registered. This is a bug");
                }
                C.Logger.Debug("Device found with serial [" + serial + "]");
                interfaces.Add(serial, f);

                if (onConnect != null)
                {
                    onConnect(f, true);
                }
            }
            catch (ScopeIOException e)
            {
                C.Logger.Error("Error while opening device: " + e.Message);
                if (serial != null)
                {
                    interfaces.Remove(serial);
                }
            }
        }
Пример #18
0
        public static void OpenDevice(Int32 vid, Int32 pid)
        {
            if (usbDevice != null)
                Program.ShowError("A device is already openned, please close it first.");

            Logger.Log("Connecting to device vid." + vid + " pid." + pid);
            UsbDeviceFinder usbFinder = new UsbDeviceFinder(vid, pid);

            // Find and open the usb device.
            usbDevice = UsbDevice.OpenUsbDevice(usbFinder);

            // If the device is open and ready
            if (usbDevice == 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 = usbDevice 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(0);
            }
        }
		public override bool OpenDevice ()
		{
			// Find and open the usb device.
			if (device == null)
				device = UsbDevice.OpenUsbDevice (deviceFinder);

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

			// If this is a "whole" usb device (libusb-win32, linux libusb)
			// it will have an IUsbDevice interface. If not (WinUSB) the 
			// variable will be null indicating this is an interface of a 
			// device.
			IUsbDevice wholeUsbDevice = device 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 (0);
			} else {
				return false;
			}

			connectedToDriver = true;

			return true;
        }
		public override bool Open(out UsbDevice usbDevice)
		{
			usbDevice = null;
			MacUsbDevice macUsbDevice;
			bool bSuccess = Open(out macUsbDevice);
			if (bSuccess)
				usbDevice = macUsbDevice;
			return bSuccess;
		}
        public UsbConnection(UsbRegistry regDevice)
        {
            this.DevicePath = regDevice.SymbolicName;
            device = regDevice.Device;


            SerialNumber = device.Info.SerialString;
            Name = device.Info.ProductString;
            Version = device.Info.Descriptor.BcdDevice;
        }
Пример #22
0
 public PTPDevice(UsbDevice dev)
 {
     _Device = dev;
     PTPSupported = false;
     _Name = dev.Info.ProductString; // TODO: try get better name
     Reader = null;
     Writer = null;
     ConfigurationID = 1;
     InterfaceID = 0;
     ReaderEndpointID = ReadEndpointID.Ep01;
     WriterEndpointID = WriteEndpointID.Ep02;
 }
Пример #23
0
 public PTPDevice(UsbDevice dev)
 {
     this.Device = dev;
     this.PTPSupported = false;
     this._Name = dev.Info.ProductString; // TODO: try get better name
     this.Reader = null;
     this.Writer = null;
     this.ConfigurationID = 1;
     this.InterfaceID = 0;
     this.ReaderEndpointID = ReadEndpointID.Ep01;
     this.WriterEndpointID = WriteEndpointID.Ep02;
 }
Пример #24
0
        public USBFadecandy(int pixelCount = 0, string serialNumber = "") : base(pixelCount)
        {
            if(pixelCount > 512)
                throw new ArgumentOutOfRangeException("pixelCount");

            var packets = pixelCount/21; // Can fit 21 pixels into each packet
            activeBuffer = new byte[64 * packets]; // Each packet needs to be 64 bytes
            queuedBuffer = new byte[64 * packets];

            // Setup control bytes for all packets. Never wiped out, only needs to be done once.
            for (int i = 0; i < packets; i++)
            {
                byte packet_index = (byte)(i & 0x1F); // Bits 0-4, packet index
                byte final = (byte)(i == packets - 1 ? 1 : 0); // Bit 5, final bit (denotes last packet)
                //byte type = 0; // Bits 6-7, type code (0 for video data)

                activeBuffer[i*64] = (byte)(packet_index & (final >> 5)); // First byte in each packet is a bitfield
            }
            Array.Copy(activeBuffer, queuedBuffer, activeBuffer.Length);

            dirty = false;

            UsbDeviceFinder usbFinder = string.IsNullOrEmpty(serialNumber) ? new UsbDeviceFinder(0x1d50, 0x607a) : new UsbDeviceFinder(0x1d50, 0x607a, serialNumber);

            ErrorCode ec = ErrorCode.None;
            
            // Find and open the usb device.
            fcdevice = UsbDevice.OpenUsbDevice(usbFinder);
                
            // If the device is open and ready
            if (fcdevice == null) throw new Exception("Device Not Found.");

            // If this is a "whole" usb device (libusb-win32, linux libusb)
            // it will have an IUsbDevice interface. If not (WinUSB) the 
            // variable will be null indicating this is an interface of a 
            // device.
            IUsbDevice wholeUsbDevice = fcdevice 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(0);
            }

            // open write endpoint 1.
            writer = fcdevice.OpenEndpointWriter(WriteEndpointID.Ep01);
        }
Пример #25
0
		internal void OpenDevice (UsbRegistry registry)
		{
			this._device = registry.Device;

			IUsbDevice wholeUsbDevice = _device as IUsbDevice;

			if (!ReferenceEquals (wholeUsbDevice, null)) {
				// Select config #1
				wholeUsbDevice.SetConfiguration (1);
				// Claim interface #0.
				wholeUsbDevice.ClaimInterface (0);
			}			
		}
Пример #26
0
 public void Close()
 {
     this.Dispose();
     if (MyUsbDevice != null)
     {
         if (MyUsbDevice.DriverMode == UsbDevice.DriverModeType.MonoLibUsb)
         {
             try { MyUsbDevice.Close(); }
             catch { }
         }
         MyUsbDevice = null;
     }
 }
Пример #27
0
        public GarminUnit(UsbDevice Device)
        {
            Configuration = new Dictionary<string, ushort>();

            IUsbDevice wholedevice = Device as IUsbDevice;
            if ( !ReferenceEquals(wholedevice, null))
            {
                wholedevice.SetConfiguration(1);
                wholedevice.ClaimInterface(0);
            }
            Reader = Device.OpenEndpointReader(ReadEndpointID.Ep01);
            Writer = Device.OpenEndpointWriter(WriteEndpointID.Ep02);
        }
Пример #28
0
 public bool Connect()
 {
     foreach (int mode in Enum.GetValues(typeof(DeviceMode)))
     {
         if (! ReferenceEquals((mDevice = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(mVendor, mode))), null))
         {
             //write debug log "OK"
             return true;
         }
     }
     //write debug log "FAIL"
     return false;
 }
Пример #29
0
 public void close()
 {
     if (MyUsbDevice != null)
     {
         if (MyUsbDevice.IsOpen)
         {
             setToBlack();
             update();
             MyUsbDevice.Close();
         }
         MyUsbDevice = null;
         UsbDevice.Exit();
     }
 }
Пример #30
0
        public static void Connect()
        {
            device = UsbDevice.OpenUsbDevice(finder);

            if (device == null)
                throw new DeviceNotFoundException();

            IUsbDevice usbDev = device as IUsbDevice;
            if (!ReferenceEquals(usbDev, null))
            {
                usbDev.SetConfiguration(1);
                usbDev.ClaimInterface(0);
            }
        }
Пример #31
0
        public void Thu()
        {
            ErrorCode        ec = ErrorCode.None;
            UsbRegDeviceList lu = LibUsbDotNet.UsbDevice.AllDevices;

            MyUsbFinder = new UsbDeviceFinder(1614, 33024);
            LibUsbDotNet.UsbDevice MyUsbDevice = null;
            try
            {
                // Find and open the usb device.
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
                //MyUsbDevice = lu[0].Device;
                // 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)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.

                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
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface
                    wholeUsbDevice.ClaimInterface(1);
                }

                // open read endpoint
                UsbEndpointReader reader =
                    MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);

                // open write endpoint0123456789
                UsbEndpointWriter writer =
                    MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep03);

                // write data, read data
                int bytesWritten;
                //ec = writer.Write(new byte[] { 0x00, 0x03, 0x00, 0x00, 0x00,
                // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 2000, out bytesWritten);

                //if (ec != ErrorCode.None)
                //    throw new Exception(UsbDevice.LastErrorString);
                ec = ErrorCode.None;

                byte[] readBuffer = new byte[1024];
                while (ec == ErrorCode.None)
                {
                    int bytesRead;

                    // If the device hasn't sent data in the last 100 milliseconds,
                    // a timeout error (ec = IoTimedOut) will occur.
                    ec = reader.Read(readBuffer, 100, out bytesRead);

                    if (bytesRead == 0)
                    {
                        throw new Exception("No more bytes!");
                    }

                    // Write that output to the console.
                    //  PrintHex(readBuffer, bytesRead);
                }

                //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
                            wholeUsbDevice.ReleaseInterface(1);
                        }

                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;

                    // Free usb resources
                    UsbDevice.Exit();
                }
            }
        }
Пример #32
-22
        public static void CloseDevice()
        {
            if (usbDevice != null)
            {
                if (usbDevice.IsOpen)
                {
                    Logger.Log("Closing device");

                    // 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 = usbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(0);
                    }

                    usbDevice.Close();
                }
                usbDevice = null;

                // Free usb resources
                UsbDevice.Exit();
            }
        }