private void Init()
        {
            byte[] codyMasterSeed = Encoding.Unicode.GetBytes("CodyMasterRockeySeed");

            ushort pCount = 0;
            var    pt     = RockeyArmHelper.EnumRockeyArm(out pCount);

            if (pCount > 1)
            {
                throw new Exception("加密锁不止一个!");
            }
            else if (pCount == 0)
            {
                throw new Exception("没有找到加密锁!");
            }
            else
            {
                DONGLE_INFO pDongleInfo = (DONGLE_INFO)Marshal.PtrToStructure((IntPtr)(UInt32)pt, typeof(DONGLE_INFO));
                if (pDongleInfo.m_PID != DefaultPid)
                {
                    throw new Exception("加密锁已被初始化!");
                }
            }

            uint hDongle = 0;

            RockeyArmHelper.OpenRockey(ref hDongle, 0);
            RockeyArmHelper.VerifyPIN(hDongle, DefaultAdminPin);
            RockeyArmHelper.GenUniqueKey(hDongle, codyMasterSeed);

            RockeyArmHelper.CloseRockey(hDongle);
        }
        public static string GetHIdStr(DONGLE_INFO pDongleInfo)
        {
            string hIdStr = string.Empty;

            for (int i = 0; i < 8; i++)
            {
                hIdStr += pDongleInfo.m_HID[i].ToString("X");
            }
            return(hIdStr);
        }
        /// <summary>
        /// 读取指定Cody系统类型的加密锁
        /// </summary>
        internal ReadCustomerInfoResult ReadCustomerInfo(int codySystemType, out string error)
        {
            try
            {
                ushort pCount = 0;
                var    pt     = RockeyArmHelper.EnumRockeyArm(out pCount);

                for (int i = 0; i < pCount; i++)
                {
                    DONGLE_INFO pDongleInfo = (DONGLE_INFO)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(DONGLE_INFO))), typeof(DONGLE_INFO));

                    if (pDongleInfo.m_PID == CodyMasterPid)//只读取CodyMaster初始化过的加密锁
                    {
                        uint hDongle = 0;
                        RockeyArmHelper.OpenRockey(ref hDongle, i);
                        byte[] datas      = RockeyArmHelper.ReadData(hDongle, 0, 4);
                        int    systemType = BitConverter.ToInt32(datas, 0);

                        if (systemType == codySystemType)
                        {
                            byte[] objDatas = RockeyArmHelper.ReadData(hDongle, 4);

                            error = string.Empty;
                            RockeyArmHelper.CloseRockey(hDongle);
                            return(new ReadCustomerInfoResult()
                            {
                                PDongleInfo = pDongleInfo,
                                ObjDatas = objDatas
                            });
                        }
                        else
                        {
                            RockeyArmHelper.CloseRockey(hDongle);
                        }
                    }
                }

                error = "没有找到针对此系统的加密锁!";
                return(null);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(null);
            }
        }
        public static string ReadCustomerInfoStr(out string error)
        {
            try
            {
                ushort pCount = 0;
                var    pt     = RockeyArmHelper.EnumRockeyArm(out pCount);

                string strInfo = string.Format("共找到{0}个加密锁.", pCount);
                for (int i = 0; i < pCount; i++)
                {
                    DONGLE_INFO pDongleInfo = (DONGLE_INFO)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(DONGLE_INFO))), typeof(DONGLE_INFO));

                    if (pDongleInfo.m_PID == CodyMasterPid)//只读取CodyMaster初始化过的加密锁
                    {
                        uint hDongle = 0;
                        RockeyArmHelper.OpenRockey(ref hDongle, i);
                        byte[] datas      = RockeyArmHelper.ReadData(hDongle, 0, 4);
                        int    systemType = BitConverter.ToInt32(datas, 0);

                        byte[] objDatas = RockeyArmHelper.ReadData(hDongle, 4);

                        if (systemType == (int)CodySystemTypeEnum.PQ)
                        {
                            var rockeyArm = SmartSerializeHelper.DeserializeObject <CodyPQRockeyArm>(objDatas, CodyPQRockeyArm.LoadObj);
                            strInfo += string.Format("\r\n----------第{0}个----------\r\n{1}", i + 1, rockeyArm.GetInfo());
                        }
                        else if (systemType == (int)CodySystemTypeEnum.Cert)
                        {
                            var rockeyArm = SmartSerializeHelper.DeserializeObject <CodyCertRockeyArm>(objDatas, CodyCertRockeyArm.LoadObj);
                            strInfo += string.Format("\r\n----------第{0}个----------\r\n{1}", i + 1, rockeyArm.GetInfo());
                        }

                        RockeyArmHelper.CloseRockey(hDongle);
                    }
                }

                error = string.Empty;
                return(strInfo);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(null);
            }
        }
        public string WriteCustomerInfo(int codySystemType, byte[] infoBytes, out string error)
        {
            //byte[] datas = SmartSerializeHelper.SerializeObject(obj, CodyPQRockeyArm.LoadBytes, false);
            List <byte> byteList = new List <byte>();

            byteList.AddRange(BitConverter.GetBytes(codySystemType));
            byteList.AddRange(BitConverter.GetBytes(infoBytes.Length));
            byteList.AddRange(infoBytes);

            try
            {
                DONGLE_INFO pDongleInfo = this.WriteData(byteList.ToArray());
                string      hId         = RockeyArmHelper.GetHIdStr(pDongleInfo);//回写设备Id

                error = string.Empty;
                return(hId);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(string.Empty);
            }
        }