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

                    _usbDevice.Close();
                }
                _usbDevice = null;

                // Free usb resources
                UsbDevice.Exit();
            }
        }
Exemple #2
0
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                reader.DataReceivedEnabled = false;
                reader.DataReceived       -= (OnRxEndPointData);
                reader.Dispose();
                writer.Dispose();

                if (MyUsbDevice != null)
                {
                    if (MyUsbDevice.IsOpen)
                    {
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            wholeUsbDevice.ReleaseInterface(0);
                        }
                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;
                    UsbDevice.Exit();
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemple #3
0
        /// <summary>
        /// Disposes any unmanaged resources.
        /// <remarks>Greatly inspired by the examples in the LibUsbDotNet documentation.</remarks>
        /// </summary>
        public void Dispose()
        {
            UsbEndpointReader.Flush();
            UsbEndpointReader.Reset();
            UsbEndpointReader.Dispose();
            UsbEndpointWriter.Flush();
            UsbEndpointWriter.Reset();
            UsbEndpointWriter.Dispose();

            if (UsbDevice == null)
            {
                return;
            }
            if (UsbDevice.IsOpen)
            {
                var wholeUsbDevice = UsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // Release interface #0.
                    wholeUsbDevice.ReleaseInterface(0);
                    Shouter.ShoutLine(4, "Interface released.");
                }

                UsbDevice.Close();
                Shouter.ShoutLine(4, "Device closed.");
            }
            UsbDevice = null;

            // Free usb resources
            UsbDevice.Exit();

            Shouter.ShoutLine(4, "USB resources freed.");
        }
            /// <summary>
            /// Unloads the controller
            /// </summary>
            internal void Unload()
            {
                try
                {
                    if (ControllerDevice != null)
                    {
                        if (UnloadBuffer.Length > 0)
                        {
                            // Send unload buffer to turn off controller
                            int bytesWritten;
                            ControllerDevice.ControlTransfer(ref setupPacket, UnloadBuffer, UnloadBuffer.Length, out bytesWritten);
                        }
                        IUsbDevice wholeUsbDevice = ControllerDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            // Release interface
                            wholeUsbDevice.ReleaseInterface(1);
                        }
                        ControllerDevice.Close();
                        UsbDevice.Exit();
                    }
                }
                catch
                {
                    //Only trying to unload
                }

                if (ControllerDevice != null)
                {
                }
            }
Exemple #5
0
        // Cleanup method - call this when you've finished
        public void Cleanup()
        {
            if ((RobotArm == null))
            {
                return;
            }
            if (!RobotArm.IsOpen)
            {
                return;
            }

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

            RobotArm.Close();
            UsbDevice.Exit();
        }
Exemple #6
0
        public void showInfo()
        {
            // Dump all devices and descriptor information to console output.
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    form.setText(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        form.setText(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            form.setText(interfaceInfo.ToString());

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                form.setText(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }

            // Free usb resources.
            // This is necessary for libusb-1.0 and Linux compatibility.
            UsbDevice.Exit();
        }
Exemple #7
0
        private void buttonExit_Click(object sender, EventArgs e)
        {
            USBcmdTimer.Stop();
            if (reader.DataReceivedEnabled)
            {
                reader.DataReceivedEnabled = false;
                reader.DataReceived       -= (OnRxEndPointData);
            }
            if (MyUsbDevice != null)
            {
                if (MyUsbDevice.IsOpen)
                {
                    IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(0);
                    }
                    MyUsbDevice.Close();
                }
            }
            MyUsbDevice = null;

            UsbDevice.Exit();

            Close();
        }
Exemple #8
0
        string findArrivaledDevice(string serialNumber)
        {
            string           returnStr  = String.Empty;
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            //Debug.WriteLine($"find devices {allDevices.Count}");
            m_log.Add($"find devices {allDevices.Count}");
            for (int index = 0; index < allDevices.Count; index++)
            {
                var       device = allDevices[index];
                UsbDevice usbDevice;
                bool      result = device.Open(out usbDevice);
                m_log.Add($"open device :{serialNumber} registry info page:{result}");
                if (result)
                {
                    if (serialNumber == usbDevice.Info.SerialString)
                    {
                        string[] locationPaths = (string[])device.DeviceProperties["LocationPaths"];
                        P_ID     p_id          = P_ID.NULL;
                        V_ID     v_id          = V_ID.NULL;
                        Enum.TryParse <P_ID>(device.Pid.ToString(), out p_id);
                        Enum.TryParse <V_ID>(device.Vid.ToString(), out v_id);

                        DeviceManufactory man = new DeviceManufactory();
                        man.p_id         = p_id;
                        man.v_id         = v_id;
                        man.company_name = device.FullName;
                        return(filterUsbPort(locationPaths[0], man));
                    }
                }
            }
            UsbDevice.Exit();
            return(returnStr);
        }
Exemple #9
0
        public void reader_DataReceived(object sender, EndpointDataEventArgs e)
        {
            if (run)
            {
                var data   = e.Buffer;
                var input1 = ControllerState.deserialize(getFastInput1(ref data));

                if (gcn1ok)
                {
                    JoystickHelper.setJoystick(ref gcn1, input1, 1, gcn1DZ);
                }
            }
            else
            {
                reader.DataReceivedEnabled = false;

                if (GCNAdapter != null)
                {
                    if (GCNAdapter.IsOpen)
                    {
                        if (!ReferenceEquals(wholeGCNAdapter, null))
                        {
                            wholeGCNAdapter.ReleaseInterface(0);
                        }
                        GCNAdapter.Close();
                    }
                    GCNAdapter = null;
                    UsbDevice.Exit();
                    DriverLog(null, new Logging.LogEventArgs("Closing driver thread..."));
                }
                DriverLog(null, new Logging.LogEventArgs("Driver thread has been stopped."));
            }
        }
Exemple #10
0
        public void close()
        {
            try {
                if (dev != null)
                {
                    if (dev.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 = dev as IUsbDevice;

                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            // Release interface #0.
                            wholeUsbDevice.ReleaseInterface(0);
                        }
                        dev.Close();
                    }
                    dev = null;

                    // Free usb resources
                    UsbDevice.Exit();
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                throw;
            }
        }
Exemple #11
0
        private void USBDeviceRelease()
        {
            if (MyUsbDevice == null)
            {
                return;
            }
            if (MyUsbDevice.IsOpen == false)
            {
                return;
            }
            if (!ReferenceEquals(reader, null))
            {
                // Release interface #0.
                reader.Dispose();
            }
            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 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();
            }
        }
Exemple #13
0
        public override void Close()
        {
            if (_reader != null)
            {
                _reader.Abort();
                _reader = null;
            }

            if (_writer != null)
            {
                _writer.Abort();
                _writer = null;
            }

            if (_usbDevice != null)
            {
                if (_usbDevice.IsOpen)
                {
                    _usbDevice.Close();
                }

                _usbDevice = null;
                UsbDevice.Exit();
            }
        }
Exemple #14
0
        public void Dispose()
        {
            if (_pin2DmdDevice != null)
            {
                var buffer = new byte[2052];

                // reset settings
                buffer[0] = 0x81;
                buffer[1] = 0xC3;
                buffer[2] = 0xE7;
                buffer[3] = 0xFF;
                buffer[4] = 0x07;
                RenderRaw(buffer);
                System.Threading.Thread.Sleep(Delay);

                // close device
                if (_pin2DmdDevice.IsOpen)
                {
                    var wholeUsbDevice = _pin2DmdDevice as IUsbDevice;
                    wholeUsbDevice?.ReleaseInterface(0);
                    _pin2DmdDevice.Close();
                }
            }
            _pin2DmdDevice = null;
            UsbDevice.Exit();
        }
Exemple #15
0
        private void CloseDevice()
        {
            if (_sensorRead != null)
            {
                _sensorRead.DataReceivedEnabled = false;
                _sensorRead.DataReceived       -= SensorRead;
                _sensorRead.Device.Close();
                _sensorRead.Dispose();
            }
            _sensorRead = null;

            if (_controlRead != null)
            {
                _controlRead.DataReceivedEnabled = false;
                _controlRead.DataReceived       -= ControlRead;
                _controlRead.Dispose();
            }
            _controlRead = null;

            if (_controlWrite != null)
            {
                _controlWrite.Dispose();
                _controlWrite.Device.Close();
            }
            _controlWrite = null;
            UsbDevice.Exit();
            PostActions.Clear();
        }
Exemple #16
0
 public static void FreeUsbResources()
 {
     // Free usb resources
     if (PlatformDetector.RunningPlatform() == PlatformDetector.Platform.Linux)
     {
         UsbDevice.Exit();
     }
 }
Exemple #17
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         daplugDevice.Close();
         UsbDevice.Exit();
     }
 }
Exemple #18
0
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            running          = false;
            device_connected = false;
            UsbDevice.Exit();

            Thread.Sleep(100);
            Environment.Exit(0);
        }
Exemple #19
0
 private void OnApplicationQuit()
 {
     if (usbReaderTransfer != null)
     {
         usbReaderTransfer.Dispose();
     }
     UsbDevice.Exit();
     GCNAdapter = null;
 }
Exemple #20
0
 public override void Close()
 {
     if (device != null && device.IsOpen)
     {
         device.Close();
     }
     UsbDevice.Exit();
     device = null;
 }
Exemple #21
0
    /// <summary>
    /// Releases all resource used by the <see cref="UsbTest.uDMX"/> object.
    /// </summary>
    /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="uDMX"/>. The <see cref="Dispose"/>
    /// method leaves the <see cref="uDMX"/> in an unusable state. After calling <see cref="Dispose"/>, you must
    /// release all references to the <see cref="UsbTest.uDMX"/> so the garbage collector can reclaim the memory that the
    /// <see cref="uDMX"/> was occupying.</remarks>
    public void Dispose()
    {
        if (_device != null)
        {
            _device.Close();
        }

        UsbDevice.Exit();
    }
Exemple #22
0
        public override void OpenDevice()
        {
            log.Info("QDLUsb trying to find device");
            UsbDevice.UsbErrorEvent += new EventHandler <UsbError>(UsbErrorEvent);
            UsbRegistry regDev = null;

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                regDev = UsbDevice.AllWinUsbDevices.Find((reg) => reg.Vid == VID && reg.Pid == PID);
            }
            else
            {
                regDev = UsbDevice.AllDevices.Find((reg) => reg.Vid == VID && reg.Pid == PID);
            }
            if (regDev == null)
            {
                log.Error("No QDLUSB Devices found");
                throw new QDLDeviceNotFoundException("Unable to find device");
            }

            if (!regDev.Open(out device) || device == null)
            {
                log.Error("No QDLUSB Devices found");
                throw new QDLDeviceNotFoundException("Unable to open device");
            }

            if (UsbDevice.IsLinux)
            {
                log.Debug("Running on linux, detaching kernel driver");
                MonoUsbDevice monodev = device as MonoUsbDevice;
                if (!monodev.DetachKernelDriver())
                {
                    log.Error("Failed to detach kernel driver");
                    throw new Exception("Failed to detach kernel driver");
                }
            }

            IUsbDevice wholeUsbDevice = device as IUsbDevice;

            if (wholeUsbDevice != null)
            {
                wholeUsbDevice.SetConfiguration(1);
                wholeUsbDevice.ClaimInterface(0);
            }

            reader = device.OpenEndpointReader(ReadEndpointID.Ep01);
            writer = device.OpenEndpointWriter(WriteEndpointID.Ep01, EndpointType.Bulk);
            if (reader == null || writer == null)
            {
                device.Close();
                device = null;
                UsbDevice.Exit();
                log.Error("Failed to open endpoints");
                throw new Exception("Unable to open endpoints");
            }
            log.Info("Found QDLUSB device");
        }
Exemple #23
0
        public override void Close()
        {
            if (m_UsbDevice != null)
            {
                m_UsbDevice.Close();
                m_UsbDevice = null;

                UsbDevice.Exit(); // Free usb resources
            }
        }
        public void CloseDevice()
        {
            if (MyUsbDevice != null)
            {
                MyUsbDevice.Close();
                MyUsbDevice = null;

                UsbDevice.Exit(); // Free usb resources
            }
        }
 public void Destroy()
 {
     #if IOS
     pollThreadRunning = false;
     pollThread.Join(1000);
     #else
     UsbDeviceNotifier.Enabled = false;
     #endif
     UsbDevice.Exit();
 }
Exemple #26
0
 /// <summary>
 ///   Release the USB Device
 /// </summary>
 public void Release()
 {
     if (Device != null && Device.IsOpen)
     {
         DeviceReset();
         Device.Close();
     }
     UsbDevice.UsbErrorEvent -= UsbDeviceOnUsbErrorEvent;
     UsbDevice.Exit();
 }
Exemple #27
0
 /// <summary>
 /// Close the connection
 /// </summary>
 public override void Close()
 {
     try{
         myUsbDevice.Close();
         myUsbDevice = null;
         UsbDevice.Exit();
     }
     catch {}
     isConnected = false;
     ConnectionWasClosed();
 }
Exemple #28
0
 /// <summary>
 /// Free connected USB device
 /// </summary>
 public void DiconnectUSB()
 {
     if (MyUsbDevice != null)
     {
         if (MyUsbDevice.Close())
         {
             MyUsbDevice      = null;
             DeviceInfoStatus = "Disconnected";
         }
     }
     UsbDevice.Exit();
 }
Exemple #29
0
            public static void open()
            {
                ErrorCode ec = ErrorCode.None;

                try
                {
                    MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
                    if (MyUsbDevice == null)
                    {
                        throw new Exception("Device Not Found.");
                    }
                    IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        wholeUsbDevice.SetConfiguration(1);
                        wholeUsbDevice.ClaimInterface(0);
                    }

                    UsbEndpointWriter writer      = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                    byte[]            bytesToSend = { 0x1b, 0x70, 0x00, 0x19, 0xff };

                    int bytesWritten;
                    ec = writer.Write(bytesToSend, 2000, out bytesWritten);
                    if (ec != ErrorCode.None)
                    {
                        throw new Exception(UsbDevice.LastErrorString);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
                }
                finally
                {
                    if (MyUsbDevice != null)
                    {
                        if (MyUsbDevice.IsOpen)
                        {
                            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                            if (!ReferenceEquals(wholeUsbDevice, null))
                            {
                                // Release interface #0.
                                wholeUsbDevice.ReleaseInterface(0);
                            }

                            MyUsbDevice.Close();
                        }
                        MyUsbDevice = null;
                        UsbDevice.Exit();
                    }
                }
            }
Exemple #30
0
 private void OnApplicationQuit()
 {
     usbReaderTransfer.Cancel();
     if (usbReaderTransfer != null)
     {
         usbReaderTransfer.Dispose();
     }
     UsbDevice.Exit();
     GCNAdapter = null;
     gcThread.Abort();
     gcThread = null;
 }