public void ConnectToMicrocontroller() { HidDeviceLoader device_loader = new HidDeviceLoader(); HidDevice device = device_loader.GetDeviceOrDefault(VendorID, ProductID); if (device != null) { bool success = device.TryOpen(out MicrocontrollerStream); if (!success) { MMazeMessaging.GetInstance().AddMessage("Unable to connect to microcontroller!"); } else { //Disable the read and write timeouts //MicrocontrollerStream.ReadTimeout = System.Threading.Timeout.Infinite; //MicrocontrollerStream.WriteTimeout = System.Threading.Timeout.Infinite; MicrocontrollerStream.ReadTimeout = 30; MicrocontrollerStream.WriteTimeout = 30; } } else { MMazeMessaging.GetInstance().AddMessage("Unable to find microcontroller device!"); } }
public void Open() { HidDeviceLoader loader = new HidDeviceLoader(); _device = loader.GetDeviceOrDefault(Constants.VendorId, Constants.ProductId); if (_device == null) { throw new DeviceNotFoundException(); } _device.Open(); }
public static string GetDeviceName(int vid, int pid, int?ser = null) { var str = "Unknown Device"; try { var result = Loader.GetDeviceOrDefault(vid, pid, ser); str = result.Manufacturer; if (str.Length > 0) { str += " "; } str += result.ProductName; } catch { // ignored } ; return(str); }
public USBConnector() { m_monitoringTimer = new Timer(state => { var previousState = m_isDeviceConnected; var device = s_loader.GetDeviceOrDefault(VendorId, ProductId); if (previousState.HasValue) { if (device == null && previousState == false) { return; } if (device != null && previousState == true) { return; } } m_isDeviceConnected = device != null; OnDeviceConnected(m_isDeviceConnected.Value); }); }
private static HidDevice FindUSBDevice() { HidDeviceLoader loader = new HidDeviceLoader(); HidDevice device = loader.GetDeviceOrDefault(vendorId, productId); if (device != null) { Logger.Log("StreamDeck", "Found StreamDeck USB device!"); return(device); } Logger.Log("StreamDeck", "Could not find StreamDeck USB device!", LoggerVerbosity.Error); return(null); }
private HidStream Connect() { if (m_device == null) { m_device = s_loader.GetDeviceOrDefault(VendorId, ProductId); } if (m_device == null) { return(null); } m_receiveBufferLength = m_device.MaxOutputReportLength; m_sentBufferLength = m_device.MaxInputReportLength - 1; return(m_device.Open()); }
private async void IInitDevice(DeviceID id, DeviceEventType e, HidDevice hid = null) { ResponseDevice device = DeviceFactory.Create(id); if (device != null) { m_log.Trace("ResponseDevice detected..."); if (hid == null) { HidDeviceLoader loader = new HidDeviceLoader(); for (int i = 0; i < 10; i++) { hid = loader.GetDeviceOrDefault(vendorID: id.Vendor, productID: id.Product); if (hid == null) { m_log.Trace("... HID device matching USB insert not found, retrying in 50ms"); Thread.Sleep(50); // :( } else { break; } } if (hid == null) { m_log.Warn("Could not find HID device [V:{0:X}] [P:{1:X}] [S:{2}] after USB insert", id.Vendor, id.Product, id.Serial); return; } } m_log.Info("Discovered `{0}` [V:{1:X}] [P:{2:X}] [S:{3}]", hid.ProductName, id.Vendor, id.Product, id.Serial); if (await device.Initialize(hid)) { m_devices.Add(id, device); DeviceDiscovered?.Invoke(this, new DeviceEventArgs(device, e)); } else { m_log.Warn("Failed to init `{0}` [V:{1:X}] [P:{2:X}] [S:{3}]", hid.ProductName, id.Vendor, id.Product, id.Serial); } } }
/** * Get a handle for USB device file * @param filename the name of the file OR vendor and device ids formatted as "vid&pid" * @param report_size [optional] report size in bytes * @return open read/write Stream */ public override Stream GetUSBHandle(string filename, int report_size) { HidDeviceLoader loader = new HidDeviceLoader(); int vid = 0; int pid = 0; if (filename.IndexOf("&") > 0) { String[] parts = filename.Split(new Char[] { '&' }); vid = Convert.ToInt32(parts[0]); pid = Convert.ToInt32(parts[1]); } else { System.Console.WriteLine("Invalid device specification: " + filename); return(null); } HidDevice dev = loader.GetDeviceOrDefault(vid, pid, null, null); if (dev == null) { System.Console.WriteLine("Could not find requested device: " + filename); var devices = loader.GetDevices().ToArray(); foreach (HidDevice d in devices) { System.Console.WriteLine(d); } return(null); } HidStream stream; if (!dev.TryOpen(out stream)) { System.Console.WriteLine("Found requested device but cannot connect: " + filename); return(null); } return(stream); }
public override Action Start() { bk = new blinking(); loader = new HidDeviceLoader(); HidDevice = loader.GetDeviceOrDefault(VID_WARTHOG_THROTTLE, PID_WARTHOG_THROTTLE); if (HidDevice == null) { throw new Exception("The Warthog Trottle seems to be not connected"); } buffer = new byte[HidDevice.MaxInputReportLength]; buffer[0] = 1; buffer[1] = 6; buffer[2] = 0; buffer[3] = 5; if (!HidDevice.TryOpen(out stream)) { throw new Exception("Failed to open the device Warthog"); } Brightness = 5; SetAllLeds(false); // OnStarted(this, new EventArgs()); return(null); }
static void Main(string[] args) { HidDeviceLoader loader = new HidDeviceLoader(); Thread.Sleep(2000); // Give a bit of time so our timing below is more valid as a benchmark. var stopwatch = new Stopwatch(); stopwatch.Start(); var deviceList = loader.GetDevices().ToArray(); stopwatch.Stop(); long deviceListTotalTime = stopwatch.ElapsedMilliseconds; Console.WriteLine("Complete device list (took {0} ms to get {1} devices):", deviceListTotalTime, deviceList.Length); foreach (HidDevice dev in deviceList) { Console.WriteLine(dev); } Console.WriteLine(); Console.WriteLine("Opening HID class device..."); #if SAMPLE_OPEN_AND_READ var device = loader.GetDevices(0x1b1c, 0x0c04).First(); if (device == null) { Console.WriteLine("Failed to open device."); Environment.Exit(1); } Console.Write(@" Max Lengths: Input: {0} Output: {1} Feature: {2} The operating system name for this device is: {3} " , device.MaxInputReportLength , device.MaxOutputReportLength , device.MaxFeatureReportLength , device.DevicePath ); HidStream stream; if (!device.TryOpen(out stream)) { Console.WriteLine("Failed to open device."); Environment.Exit(2); } using (stream) { int n = 0; while (true) { var bytes = new byte[device.MaxInputReportLength]; int count; try { count = stream.Read(bytes, 0, bytes.Length); } catch (TimeoutException) { Console.WriteLine("Read timed out."); continue; } if (count > 0) { Console.Write("* {0} : ", count); for (int i = 0; i < count && i < 62; i++) { Console.Write("{0:X} ", bytes[i]); } Console.WriteLine(); if (++n == 100) { break; } } } } #elif SAMPLE_DYMO_SCALE HidDevice scale = loader.GetDeviceOrDefault(24726, 344); if (scale == null) { Console.WriteLine("Failed to find scale device."); Environment.Exit(1); } HidStream stream; if (!scale.TryOpen(out stream)) { Console.WriteLine("Failed to open scale device."); Environment.Exit(2); } using (stream) { int n = 0; DymoScale scaleReader = new DeviceHelpers.DymoScale(stream); while (true) { int value, exponent; DymoScaleUnit unit; string unitName; DymoScaleStatus status; string statusName; bool buffered; scaleReader.ReadSample(out value, out exponent, out unit, out status, out buffered); unitName = DymoScale.GetNameFromUnit(unit); statusName = DymoScale.GetNameFromStatus(status); Console.WriteLine("{4} {0}: {1}x10^{2} {3} ", statusName, value, exponent, unitName, buffered ? "b" : " "); if (!buffered) { if (++n == 100) { break; } } } } #else #error "No sample selected." #endif Console.WriteLine("Press a key to exit..."); Console.ReadKey(); }
private void UpdateUI() { // enter bootloader button is enabled if vid/pid of MUNIA // is observed, if so all other functions are disabled var muniaInterfaces = _loader.GetDevices(VID, PID_MUNIA); if (muniaInterfaces.Count() > 1) { // munia detected, so it's not in BL, but we can send a packet to request this tsbLoadHex.Enabled = tsbProgram.Enabled = tsbReset.Enabled = false; _blDevice = null; _blInterface = null; imgBlStatus.Image = Properties.Resources.warn; lblStatus.Text = "Status: user"; imgBlStatus.ToolTipText = "MUNIA is currently in user mode.\r\nReboot it in bootloader mode to continue."; tsbEnterBootloader.Enabled = true; lblHEX.Visible = imgHexStatus.Visible = pbFlash.Visible = lblProgress.Visible = false; tsbLoadHex.Enabled = tsbProgram.Enabled = tsbReset.Enabled = false; return; } else { tsbEnterBootloader.Enabled = false; } // see if there is a selected device, and whether is still valid HidStream s = null; bool blDeviceOk = false; if (_blDevice != null && _blDevice.TryOpen(out s)) { s.Dispose(); blDeviceOk = true; } else { _blDevice = null; } // if no device selected, see if we can find one if (_blDevice == null) { _blDevice = _loader.GetDeviceOrDefault(VID, PID_BL); blDeviceOk = _blDevice != null && _blDevice.TryOpen(out s); if (blDeviceOk) { s.Dispose(); _blInterface = new HidBootloader(_blDevice); } } if (blDeviceOk) { imgBlStatus.Image = Resources.ok; lblStatus.Text = "Status: in BL"; lblStatus.ToolTipText = "MUNIA is currently in bootloader mode,\r\n and is ready to be flashed."; } else { imgBlStatus.Image = Resources.notok; lblStatus.Text = "Status: not found"; lblStatus.ToolTipText = "MUNIA is currently not plugged in or malfunctioning."; lblHEX.Visible = imgHexStatus.Visible = pbFlash.Visible = lblProgress.Visible = false; tsbLoadHex.Enabled = tsbProgram.Enabled = tsbReset.Enabled = false; return; } lblHEX.Visible = imgHexStatus.Visible = pbFlash.Visible = lblProgress.Visible = true; tsbLoadHex.Enabled = true; tsbReset.Enabled = true; if (_hexFile?.Size > 0x100) { tsbProgram.Enabled = true; imgHexStatus.Image = Resources.ok; imgHexStatus.ToolTipText = "Firmware hex file is successfully loaded."; } else { tsbProgram.Enabled = false; imgHexStatus.Image = Resources.notok; imgHexStatus.ToolTipText = "Load a firmware hex file to flash first."; } }