Exemplo n.º 1
0
        /// <summary>
        /// 创建包-同步时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private static byte[] BuildBag_SyncTime(DateTime dt)
        {
            byte[] arrRtn = new byte[23];
            int    iIndex = 0;

            //帧起始符 高位
            arrRtn[iIndex++] = 0x55;
            //帧起始符 低位
            arrRtn[iIndex++] = 0xFF;
            //数据长度 高位
            arrRtn[iIndex++] = 0x00;
            //数据长度 低位
            arrRtn[iIndex++] = 0x12;
            //命令字
            arrRtn[iIndex++] = 0x9A;

            //广播地址 5字节
            arrRtn[iIndex++] = 0xFF;
            arrRtn[iIndex++] = 0xFF;
            arrRtn[iIndex++] = 0xFF;
            arrRtn[iIndex++] = 0xFF;
            arrRtn[iIndex++] = 0xFF;
            //主管理机地址 5字节
            arrRtn[iIndex++] = 0x01;
            arrRtn[iIndex++] = 0x00;
            arrRtn[iIndex++] = 0x00;
            arrRtn[iIndex++] = 0x00;
            arrRtn[iIndex++] = 0x00;
            //时间 7字节
            string strYear = dt.Year.ToString();

            //年
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(strYear.Substring(2, 2));
            //月
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Month));
            //日
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Day));
            //周
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(Functions.GetWeekOfYear(dt)));
            //时
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Hour));
            //分
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Minute));
            //秒
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Second));

            //CRC8校验
            arrRtn[iIndex++] = CRC8A.GetCRC8(arrRtn, 0, arrRtn.Length - 1);
            return(arrRtn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建包-写卡
        /// </summary>
        /// <param name="objCard"></param>
        /// <returns></returns>
        private static byte[] BuildBag_WriteCard(Card objCard)
        {
            byte[] arrRtn = new byte[0];
            if (objCard != null)
            {
                int iAreaCode, iBuidCode, iUnitCode, iRoomCode;
                iAreaCode = objCard.RAreaCode;
                iBuidCode = objCard.RBuildCode;
                iUnitCode = objCard.RUnitCode;
                iRoomCode = objCard.RRoomCode;

                if ((iAreaCode >= 0 && iAreaCode <= 99) && (iBuidCode >= 0 && iBuidCode <= 99) && (iUnitCode >= 0 && iUnitCode <= 99) && (iRoomCode >= 0 && iRoomCode <= 9999))
                {
                    byte[] arrRtnTemp = new byte[1450];
                    int    iIndex     = 0;
                    //帧起始符 高位
                    arrRtnTemp[iIndex++] = 0x55;
                    //帧起始符 低位
                    arrRtnTemp[iIndex++] = 0xFF;
                    //数据长度 高位
                    arrRtnTemp[iIndex++] = 0x00;
                    //数据长度 低位
                    arrRtnTemp[iIndex++] = 0x21;
                    //命令字
                    arrRtnTemp[iIndex++] = GetCommandCodeByType(ECommandType.WriteCard_Send);

                    //区编码
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(iAreaCode));
                    //栋编码
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(iBuidCode));
                    //单元编码
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(iUnitCode));

                    string strRoomCode = FormatRoomCode(iRoomCode);
                    //楼层编码
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(strRoomCode.Substring(0, 2)));
                    //房间编码
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(strRoomCode.Substring(2, 2)));

                    //过期时间
                    DateTime dt      = Functions.ConvertToNormalTime(objCard.ExpiryDate);
                    string   strYear = dt.Year.ToString();
                    //年-高位
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(strYear.Substring(0, 2));
                    //年-低位
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(strYear.Substring(2, 2));
                    //月
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Month));
                    //日
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(dt.Day));

                    //卡片类型
                    arrRtnTemp[iIndex++] = Convert.ToByte(objCard.CardType);

                    DateTime CreateDt = System.DateTime.Now;
                    //预留6个字节
                    //iIndex += 6;
                    //发卡时间 当前 年
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(CreateDt.Year.ToString().Substring(2, 2)));
                    //发卡时间 当前 月
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(CreateDt.Month));
                    //发卡时间 当前 日
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(CreateDt.Day));
                    //发卡时间 当前 时
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(CreateDt.Hour));
                    //发卡时间 当前 分
                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(CreateDt.Minute));
                    //发卡时间 当前 写扇区  先转成16进制再转成字符串
                    string chsStr = "";
                    switch (Convert.ToInt32(objCard.listChsInfo[1][0]))
                    {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        chsStr = objCard.listChsInfo[1][0].ToString();
                        break;

                    case 10:
                        chsStr = string.Format("A");
                        break;

                    case 11:
                        chsStr = string.Format("B");
                        break;

                    case 12:
                        chsStr = string.Format("C");
                        break;

                    case 13:
                        chsStr = string.Format("D");
                        break;

                    case 14:
                        chsStr = string.Format("E");
                        break;

                    case 15:
                        chsStr = string.Format("F");
                        break;
                    }

                    arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(chsStr));

                    //序号 16个字节
                    string strSerialNo = objCard.SerialNo.Replace(" ", "").PadLeft(32, '0');
                    for (int i = 1; i <= 16; i++)
                    {
                        arrRtnTemp[iIndex++] = ScaleConverter.OneHexStr2Byte(strSerialNo.Substring((i - 1) * 2, 2));
                    }

                    //CRC8校验
                    arrRtnTemp[iIndex++] = CRC8A.GetCRC8(arrRtnTemp, 0, iIndex - 1);

                    arrRtn = new byte[iIndex];
                    Array.Copy(arrRtnTemp, arrRtn, arrRtn.Length);
                }
            }
            return(arrRtn);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建包-扇区重置
        /// </summary>
        /// <param name="chs">需要重置的扇区</param>
        /// <returns></returns>
        private static byte[] BuildBag_ChsClean(String chs)
        {
            byte[] arrRtn = new byte[7];
            int    iIndex = 0;

            //帧起始符 高位
            arrRtn[iIndex++] = 0x55;
            //帧起始符 低位
            arrRtn[iIndex++] = 0xFF;
            //数据长度 高位
            arrRtn[iIndex++] = 0x00;
            //数据长度 低位
            arrRtn[iIndex++] = 0x02;
            //命令字
            arrRtn[iIndex++] = 0x38;
            //数据 要重置的扇区

            string chsStr = "";

            switch (Convert.ToInt32(chs))
            {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                chsStr = chs;
                break;

            case 10:
                chsStr = string.Format("A");
                break;

            case 11:
                chsStr = string.Format("B");
                break;

            case 12:
                chsStr = string.Format("C");
                break;

            case 13:
                chsStr = string.Format("D");
                break;

            case 14:
                chsStr = string.Format("E");
                break;

            case 15:
                chsStr = string.Format("F");
                break;
            }
            arrRtn[iIndex++] = ScaleConverter.OneHexStr2Byte(BQ2(chsStr));

            //CRC8校验
            arrRtn[iIndex++] = CRC8A.GetCRC8(arrRtn, 0, arrRtn.Length - 1);
            return(arrRtn);
        }