Exemple #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;
             }
         }
     }
 }
Exemple #2
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();
        }
Exemple #3
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;
        }
Exemple #4
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;
                }
            }
        }
Exemple #5
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();
        }
        public void Close()
        {
            try
            {
                Log.Debug("Closing Crazyradio USB dongle from communication...");

                if (_crazyradioDataEndpointReader != null && !_crazyradioDataEndpointReader.IsDisposed)
                {
                    _crazyradioDataEndpointReader.Dispose();
                }

                if (_crazyradioDataEndpointWriter != null && !_crazyradioDataEndpointWriter.IsDisposed)
                {
                    _crazyradioDataEndpointWriter.Dispose();
                }

                if (_crazyradioUsbDevice.IsOpen)
                {
                    _crazyradioUsbDevice.Close();

                    Log.Debug("Successfully closed Crazyradio USB dongle from communication.");
                }
                else
                {
                    Log.DebugFormat("Crazyradio USB dongle is already closed.");
                }
            }
            catch (Exception ex)
            {
                const string message = "Error closing Crazyradio USB dongle from communication.";
                Log.Error(message, ex);
                throw new CrazyradioDriverException(message, ex);
            }
        }
Exemple #7
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.");
        }
Exemple #8
0
        public void Dispose()
        {
            try
            {
                if (aliveTimer != null)
                {
                    aliveTimer.Change(Timeout.Infinite, Timeout.Infinite);
                }
            }
            catch { }

            try
            {
                reader.Dispose();
                writer.Dispose();
            }
            catch { }

            try
            {
                controlDevice.Close();
            }
            catch { }

            try
            {
                sensorDevice.Close();
            }
            catch { }

            controlDevice = null;

            SensorDataUpdate = null;
            Removed          = null;
        }
Exemple #9
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 #10
0
 public void Close()
 {
     epReader?.Dispose();
     epReader = null;
     epWriter?.Dispose();
     epWriter = null;
     device?.Close();
     device = null;
 }
Exemple #11
0
 public void Close()
 {
     if (device != null)
         device.Close();
     device = null;
     if (epReader != null)
         epReader.Dispose();
     epReader = null;
     if (epWriter != null)
         epWriter.Dispose();
     epWriter = null;
 }
Exemple #12
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();
                    }
                }
            }
        }
Exemple #13
0
 //关闭USB设备
 public void CloseUSB()
 {
     if (!ReferenceEquals(reader, null))
     {
         reader.Dispose();
     }
     if (!ReferenceEquals(writer, null))
     {
         writer.Dispose();
     }
     if (!ReferenceEquals(MyUsbDevice, null))
     {
         MyUsbDevice.Close();
     }
 }
Exemple #14
0
 public void Dispose()
 {
     if (OscillDevice != null)
     {
         OscillDevice.Close();
     }
     if (writer != null)
     {
         writer.Dispose();
     }
     if (reader != null)
     {
         reader.Dispose();
     }
     OscillDevice = null;
 }
Exemple #15
0
        public void Dispose()
        {
            aliveTimer.Change(Timeout.Infinite, Timeout.Infinite);
            reader.Dispose();
            writer.Dispose();
            try
            {
                dev.Close();
            }
            catch { }

            dev = null;

            SensorDataUpdate = null;
            Removed          = null;
        }
        public bool Disconnect()
        {
            if (IsConnected == false)
            {
                throw new Exception("Attempted to Disconnect and already disconnected connection.");
            }

            endpointReader.DataReceived -= EndpointReader_DataReceived;

            endpointReader.Dispose();
            endpointWriter.Dispose();

            pendingRequests.Clear();

            return(true);
        }
Exemple #17
0
 private void CloseUSB()
 {
     if (!ReferenceEquals(reader, null))
     {
         reader.Dispose();
     }
     if (!ReferenceEquals(writer, null))
     {
         writer.Dispose();
     }
     if (!ReferenceEquals(USBDevice, null))
     {
         USBDevice.Close();
         USBDevice = null;
     }
 }
Exemple #18
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;
     }
 }
        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();
                }
            }
        }
Exemple #20
0
 public void Close()
 {
     if (device != null)
     {
         device.Close();
     }
     device = null;
     if (epReader != null)
     {
         epReader.Dispose();
     }
     epReader = null;
     if (epWriter != null)
     {
         epWriter.Dispose();
     }
     epWriter = null;
 }
Exemple #21
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;
            }
        }
Exemple #22
0
 public void GutenTAGClose()
 {
     if (reader != null)
     {
         reader.Dispose();
     }
     if (writer != null)
     {
         writer.Dispose();
     }
     if (MyUsbDevice != null)
     {
         if (MyUsbDevice.IsOpen)
         {
             MyUsbDevice.Close();
         }
     }
     isOpen = false;
 }
Exemple #23
0
 private void closeTestDevice()
 {
     if (!ReferenceEquals(usb, null))
     {
         if (usb.IsOpen)
         {
             mEP1Reader.DataReceived -= mBulkInOut_DataReceived;
             stopReadWrite();
             mEP1Reader.Dispose();
             mEP1Writer.Dispose();
             usb.ReleaseInterface(0);
             usb.SetConfiguration(0);
             usb.ActiveEndpoints.Clear();
             usb.Close();
         }
         usb        = null;
         mEP1Reader = null;
         mEP1Writer = null;
     }
 }
 private void CloseCustomDevice(ref WinUsbDevice usb, ref UsbEndpointReader reader, ref UsbEndpointWriter writer)
 {
     if (reader != null)
     {
         reader.DataReceivedEnabled = false;
         reader.DataReceived       -= CustomDataReceived;
         reader.Dispose();
     }
     reader = null;
     if (writer != null)
     {
         writer.Dispose();
     }
     writer = null;
     if (usb != null)
     {
         usb.Close();
     }
     usb = null;
 }
        public void DisconnectUSB()
        {
            lock (_sync)
            {
                if (SwDevice != null)
                {
                    SendUSB(SwitchCommand.DetachController());
                    if (SwDevice.IsOpen)
                    {
                        if (SwDevice is IUsbDevice wholeUsbDevice)
                        {
                            wholeUsbDevice.ReleaseInterface(0);
                        }
                        SwDevice.Close();
                    }
                }

                reader?.Dispose();
                writer?.Dispose();
            }
        }
 private void Usb_exit()
 {
     reader.DataReceivedEnabled = false;
     reader.DataReceived       -= (OnRxEndPointData);
     this.EndInvoke(ketthuc);
     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();
     }
 }
Exemple #27
0
    void FixedUpdate()
    {
        readerReadData(ref ReadBuffer);

        setCalibrationModifiers(); //Change this later to using only cstick and analog with start and Z as the trigger+
        applyDeadzone();

        #region Debug
        //if (Time.frameCount % 2 == 0)
        //{
        //UnityEngine.Debug.Log("analogX&Y: (" + data.axes[1] + ", " + data.axes[0] + ").");
        //UnityEngine.Debug.Log("ABXY: (" + data.buttons[0] + ", " + data.buttons[1] + ", " + data.buttons[2] + ", " + data.buttons[3] + ").");
        //UnityEngine.Debug.Log("Start: (" +  data.buttons[7] + ").");
        //UnityEngine.Debug.Log("UDLR: (" + data.buttons[8] + ", " + data.buttons[9] + ", " + data.buttons[10] + ", " + data.buttons[11] + ").");
        //UnityEngine.Debug.Log("Analog Calibration Modifiers: (" + calibrModAnalogYPlus + ", " + calibrModAnalogYMinus + ", " + calibrModAnalogXMinus + ", " + calibrModAnalogXPlus + ").");
        //logBoolean = false;
        //}
        #endregion

        im.PassInput(ref data);//send data
        reader.Dispose();
        data.Reset();
    }
        private void closeDevice()
        {
            if (rdr != null)
            {
                rdr.DataReceivedEnabled = false;
                rdr.Dispose();
                rdr = 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 = dev as IUsbDevice;

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

            dev.Close();
            dev = null;
        }
Exemple #29
0
        private void closeDevice()
        {
            if (mDev != null)
            {
                if (mEpReader != null)
                {
                    mEpReader.DataReceivedEnabled = false;
                    mEpReader.DataReceived       -= mEp_DataReceived;
                    mEpReader.Dispose();
                    mEpReader = null;
                }
                if (mEpWriter != null)
                {
                    mEpWriter.Dispose();
                    mEpWriter = null;
                }
                mDev.ReleaseInterface(0);
                mDev.SetConfiguration(0);

                mDev.Close();
                mDev = null;
            }
            panTransfer.Enabled = false;
        }
Exemple #30
0
        public void Run(System.IO.Stream output, int count)
        {
            miBytesReceived = 0;
            BytesReceived   = new byte[TEST_SIZE];
            BytesToSend     = new byte[TEST_SIZE];
            Random rnd = new Random();

            rnd.NextBytes(BytesToSend);

            byte[]       _bTemp = new byte[64];
            StreamWriter sw     = new StreamWriter(output);

            sw.WriteLine("--- Test_BigReadWrite ---"); sw.Flush();

            UsbDevice testDevice = PicTestDevice.TestDevice;

            if (testDevice != null)
            {
                Debug.Assert(testDevice.Open(), "Failed opening device.");

                int ret = 0;
                PicTestDevice.OpenAndConfigure(sw);

                byte bTestType = 0;
                Debug.Assert(PicTestDevice.GetTestType(ref bTestType));

                UsbEndpointReader reader = testDevice.OpenBulkEndpointReader(ReadEndpoints.Ep01);
                UsbEndpointWriter writer = testDevice.OpenBulkEndpointWriter(WriteEndpoints.Ep01);
                if (bTestType != 3)
                {
                    Debug.Assert(PicTestDevice.SetTestType(3, ref bTestType));
                    while (ret >= 0)
                    {
                        ret = reader.Read(_bTemp, 1000);
                        if (ret == 1 && _bTemp[0] == 0x80)
                        {
                            break;
                        }
                    }
                }

                reader.DataReceived += new EventHandler <DataReceivedArgs>(reader_DataReceived);
                for (int iLoop = 0; iLoop < count; iLoop++)
                {
                    miBytesReceived            = 0;
                    reader.DataReceivedEnabled = true;
                    System.Windows.Forms.Application.DoEvents();
                    Debug.Assert(writer.Write(BytesToSend, Timeout.Infinite) == TEST_SIZE);
                    while (ReceiveCount < TEST_SIZE)
                    {
                        System.Windows.Forms.Application.DoEvents();
                        Thread.Sleep(1000);
                    }
                    reader.DataReceivedEnabled = false;
                    System.Windows.Forms.Application.DoEvents();
                    sw.WriteLine("SUCCESS:" + TEST_SIZE); sw.Flush();
                }
                reader.DataReceived -= reader_DataReceived;
                reader.Dispose();
                writer.Dispose();

                PicTestDevice.CloseAndDeConfigure(sw);
            }
        }