Beispiel #1
0
        public void ToStringTest()
        {
            string       tag      = "001";
            string       data     = "  2007032296";
            ControlField target   = new ControlField(tag, data);
            string       expected = "001       2007032296";
            string       actual   = target.ToString();

            Assert.AreEqual(expected, actual);
        }
        public virtual string ReadMsg()
        {
            string msg = "";

            if (RawDataChar == null)
            {
                return("无数据");
            }

            if (RawDataChar.Length >= 14)
            {
                if (RawDataChar[0] == BaseProtocol.BeginChar && RawDataChar[RawDataChar.Length - 1] == BaseProtocol.EndChar)
                {
                    Length = (int)RawDataChar[1];
                    byte[] Body_b = new byte[Length];
                    byte   bState = RawDataChar[2];

                    IsPW = bState % 2 > 0;
                    IsTP = bState % 4 > 2;

                    Array.Copy(RawDataChar, 3, Body_b, 0, Body_b.Length);
                    CC  = RawDataChar[RawDataChar.Length - 2];
                    AFN = RawDataChar[14];
                    byte   CCN = FormatHelper.GetXorByte(Body_b, 0, Body_b.Length);
                    string s   = "CCN = " + CCN + "  Length =  " + Length + " body_b .length is " + Body_b.Length;
                    //update by kqz 2017-10:40 解决文件下发问题
                    if ((AFN == (byte)BaseProtocol.AFN.ToDtuSendFile) || (CCN == CC))
                    {
                        ControlField = RawDataChar[3];
                        //if (ControlField == (byte)BaseProtocol.ControlField.FromDtu)
                        if (Enum.IsDefined(typeof(BaseProtocol.ControlField), (int)ControlField))
                        {
                            StationType = RawDataChar[11];
                            StationCode = Convert.ToInt32(HexStringUtility.ByteArrayToHexString(new byte[] { RawDataChar[12], RawDataChar[13] }), 16);
                            AFN         = RawDataChar[14];
                            if (Enum.IsDefined(typeof(BaseProtocol.AFN), (int)AFN))
                            {
                                byte[] AddressField_b = new byte[7];
                                Array.Copy(RawDataChar, 4, AddressField_b, 0, 7);
                                AddressField = HexStringUtility.ByteArrayToHexString(AddressField_b);

                                //用户数据长度为长度减控制域1字节减地址域7字节减应用层功能码1字节减设备类型1字节减射频地址2字节减密码减时间戳
                                UserDataBytes = new byte[Length - 1 - 7 - 1 - 1 - 2 - (IsTP ? 2 : 0) - (IsPW ? 5 : 0)];
                                Array.Copy(RawDataChar, 15, UserDataBytes, 0, UserDataBytes.Length);
                                UserData = HexStringUtility.ByteArrayToHexString(UserDataBytes);

                                try
                                {
                                    if (IsTP)
                                    {
                                        byte[] TPBytes = new byte[5];
                                        Array.Copy(RawDataChar, RawDataChar.Length - 2 - 5, TPBytes, 0, TPBytes.Length);
                                        TP = HexStringUtility.ByteArrayToHexString(TPBytes);
                                    }
                                    if (IsPW)
                                    {
                                        byte[] PWBytes = new byte[2];
                                        Array.Copy(RawDataChar, RawDataChar.Length - 2 - 2 - (IsTP ? 5 : 0), PWBytes, 0, PWBytes.Length);
                                        PW = HexStringUtility.ByteArrayToHexString(PWBytes);
                                    }
                                }
                                catch
                                {
                                    msg = "密码时间戳格式不正确,IsPW:" + (IsPW ? "yes" : "no") + ",IsTP:" + (IsTP ? "yes" : "no");
                                }
                            }
                            else
                            {
                                msg = "功能码不对," + AFN.ToString("X");
                            }
                        }
                        else
                        {
                            msg = "控制域不对," + ControlField.ToString("X");
                        }
                    }
                    else
                    {
                        msg = s + "   校验码不对," + CC;
                    }
                }
                else
                {
                    msg = "开始结束字符不对," + RawDataChar[0].ToString("X") + " " + RawDataChar[RawDataChar.Length - 1].ToString("X");
                }
            }
            else
            {
                msg = "长度不足14," + RawDataChar.Length;
            }

            return(msg);
        }