/// <inheritdoc /> public IEnumerable <string> Discover() { lock (_discoveryLock) { UsbDeviceCollection?usbDeviceCollection = _context.List(); _context.SetDebugLevel(LibUsbDotNet.LogLevel.Debug); // Filter out all but Concept2 Vendor var discoveredDevices = usbDeviceCollection.Where(d => d.VendorId == VendorId); // Discover disconnected device based on location foreach (UsbDevice detachedDevice in _devices.Values.Except(discoveredDevices, new IUsbDeviceComparer())) { Location location = new (detachedDevice.BusNumber, detachedDevice.Address); // If events are enabled, fire one for the device being lost EventArgs args = new DeviceEventArgs(location); DeviceLost?.Invoke(this, args); // Clean up device Destroy(detachedDevice); // Remove from devices _devices.Remove(location, out _); } // Discover new devices using Location as the comparer foreach (UsbDevice foundDevice in discoveredDevices.Except(_devices.Values, new IUsbDeviceComparer())) { if (!IsValidDevice(foundDevice)) { _logger.LogWarning("Invalid device encountered. Not being added to the device list."); } Location location = new (foundDevice.BusNumber, foundDevice.Address); if (!foundDevice.IsOpen) { Initialize(foundDevice); } string serialNumber = foundDevice.Info.SerialNumber; // If events are enabled, fire one for the device being found EventArgs args = new DeviceEventArgs(location) { SerialNumber = serialNumber }; DeviceFound?.Invoke(this, args); // Add to devices _devices.TryAdd(location, serialNumber, foundDevice); } return(_devices.SerialNumbers); } }
public static void Main(string[] args) { using (var context = new UsbContext()) { context.SetDebugLevel(LogLevel.Info); //Get a list of all connected devices var usbDeviceCollection = context.List(); //Narrow down the device by vendor and pid var selectedDevice = usbDeviceCollection.FirstOrDefault(d => d.ProductId == ProductId && d.VendorId == VendorId); if (selectedDevice != null) { Console.WriteLine("Found USB device in DFU mode..."); //Open the device selectedDevice.TryOpen(); //Get the first config number of the interface selectedDevice.ClaimInterface(selectedDevice.Configs[0].Interfaces[0].Number); Console.WriteLine(GetDeviceInfo(selectedDevice)); dfu.SetUsb(selectedDevice); //do DFU stuff!!! try { dfu.ProgramFirmware(@"F:\nanobooter-nanoclr.dfu"); } catch (Exception e) { Console.WriteLine($"Failed!!! - {e}"); } } else { Console.WriteLine("No USB device found!"); } } }
static UsbDevice GetDevice(LibUsbDotNet.LogLevel level) { if (LibUsbCtx != null) { throw new Exception("Libusb has already been initialized"); } LibUsbCtx = new UsbContext(); LibUsbCtx.SetDebugLevel(level); var usbDeviceCollection = LibUsbCtx.List(); var selectedDevice = usbDeviceCollection.FirstOrDefault(d => d.ProductId == 0x3006 && d.VendorId == 0x057e); if (selectedDevice == null) { return(null); } selectedDevice.Open(); return(new UsbDevice(selectedDevice)); }
private static IUsbDevice GetSysDVRDevice() { if (device != null) { return(device); } LibUsbCtx = new UsbContext(); LibUsbCtx.SetDebugLevel(LogLevel); var usbDeviceCollection = LibUsbCtx.List(); var dev = usbDeviceCollection.FirstOrDefault(d => d.ProductId == 0x3006 && d.VendorId == 0x057e); if (dev == null) { throw new Exception("Device not found"); } dev.Open(); device = dev; return(dev); }
public static void Main(string[] args) { using (var context = new UsbContext()) { context.SetDebugLevel(LogLevel.Info); //Get a list of all connected devices var usbDeviceCollection = context.List(); //Narrow down the device by vendor and pid var selectedDevice = usbDeviceCollection.FirstOrDefault(d => d.ProductId == ProductId && d.VendorId == VendorId); //Open the device selectedDevice.Open(); //Get the first config number of the interface selectedDevice.ClaimInterface(selectedDevice.Configs[0].Interfaces[0].Number); //Open up the endpoints var writeEndpoint = selectedDevice.OpenEndpointWriter(WriteEndpointID.Ep01); var readEnpoint = selectedDevice.OpenEndpointReader(ReadEndpointID.Ep01); //Create a buffer with some data in it var buffer = new byte[64]; buffer[0] = 0x3f; buffer[1] = 0x23; buffer[2] = 0x23; //Write three bytes writeEndpoint.Write(buffer, 3000, out var bytesWritten); var readBuffer = new byte[64]; //Read some data readEnpoint.Read(readBuffer, 3000, out var readBytes); } }