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; } } } }
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; }
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; } } }
public void Dispose() { Registry = null; IsDisposed = true; if (Device != null) { Writer.Abort(); Writer.Dispose(); Writer = null; if (Device.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 = Device as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } Device.Close(); } Device = null; // Free usb resources UsbDevice.Exit(); } }
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); } }
/// <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."); }
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; }
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(); }
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) { } }
public void Close() { epReader?.Dispose(); epReader = null; epWriter?.Dispose(); epWriter = null; device?.Close(); device = null; }
public void Close() { if (device != null) device.Close(); device = null; if (epReader != null) epReader.Dispose(); epReader = null; if (epWriter != null) epWriter.Dispose(); epWriter = null; }
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(); } } } }
//关闭USB设备 public void CloseUSB() { if (!ReferenceEquals(reader, null)) { reader.Dispose(); } if (!ReferenceEquals(writer, null)) { writer.Dispose(); } if (!ReferenceEquals(MyUsbDevice, null)) { MyUsbDevice.Close(); } }
private void CloseUSB() { if (!ReferenceEquals(reader, null)) { reader.Dispose(); } if (!ReferenceEquals(writer, null)) { writer.Dispose(); } if (!ReferenceEquals(USBDevice, null)) { USBDevice.Close(); USBDevice = 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); }
public void Dispose() { aliveTimer.Change(Timeout.Infinite, Timeout.Infinite); reader.Dispose(); writer.Dispose(); try { dev.Close(); } catch { } dev = null; SensorDataUpdate = null; Removed = null; }
public void Dispose() { if (OscillDevice != null) { OscillDevice.Close(); } if (writer != null) { writer.Dispose(); } if (reader != null) { reader.Dispose(); } OscillDevice = null; }
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(); } } }
public void Close() { if (device != null) { device.Close(); } device = null; if (epReader != null) { epReader.Dispose(); } epReader = null; if (epWriter != null) { epWriter.Dispose(); } epWriter = null; }
public void GutenTAGClose() { if (reader != null) { reader.Dispose(); } if (writer != null) { writer.Dispose(); } if (MyUsbDevice != null) { if (MyUsbDevice.IsOpen) { MyUsbDevice.Close(); } } isOpen = false; }
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; } }
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(); } }
private bool reconnect() { //clear the info so far if (MyUsbDevice != null) { writer.Dispose(); wholeUsbDevice.ReleaseInterface(0); wholeUsbDevice.Close(); MyUsbDevice.Close(); UsbDevice.Exit(); } //now start over MyUsbFinder = new UsbDeviceFinder(0x06D3, 0x01D0); MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) { msgchnl.setcurrMessage("Problem in reconnect() MyUsbDevice is null"); return(false); } else { wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(0); } writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); //Console.WriteLine("New Writer an reader was assigned"); return(true); } }
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; }
public bool UsbConnect(bool buttonclick, int datalength) { if (buttonclick) { try { if (UsbDeviceFinder()) { _usbdevice = UsbDevice.OpenUsbDevice(_usbDeviceFinder); IUsbDevice wholeUsbDevice = _usbdevice as IUsbDevice; if (!(wholeUsbDevice is null)) { wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(0); } _usbreader = _usbdevice.OpenEndpointReader(ReadEndpointID.Ep01); _usbreader.DataReceived += OnRxEndPointData; _usbreader.ReadBufferSize = datalength; _usbreader.Reset(); _usbreader.DataReceivedEnabled = true; _usbwriter = _usbdevice.OpenEndpointWriter(WriteEndpointID.Ep01); } else { return(false); } } catch (Exception ex) { _msgserver.AddWindowsMsg("设备连接失败,请检查!"); return(false); } return(true); } else { if (_usbdevice != null) { _usbreader.DataReceivedEnabled = false; _usbreader.DataReceived -= OnRxEndPointData; _usbwriter.Dispose(); if (_usbdevice.IsOpen) { try { IUsbDevice wholeusbdevice = _usbdevice as IUsbDevice; if (!(wholeusbdevice is null)) { wholeusbdevice.ReleaseInterface(0); } } catch (Exception ex) { return(false); } } _usbdevice.Close(); _usbdevice = null; UsbDevice.Exit(); for (int i = 0; i < _msgserver._usbDeviceList.Count; ++i) { _msgserver._usbDeviceList.RemoveAt(0); } _msgserver.AddMsg(_msgserver._usbBindList, $" > close device !"); _msgserver.AddWindowsMsg("采集设备需要重新上电"); return(true); } else { return(false); } } }
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); } }