private HidSharp.HidStream GetStream() { if (!IsOpen || stream == null) { stream = internalDevice.Open(); } return(stream); }
public void OpenDevice(DeviceMode readMode, DeviceMode writeMode, ShareMode shareMode) { if (IsOpen) { return; } //HidSharp.OpenConfiguration config = new HidSharp.OpenConfiguration(); //config.SetOption(HidSharp.OpenOption.Priority, HidSharp.OpenOption.Priority.) stream = internalDevice.Open(); //_deviceReadMode = readMode; //_deviceWriteMode = writeMode; //_deviceShareMode = shareMode; IsOpen = true; }
/// <summary> /// Create a 64-byte messenger object bound to a USB HID device. /// </summary> /// <param name="vid">Vendor ID</param> /// <param name="pid">Product ID</param> /// <param name="serialnumber">Serial number</param> /// <param name="timeoutms">Time in milliseconds to wait for /// read and write operations to complete. Zero means wait /// forever.</param> public Messenger(int vid = IO.Devices.USB.Munts.HID.Vendor, int pid = IO.Devices.USB.Munts.HID.Product, string serialnumber = null, int timeoutms = 1000) { // Validate parameters if (timeoutms < 0) { throw new Exception("Invalid timeout"); } if ((vid < 0) || (vid > 65535)) { throw new Exception("Invalid vendor ID"); } if ((pid < 0) || (pid > 65535)) { throw new Exception("Invalid product ID"); } if (System.IO.File.Exists("/usr/lib/libsimpleio.so") || System.IO.File.Exists("/usr/local/lib/libsimpleio.so")) { // Use libsimpleio libHIDRaw this.transport = Transport.libsimpleio; // Open the raw HID device int error; libHIDRaw.HIDRAW_open3(vid, pid, serialnumber, out this.fd, out error); if (error != 0) { throw new Exception("Cannot open USB device"); } } else { // Use HidSharp library this.transport = Transport.HidSharp; try { // Open the USB HID device HidSharp.DeviceList list = HidSharp.DeviceList.Local; if (!list.TryGetHidDevice(out hid_device, vid, pid, null, serialnumber)) { throw new Exception("DeviceList.TryGetHidDevice() failed"); } hid_stream = hid_device.Open(); hid_stream.ReadTimeout = timeoutms; hid_stream.WriteTimeout = timeoutms; } catch (System.Exception e) { throw new Exception("Cannot open USB HID device, " + e.Message); } } this.timeout = timeoutms; }
public void StartReading() { lock (readingLock) { if (DeviceReport == null) { reading = false; } if (reading) { return; } reading = true; sendingQueue = new QueueWorker <Wrapper <HidReport> >(5, (report) => { if (report != null) { DeviceReportEvent threadSafeEvent = DeviceReport; threadSafeEvent?.Invoke(report.Value); } }, $"ReportThread {GetUniqueKey(DevicePath)}"); readingThread = new Thread(() => { while (reading) { if (DeviceReport == null) { break; } try { HidSharp.HidStream _stream = GetStream(); lock (_stream) { byte[] data = _stream.Read(); //DeviceReportEvent threadSafeEvent = DeviceReport; ////new Thread(() => ////{ //// threadSafeEvent?.Invoke(new HidReport() { ReportId = data[0], ReportType = HidReportType.Input, ReportBytes = data.Skip(1).ToArray() }); ////}).Start(); // //ThreadPool.QueueUserWorkItem((stateInfo) => //{ // threadSafeEvent?.Invoke(new HidReport() { ReportId = data[0], ReportType = HidReportType.Input, ReportBytes = data.Skip(1).ToArray() }); //}); sendingQueue.EnqueueTask(new HidReport() { ReportId = data[0], ReportType = HidReportType.Input, ReportBytes = data.Skip(1).ToArray() }); } if (PollingRate > 0) { int SleepTime = 0; while (SleepTime < (PollingRate / 1000)) { Thread.Sleep(1000); SleepTime++; } Thread.Sleep(PollingRate % 1000); } } catch (System.TimeoutException) { } catch (Exception) // for example System.IO.IOException: 'Operation failed after some time.' { reading = false; } } reading = false; }); readingThread.Start(); } }