Пример #1
0
 internal void ClosePort()
 {
     lock (lock_access)
     {
         if (Is_opened)
         {
             oldDevice.Enqueue(srb_reader);
             srb_reader.Flush();
             srb_reader.Dispose();
             srb_reader = null;
             oldDevice.Enqueue(srb_writer);
             srb_writer.Flush();
             srb_writer.Dispose();
             srb_writer = null;
             IUsbDevice wholeUsbDevice = selected_device as IUsbDevice;
             if (!ReferenceEquals(wholeUsbDevice, null))
             {
                 // Release interface #0.
                 wholeUsbDevice.ReleaseInterface(0);
                 wholeUsbDevice.ReleaseInterface((1 | 0x80));
                 wholeUsbDevice.ReleaseInterface(2);
                 wholeUsbDevice.Close();
                 oldDevice.Enqueue(selected_device);
                 selected_device.Close();
                 selected_device = null;
             }
         }
     }
 }
Пример #2
0
        public void closeDevice()
        {
            if (mUsbDevice != null)
            {
                if (mUsbDevice.IsOpen)
                {
                    if (mEpReader != null)
                    {
                        mEpReader.DataReceivedEnabled = false;
                        mEpReader.DataReceived       -= mEp_DataReceived;
                        mEpReader.Dispose();
                        mEpReader = null;
                    }
                    if (mEpWriter != null)
                    {
                        mEpWriter.Abort();
                        mEpWriter.Dispose();
                        mEpWriter = 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.
                    IUsbDevice wholeUsbDevice = mUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(0);
                    }
                    mUsbDevice.Close();
                    mUsbDevice = null;
                }
            }
        }
Пример #3
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();
            }
        }
Пример #4
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();
            }
        }
Пример #5
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();
        }
Пример #6
0
 public void Disconnect()
 {
     if (bkThread != null)
     {
         bkThExit = true;
         bkThInterrupt.Set();
         if (bkThread.ThreadState == System.Threading.ThreadState.Running)
         {
             bkThread.Join();
         }
     }
     bkThread = null;
     if (MyUSBDevice != null)
     {
         if (MyUSBDevice.IsOpen)
         {
             WholeUSBDevice = MyUSBDevice as IUsbDevice;
             if (!ReferenceEquals(WholeUSBDevice, null))
             {
                 WholeUSBDevice.ReleaseInterface(0);
             }
             MyUSBReader.DataReceivedEnabled = false;
             MyUSBReader.DataReceived       -= MyUSBReader_DataReceived;
             MyUSBDevice.Close();
         }
         MyUSBDevice       = null;
         WholeUSBDevice    = null;
         MyUSBDeviceFinder = null;
         MyUSBReader       = null;
         MyUSBWriter       = null;
     }
 }
Пример #7
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)
            {
            }
        }
Пример #8
0
        // De-initialize the controller
        public void UnInit()
        {
            // Always disable and unhook event when done.
            //reader.DataReceivedEnabled = false;
            //reader.DataReceived -= (OnRxEndPointData);

            pollTimer.Stop();
            pollTimer.Elapsed -= (pollTimer_Elapsed);

            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;
        }
Пример #9
0
            /// <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)
                {
                }
            }
Пример #10
0
        private void closeTestDevice(object state)
        {
            if (!ReferenceEquals(mUsbDevice, null))
            {
                if (mUsbDevice.IsOpen)
                {
                    mEP1Reader.DataReceived -= OnDataReceived;
                    stopReadWrite();
                    mUsbDevice.ActiveEndpoints.Clear();
                    UsbDevice.UsbErrorEvent -= OnUsbError;

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

                    mUsbDevice.Close();
                }
                mUsbDevice = null;
                mEP1Reader = null;
                mEP1Writer = null;
            }
            cmdOpenClose.Text = "Open";
            panDevice.Enabled = false;
        }
Пример #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();
        }
Пример #12
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;
            }
        }
Пример #13
0
        public void Dispose()
        {
            if (this.m_usbDevice != null)
            {
                if (this.m_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 = this.m_usbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(this.m_interfaceID);
                    }

                    this.m_usbDevice.Close();
                }
                this.m_usbDevice = null;
            }

            GC.SuppressFinalize(this);
        }
Пример #14
0
 public void Dispose()
 {
     if (monitorBuddyDevice != null)
     {
         monitorBuddyDevice.ReleaseInterface(interfaceID);
         monitorBuddyDevice.Close();
     }
 }
Пример #15
0
        public bool Connect()
        {
            _usbRegistry.Open(out _usbDevice);
            if (_usbDevice == 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 = _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.
                if (!wholeUsbDevice.ClaimInterface(0))
                {
                    wholeUsbDevice.ReleaseInterface(0);
                    _usbDevice.Close();
                    return(false);
                }
            }
            if (_resolverInfo.V45Endpoints == true)
            {
                // Open EP1 for writing, used to send BCP commands/packets to the unit.
                _endpointWriter = _usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

                // Open EP2 for reading, used to send BCP replies to the USB host.
                _endpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep02);

                // Open EP5 for reading, used to send “real time capture data” to the host.
                _realTimeEndpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep05);
            }
            else
            {
                // Open EP2 for writing, used to send BCP commands/packets to the unit.
                _endpointWriter = _usbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);

                // Open EP6 for reading, used to send BCP replies to the USB host.
                _endpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep06);

                // Open EP8 for reading, used to send “real time capture data” to the host.
                _realTimeEndpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep08);
            }
            return(true);
        }
Пример #16
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();
                    }
                }
            }
 private void Close()
 {
     if (this.MyUsbDevice != null)
     {
         IUsbDevice wholeUsbDevice = this.MyUsbDevice as IUsbDevice;
         if (!ReferenceEquals(wholeUsbDevice, null))
         {
             wholeUsbDevice.ReleaseInterface(0);
         }
         this.MyUsbDevice.Close();
     }
     this.MyUsbDevice = null;
 }
Пример #18
0
 /// <summary>
 /// Cleans up a device, releasing interfaces and closing connection
 /// </summary>
 /// <param name="device">The device to close</param>
 private void Destroy(IUsbDevice device)
 {
     try
     {
         // Attempt to clean up device
         device.ReleaseInterface(device.Configs[0].Interfaces[0].Number);
         device.Close();
     }
     catch (Exception e)
     {
         _logger.LogWarning(e, "Attempted to clean up device, but failed.");
     }
 }
Пример #19
0
        public void disconnect()
        {
            lock (DeviceAccessorySyncLock)
            {
                onDeviceDisconnected();

                shutdown = true;
                lock (messageQueue)
                {
                    Monitor.PulseAll(messageQueue);
                }

                if (MyUsbDevice != null)
                {
                    UsbDevice TempUsbDevice = MyUsbDevice;
                    MyUsbDevice = null;
                    try
                    {
                        if (TempUsbDevice.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 = TempUsbDevice as IUsbDevice;
                            if (!ReferenceEquals(wholeUsbDevice, null))
                            {
                                // Release interface #0.
                                wholeUsbDevice.ReleaseInterface(0);
                            }
                        }
                        TempUsbDevice.Close();
                    }
                    finally
                    {
                        // Free usb resources
                        if (reader != null && !reader.IsDisposed)
                        {
                            reader.Dispose();
                        }
                        if (writer != null && !writer.IsDisposed)
                        {
                            writer.Dispose();
                        }
                        UsbDevice.Exit();
                    }
                }
            }
        }
Пример #20
0
 public void Close()
 {
     if (Driver.IsOpen)
     {
         IUsbDevice wholeUsbDevice = Driver as IUsbDevice;
         if (wholeUsbDevice is not null)
         {
             // Release interface #0.
             wholeUsbDevice.ReleaseInterface(0);
         }
         Driver.Close();
     }
     DeviceInfo.Ready = Driver.IsOpen;
 }
Пример #21
0
        public bool libUSBCloseEP()
        {
            setStetOffStream = false;
            while (getStetOffStream)
            {
            }
            ;

            // Stope readUSB rhread
            if (readLibUSBThread.IsAlive)
            {
                readLibUSBThread.Abort();
                readLibUSBThread.Join(10000);
            }

            //
            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(0);
                        }
                        MyUsbDevice.Close();
                    }
                }
                MyUsbDevice = null;

                // Free usb resources
                UsbDevice.Exit();
            }
            catch
            {
                return(true);
            }

            return(true);
        }
Пример #22
0
        public bool Close()
        {
            if (!IsOpen)
            {
                return(false);
            }

            IUsbDevice whole = _Device as IUsbDevice;

            if (!ReferenceEquals(whole, null))
            {
                whole.ReleaseInterface(InterfaceID);
            }

            return(_Device.Close());
        }
Пример #23
0
 public override void Close()
 {
     if (_reader is object)
     {
         _reader.Dispose();
     }
     if (_writer is object)
     {
         _writer.Dispose();
     }
     if (_dev is object)
     {
         _dev.ReleaseInterface(0);
         _dev.Close();
         _dev = null;
     }
 }
Пример #24
0
        public override void Close()
        {
            if (_device != null && _device.IsOpen)
            {
                IUsbDevice wholeUsbDevice = _device as IUsbDevice;

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

                _device.Close();
            }

            base.Close();
        }
Пример #25
0
        internal void Disconnect()
        {
            try {
                if (PinConfig != null)
                {
                    PinConfig.Abort();
                    PinConfig.Dispose();
                    PinConfig = null;
                }
                if (pinState != null)
                {
                    pinState.DataReceivedEnabled = false;
                    pinState.DataReceived       -= pinState_DataReceived;               // TODO: sometimes pinState is null when we get here.
                    pinState.Dispose();
                    pinState = null;
                }
                if (CommsConfig != null)
                {
                    CommsConfig.Abort();
                    CommsConfig.Dispose();
                    CommsConfig = null;
                }
                if (CommsReceive != null)
                {
                    CommsReceive.Dispose();
                    CommsReceive = null;
                }
            }
            catch (Exception e)
            {
            }

            if (usb != null)
            {
                if (usb.IsOpen)
                {
                    IUsbDevice wholeUsbDevice = usb as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        wholeUsbDevice.ReleaseInterface(0);
                    }
                    usb.Close();
                }
            }
        }
Пример #26
0
        public void Close()
        {
            if (_device != null)
            {
                if (_device.IsOpen)
                {
                    IUsbDevice whole = _device as IUsbDevice;

                    if (!ReferenceEquals(whole, null))
                    {
                        whole.ReleaseInterface(1);
                    }

                    _device.Close();
                    UsbDevice.Exit();
                }
            }
        }
Пример #27
0
        public void Disconnect()
        {
            lock (_sync)
            {
                if (SwDevice != null)
                {
                    if (SwDevice.IsOpen)
                    {
                        IUsbDevice?wholeUsbDevice = SwDevice as IUsbDevice;
                        wholeUsbDevice?.ReleaseInterface(0);
                        SwDevice.Close();
                    }
                }

                reader?.Dispose();
                writer?.Dispose();
                Connected = false;
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (device != null && device.IsOpen)
                    {
                        IUsbDevice wholeUsbDevice = device as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            wholeUsbDevice.ReleaseInterface(interfaceId);
                        }
                        device.Close();
                    }
                }

                disposedValue = true;
            }
        }
Пример #29
0
        //--------------------------------------------------------------------------------------------------------
        public static void Close()
        {
            if (USBDev != null)
            {
                if (USBDev.IsOpen)
                {
                    IUsbDevice wholeUsbDevice = USBDev as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        wholeUsbDevice.ReleaseInterface(0);
                    }

                    USBDev.Close();
                }

                USBDev = null;

                UsbDevice.Exit();
            }
        }
Пример #30
0
        private static void ReleaseResourcesIfRequired()
        {
            if (MyUsbDevice != null)
            {
                if (MyUsbDevice.IsOpen)
                {
                    _orb.TurnLightningOff();
                    IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface
                        wholeUsbDevice.ReleaseInterface(1);
                    }

                    MyUsbDevice.Close();
                }
                MyUsbDevice = null;
                // Free usb resources
                UsbDevice.Exit();
            }
        }