コード例 #1
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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());
        }
コード例 #2
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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());
        }
コード例 #3
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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());
        }
コード例 #4
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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);
        }
コード例 #5
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        public int Read(byte[] buff, int len)
        {
            if (m_DevicePtr == IntPtr.Zero)
            {
                return(0);
            }

            return(HidApi.hid_read(m_DevicePtr, buff, Convert.ToUInt32(len)));
        }
コード例 #6
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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);
        }
コード例 #7
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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();
            }
        }
コード例 #8
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        /// <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);
        }
コード例 #9
0
        /// <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);
        }
コード例 #10
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
        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)));
        }
コード例 #11
0
 static HidDeviceManager()
 {
     HidApi.hid_init();
 }