public void usbConnect() // The name is self-explanatory... { // Find and open the usb device. mUsbDevice = UsbDevice.OpenUsbDevice(mini2440Finder); if (mUsbDevice == null) { mUsbDevice = UsbDevice.OpenUsbDevice(mini6410Finder); } // If we fail to find or connect to the device if (mUsbDevice == null) { l_usbFound.Text = "Error"; t_log.AppendText("\r\n" + "## Could not find or open device." + "\r\n" + "Please plug the device, I will detect it." + "\r\n"); b_download.Enabled = false; b_upload.Enabled = false; } // If the device is open and ready else { l_usbFound.Text = "Connected"; if (mUsbDevice.UsbRegistryInfo.Pid == pid2440) { t_log.AppendText("\r\n" + "## Mini2440 connected." + "\r\n"); } if (mUsbDevice.UsbRegistryInfo.Pid == pid6410) { t_log.AppendText("\r\n" + "## Mini6410 connected." + "\r\n"); } b_download.Enabled = true; b_upload.Enabled = true; mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(byte.Parse("1") | 0x80)); // = 81 mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)byte.Parse("3")); if (mEpWriter.EndpointInfo == null) // Try opening endpoint 4 if endpoint 3 is unavailable (i.e. after usb reset when downloading firmware) { mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)byte.Parse("4")); } //mEpReader.Flush(); mEpWriter.Flush(); 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); } } }
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; } } } }
private bool OpenCustomDevice(out WinUsbDevice usb, out UsbEndpointReader reader, out UsbEndpointWriter writer) { usb = null; reader = null; writer = null; var device = UsbDevice.AllWinUsbDevices.FirstOrDefault(d => d.Vid == 0x1974 && d.Pid == 0x0001) as WinUsbRegistry; if (device != null) { if (device.Open(out usb)) { writer = usb.OpenEndpointWriter(WriteEndpointID.Ep01); writer.Reset(); writer.Flush(); reader = usb.OpenEndpointReader(ReadEndpointID.Ep01); reader.Reset(); reader.Flush(); reader.ReadBufferSize = 32; reader.DataReceived += CustomDataReceived; reader.DataReceivedEnabled = true; return(true); } } return(false); }
private static void InitalizeAndVerify() { bool Success = false; while (Success == false) { Writer = DI_2008.OpenEndpointWriter(WriteEndpointID.Ep01); Reader = DI_2008.OpenEndpointReader(ReadEndpointID.Ep01); //No clue what these do but they keep the device from hanging on program restarts #TrialAndError Writer.Abort(); Writer.Reset(); Writer.Flush(); Reader.Abort(); Reader.ReadFlush(); Reader.Flush(); Reader.Reset(); InternalFunctions.Write("stop"); //Make sure the device wasnt left in a scan state try { var DeviceResponding = InternalFunctions.Write("info 0"); Success = DeviceResponding.Contains("DATAQ"); } catch { } } }
/// <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 bool Disconnect() { if (_usbDevice != null) { if (_usbDevice.IsOpen) { _endpointWriter.Flush(); _endpointReader.Flush(); _realTimeEndpointReader.Flush(); // 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 #0. wholeUsbDevice.ReleaseInterface(0); } if (FailedInit || resetWhenDisconnected) { if (_usbDevice is LibUsbDevice) { var libUsbDevice = (LibUsbDevice)_usbDevice; //coming out of sleep the device needs to be reset. or sometimes the vivo gets weird so its best to reset libUsbDevice.ResetDevice(); } } _usbDevice.Close(); } _usbDevice = null; } return(true); }
private bool OpenDevice() { if (_controlWrite == null) { var device = UsbDevice.AllWinUsbDevices.FirstOrDefault(d => { var dev = d as WinUsbRegistry; return(dev.Vid == 0x054C && dev.Pid == 0x09AF && dev.InterfaceID == 5); }) as WinUsbRegistry; if (device != null) { WinUsbDevice _usb; if (device.Open(out _usb)) { PostActions.Clear(); _controlRead = _usb.OpenEndpointReader(ReadEndpointID.Ep04); _controlRead.Reset(); _controlRead.Flush(); _controlRead.ReadBufferSize = 64; _controlRead.DataReceived += ControlRead; _controlRead.DataReceivedEnabled = true; _controlWrite = _usb.OpenEndpointWriter(WriteEndpointID.Ep04); _controlWrite.Reset(); _controlWrite.Flush(); //ActivateDisplay(true, () => //{ // LedControl(true, () => // { // EnableTracking(true, () => // { // SetVRMode(true); // }); // }); //}); } } } if (_sensorRead == null) { var device = UsbDevice.AllWinUsbDevices.FirstOrDefault(d => { var dev = d as WinUsbRegistry; return(dev.Vid == 0x054C && dev.Pid == 0x09AF && dev.InterfaceID == 4); }) as WinUsbRegistry; if (device != null) { WinUsbDevice _usb; if (device.Open(out _usb)) { _sensorRead = _usb.OpenEndpointReader(ReadEndpointID.Ep03); _sensorRead.Reset(); _sensorRead.Flush(); _sensorRead.ReadBufferSize = 64; _sensorRead.DataReceived += SensorRead; _sensorRead.DataReceivedEnabled = true; } } } return(_sensorRead != null && _controlWrite != null); }
/// <summary> /// The main entry point of the application. /// </summary> static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine("======================="); Console.WriteLine("= LG360VR Activator ="); Console.WriteLine("======================="); Console.WriteLine(); // Attempt to open the device using its VID/PID. Console.WriteLine("Searching for device..."); UsbDevice device; try { device = UsbDevice.OpenUsbDevice( new UsbDeviceFinder(DeviceVID, DevicePID)); if (device == null) { throw new Exception("Device not found"); } } catch (Exception exc) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: The device couldn't be opened (" + exc.Message + ")."); Console.WriteLine("Please make sure the device is " + "connected and its default drivers have been replaced " + "with WinUSB drivers (>=6.1)."); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Any key to exit."); Console.ReadKey(true); return; } Console.WriteLine("Device found! Attempting to enable display..."); try { ErrorCode code; // First, listen to everything the device has to say. // As it turns out, if we're not gonna do that, the device // will pout and refuse to listen to what we have to say. byte[] buffer = new byte[1024]; using (UsbEndpointReader reader = device.OpenEndpointReader(ReadEndpointID.Ep01)) { int readCount; do { code = reader.Read(buffer, 1000, out readCount); }while (readCount > 0); } if (code != ErrorCode.Success && code != ErrorCode.IoTimedOut) { throw new Exception("Transfer read error '" + code.ToString() + "'"); } // After reading everything what we could from the device, we // will send commands to both disable the proximity sensor and // then turn on the displays. After this, the display should // be turned on. using (UsbEndpointWriter writer = device.OpenEndpointWriter(WriteEndpointID.Ep01)) { code = writer.Write(BuildCommand("Sleep Disable"), 1000, out _); if (code != ErrorCode.Success) { throw new Exception("Transfer write error '" + code.ToString() + "'"); } code = writer.Write(BuildCommand("VR App Start"), 2000, out _); if (code != ErrorCode.Success) { throw new Exception("Transfer write error '" + code.ToString() + "'"); } writer.Flush(); } } catch (Exception exc) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: Communication failure (" + exc.Message + ")."); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Any key to exit."); Console.ReadKey(true); return; } finally { device.Close(); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Process completed - the display should " + "now be turned on."); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Program will close in 2 seconds."); Thread.Sleep(2000); return; }
public override void Flush() { writer.Flush(); }
public override void Flush() { _reader?.Flush(); _writer?.Flush(); }