/// <summary> /// Device product /// </summary> /// <returns></returns> public string Product() { m_DeviceInfoBuffer.Clear(); HidApi.hid_get_product_string(m_DevicePtr, m_DeviceInfoBuffer, MARSHALED_STRING_MAX_LEN); return(m_DeviceInfoBuffer.ToString()); }
/// <summary> /// Device manufacturer /// </summary> /// <returns></returns> public string Manufacturer() { m_DeviceInfoBuffer.Clear(); HidApi.hid_get_manufacturer_string(m_DevicePtr, m_DeviceInfoBuffer, MARSHALED_STRING_MAX_LEN); return(m_DeviceInfoBuffer.ToString()); }
/// <summary> /// Device serial number /// </summary> /// <returns></returns> public string SerialNumber() { m_DeviceInfoBuffer.Clear(); HidApi.hid_get_serial_number_string(m_DevicePtr, m_DeviceInfoBuffer, MARSHALED_STRING_MAX_LEN); return(m_DeviceInfoBuffer.ToString()); }
/// <summary> /// Get a string from a HID device, based on its string index /// </summary> /// <param name="index">The index of the string to get</param> /// <returns></returns> public string DevicesString(int index) { m_DeviceInfoBuffer.Clear(); var res = HidApi.hid_get_indexed_string(m_DevicePtr, index, m_DeviceInfoBuffer, MARSHALED_STRING_MAX_LEN); return(res == 0 ? m_DeviceInfo.ToString() : null); }
public int Read(byte[] buff, int len) { if (m_DevicePtr == IntPtr.Zero) { return(0); } return(HidApi.hid_read(m_DevicePtr, buff, Convert.ToUInt32(len))); }
/// <summary> /// Disconnect from HID device /// </summary> /// <returns></returns> public bool Disconnect() { if (m_DevicePtr == IntPtr.Zero) { return(false); } HidApi.hid_close(m_DevicePtr); m_IsConnected = false; return(true); }
/// <summary> /// Get all available strings of HID device /// </summary> /// <returns></returns> public IEnumerable <string> DeviceStrings() { const int maxStringNum = 16; int counter = 0; m_DeviceInfoBuffer.Clear(); while (HidApi.hid_get_indexed_string(m_DevicePtr, counter, m_DeviceInfoBuffer, MARSHALED_STRING_MAX_LEN) == 0 && counter++ < maxStringNum) { yield return(m_DeviceInfoBuffer.ToString()); m_DeviceInfoBuffer.Clear(); } }
/// <summary> /// Connect to HID device /// </summary> /// <returns></returns> public bool Connect() { if (m_DevicePtr == IntPtr.Zero) { return(false); } m_DevicePtr = HidApi.hid_open_path(m_DeviceInfo.path); if (m_DevicePtr != IntPtr.Zero) { m_IsConnected = true; } return(true); }
/// <summary> /// Try to find devices by Vendor Id and Product Id /// </summary> /// <param name="vid">Vendor Id. Set pid to 0 to find devices with any Product Id</param> /// <param name="pid">Product Id. Set vid and pid to 0 to find any devices</param> /// <returns></returns> public List <HidDevice> SearchDevices(int vid, int pid) { var devices = new List <HidDevice>(); var devicesLinkedList = HidApi.hid_enumerate((ushort)vid, (ushort)pid); devicesEnumerations.Add(devicesLinkedList); if (devicesLinkedList == IntPtr.Zero) { return(devices); } hid_device_info deviceInfo = #if NET40 (hid_device_info)Marshal.PtrToStructure(devicesLinkedList, typeof(hid_device_info)); #endif #if NETSTANDARD2_0 Marshal.PtrToStructure <hid_device_info>(devicesLinkedList); #endif devices.Add(new HidDevice(deviceInfo, devicesLinkedList)); while (deviceInfo.next != IntPtr.Zero && devices.Count < MAX_DEVICE_COUNT) { var ptr = deviceInfo.next; deviceInfo = #if NET40 (hid_device_info)Marshal.PtrToStructure(deviceInfo.next, typeof(hid_device_info)); #endif #if NETSTANDARD2_0 Marshal.PtrToStructure <hid_device_info>(deviceInfo.next); #endif devices.Add(new HidDevice(deviceInfo, ptr)); } if (devices.Count >= MAX_DEVICE_COUNT) { throw new Exception($"device max number was reached while enumerating devices - {MAX_DEVICE_COUNT}"); } return(devices); }
public int Write(byte[] bytes) { if (m_DevicePtr == IntPtr.Zero) { return(0); } if (bytes == null || bytes.Length == 0) { return(0); } if (m_WriteBuffer.Length <= bytes.Length) { Array.Resize(ref m_WriteBuffer, bytes.Length + 2); } //TODO fix this for other OSs //hidapi for windows has problem - first byte must be 0 also array length shuold be increased for 1 Array.Copy(bytes, 0, m_WriteBuffer, 1, bytes.Length); return(HidApi.hid_write(m_DevicePtr, m_WriteBuffer, Convert.ToUInt32(bytes.Length + 1))); }
static HidDeviceManager() { HidApi.hid_init(); }