private HidSharp.HidStream GetStream() { if (!IsOpen || stream == null) { stream = internalDevice.Open(); } return(stream); }
/// <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; }