示例#1
0
        /// <summary>
        /// 设备信息获取(无权限要求)
        /// </summary>
        /// <returns></returns>
        public static UInt32 GetDeviceInfo()
        {
            //1.枚举锁
            //2.打开锁
            //3.获取所有信息(结构体)
            //4.获取指定信息(Item)
            //5.关闭锁

            UInt32 ret = MowayCS.MW_SUCCESS;
            UInt32 devInfoArraySize = 32;       // 获取设备的最大数量

            MW_DEVICE_INFO_CTX[] devInfoArray = new MW_DEVICE_INFO_CTX[devInfoArraySize];

            UInt32 devCount = 0;
            IntPtr hHandle  = IntPtr.Zero;

            //1.枚举锁
            ret = MowayCS.mw_enum(devInfoArray, devInfoArraySize, ref devCount);
            if (ret != MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_enum failed, {0:X8}", ret);
                return(ret);
            }

            if (devCount == 0)
            {
                Console.WriteLine("Not found device");
                return(ret);
            }

            //2.打开锁
            ret = MowayCS.mw_open(ref devInfoArray[0], MowayCS.MW_OPEN_EXCLUSIVE_MODE, ref hHandle);
            if (ret != MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_open failed, {0:X8}", ret);
                return(ret);
            }

            // 3.获取所有信息(结构体)
            MW_DEVICE_ALL_ITEM_INFO devAllItemInfo = new MW_DEVICE_ALL_ITEM_INFO();
            UInt32 devAllItemInfoSize = (UInt32)Marshal.SizeOf(devAllItemInfo);

            ret = MowayCS.mw_get_device_all_info(hHandle, Convert.ToByte(MowayCS.MW_GET_INFO_ITEM_ALL), ref devAllItemInfo, ref devAllItemInfoSize);
            if (ret == MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_get_device_all_info pid = {0}", devAllItemInfo.PID);
            }
            else
            {
                Console.WriteLine("mw_get_device_all_info failed, {0:X8}", ret);
            }

            //4.获取指定信息(Item)
            byte[] devItemInfoBuffer    = null;
            UInt32 devItemInfoBufferLen = 0;

            // 4.1 获取PID
            devItemInfoBuffer    = new byte[128];
            devItemInfoBufferLen = (UInt32)devItemInfoBuffer.Length;
            ret = MowayCS.mw_get_device_info(hHandle, Convert.ToByte(MowayCS.MW_GET_INFO_ITEM_PID), devItemInfoBuffer, ref devItemInfoBufferLen);
            if (ret == MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_get_device_info pid = {0}", devAllItemInfo.PID);
            }
            else
            {
                Console.WriteLine("mw_get_device_info failed, {0:X8}", ret);
            }

            // 4.2 获取SN
            devItemInfoBufferLen = (UInt32)devItemInfoBuffer.Length;    // 重置缓冲区大小
            ret = MowayCS.mw_get_device_info(hHandle, Convert.ToByte(MowayCS.MW_GET_INFO_ITEM_SN), devItemInfoBuffer, ref devItemInfoBufferLen);
            if (ret == MowayCS.MW_SUCCESS)
            {
                // 本例将SN转换字符串,实际内容为二进制数据。开发者需酌情存储相关数据,建议使用十六进制的字符串保存SN。
                Console.WriteLine("mw_get_device_info SN = {0}", BitConverter.ToString(devItemInfoBuffer, 0, 8));
            }
            else
            {
                Console.WriteLine("mw_get_device_info failed, {0:X8}", ret);
            }

            //5.关闭锁
            MowayCS.mw_close(hHandle);

            return(MowayCS.MW_SUCCESS);
        }