public void StartAcquisition() { if (usbDeviceReg.Open(out usbDevice)) { 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); } byte epNum = byte.Parse("1"); usbReader = usbDevice.OpenEndpointReader((ReadEndpointID)(epNum | 0x80)); usbReader.DataReceived += USB_DataReceived; usbReader.Flush(); usbReader.DataReceivedEnabled = true; } else { MessageBox.Show("Failed to open USB device.", "USB Connection Error", MessageBoxButtons.OK); } }
private bool openDevice(int index) { bool bRtn = false; closeDevice(); chkRead.CheckedChanged -= chkRead_CheckedChanged; chkRead.Checked = false; cmdRead.Enabled = true; chkRead.CheckedChanged += chkRead_CheckedChanged; if (mRegDevices[index].Open(out mUsbDevice)) { bRtn = true; // 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)) { // 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); } if (bRtn) { if (String.IsNullOrEmpty(comboBoxEndpoint.Text)) comboBoxEndpoint.SelectedIndex = 0; byte epNum = byte.Parse(comboBoxEndpoint.Text); mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(epNum | 0x80)); mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)epNum); mEpReader.DataReceived += mEp_DataReceived; mEpReader.Flush(); panTransfer.Enabled = true; } } if (bRtn) { tsStatus.Text = "Device Opened."; } else { tsStatus.Text = "Device Failed to Opened!"; if (!ReferenceEquals(mUsbDevice, null)) { if (mUsbDevice.IsOpen) mUsbDevice.Close(); mUsbDevice = null; } } return bRtn; }
private bool openDevice(int index) { bool bRtn = false; closeDevice(); if (mRegDevices[index].Open(out mUsbDevice)) { bRtn = true; // 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)) { // 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); } if (bRtn) { byte epNum = endpoint; mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(epNum | 0x80), 64); mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)epNum); mEpReader.DataReceived += mEp_DataReceived; mEpReader.ReadBufferSize = 64; mEpReader.ReadThreadPriority = ThreadPriority.AboveNormal; mEpReader.Flush(); } } if (bRtn) { tsStatus.Text = "Device Opened."; } else { tsStatus.Text = "Device Failed to Opened!"; if (!ReferenceEquals(mUsbDevice, null)) { if (mUsbDevice.IsOpen) mUsbDevice.Close(); mUsbDevice = null; } } return bRtn; }
private void OpenDevice() { if(_usbDevice != null && _usbDevice.IsOpen) return; // Find and open the usb device. _usbDevice = UsbDevice.OpenUsbDevice(_usbFinder); // If the device is open and ready Debug.Assert(_usbDevice != null, string.Format("No device with PID: {0:X4} & VID: {1:X4} Found!", _pid, _vid)); // 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)) { // 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 read endpoint 1. _epReader = _usbDevice.OpenEndpointReader((ReadEndpointID) EpIn); _epReader.Flush(); // open write endpoint 1. _epWriter = _usbDevice.OpenEndpointWriter((WriteEndpointID) EpOut); LibMain.OnConnectedChanged(true); }
public bool openDevice() { bool bRtn = false; closeDevice(); #region integration needed mRegDevices = UsbDevice.AllDevices; GC.Collect(); foreach (UsbRegistry regDevice in mRegDevices) { // add the Vid, Pid, and usb device description to the dropdown display. // NOTE: There are many more properties available to provide you with more device information. // See the LibUsbDotNet.Main.SPDRP enumeration. if (regDevice.Vid == MY_VID && regDevice.Pid == MY_PID) { regDevice.Open(out mUsbDevice); IUsbDevice wholeUsbDevice = mUsbDevice 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); bRtn = true; } } } #endregion if (bRtn) { mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)0x81); mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)0x01); /*mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(epNum | 0x81)); mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)0x01); */ mEpReader.DataReceived += mEp_DataReceived; mEpReader.Flush(); } if (bRtn) { //tsStatus.Text = "Device Opened."; } else { // tsStatus.Text = "Device Failed to Opened!"; if (!ReferenceEquals(mUsbDevice, null)) { if (mUsbDevice.IsOpen) mUsbDevice.Close(); mUsbDevice = null; } } return bRtn; }