Пример #1
0
        /// <summary>
        /// 检测响应包类型
        /// </summary>
        /// <param name="bArrBag"></param>
        /// <param name="strErrMsg"></param>
        /// <returns></returns>
        public static ECommandType GetResponseType(byte[] bArrBag, out string strErrMsg)
        {
            ECommandType eResponseType = ECommandType.UnKnown;

            strErrMsg = "";
            //数据校验
            if (bArrBag != null && bArrBag.Length >= 6)
            {
                //1.数据CRC8校验
                byte bCRCResult = CRC8A.GetCRC8(bArrBag, 0, bArrBag.Length - 1);
                byte bCRCSource = bArrBag[bArrBag.Length - 1];

                if (bCRCSource.Equals(bCRCResult))
                {
                    //2.识别帧起始符
                    if (bArrBag[0].Equals(0x55) && bArrBag[1].Equals(0xFF))
                    {
                        //3.识别数据长度
                        byte[] bArrDataLength = new byte[4];
                        bArrDataLength[0] = bArrBag[3];
                        bArrDataLength[1] = bArrBag[2];
                        uint iDataLength = Functions.ConverToUInt(bArrDataLength);//低字节在前,高字节在后
                        if (bArrBag.Length == (iDataLength + 2 + 2 + 1))
                        {
                            //4.识别命令包类型
                            eResponseType = GetCommandTypeByCode(bArrBag[4]);
                            if (eResponseType.Equals(ECommandType.UnKnown))
                            {
                                strErrMsg = "数据包指令位不正确!";
                            }
                            else if (eResponseType.Equals(ECommandType.SwipingCard))
                            {
                                if (bArrBag.Length < 23)
                                {
                                    eResponseType = ECommandType.UnKnown;
                                }
                            }
                            else if (eResponseType.Equals(ECommandType.PingResponse))
                            {
                                if (bArrBag.Length < 18)
                                {
                                    eResponseType = ECommandType.UnKnown;
                                }
                            }
                        }
                        else
                        {
                            strErrMsg = "数据长度位与数据包实际长度不一致!";
                        }
                    }
                    else if (bArrBag[0].Equals(0x55) && bArrBag[1].Equals(0xAA))
                    {
                        //刷卡头链路层ACK
                        eResponseType = ECommandType.UnKnown;
                        if (bArrBag.Length >= 16)
                        {
                            if (bArrBag[4].Equals(0x21))
                            {
                                eResponseType = ECommandType.CardHeadDeviceACK;
                            }
                            if (bArrBag[4].Equals(0x92))
                            {
                                eResponseType = ECommandType.MainDeviceACK4PingResponse;
                            }
                        }
                    }
                    else
                    {
                        strErrMsg = "帧起始符不正确!";
                    }
                }
                else
                {
                    strErrMsg = "CRC校验不合法!";
                }
            }
            else
            {
                strErrMsg = "数据长度不正确!";
            }
            return(eResponseType);
        }