Beispiel #1
0
        /// <summary>
        /// 检查帧完整性
        /// </summary>
        /// <param name="package"></param>
        /// <returns></returns>
        public static bool CheckPackage(Package651 package, out string err)
        {
            err = "";
            if (package.PackageLength < 12)
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// 比较两个Package651是否值相等
        /// </summary>
        /// <param name="pack"></param>
        /// <returns></returns>
        public bool Equals(Package651 pack)
        {
            if (this.A5 != pack.A5)
            {
                return(false);
            }
            if (this.A4 != pack.A4)
            {
                return(false);
            }
            if (this.A3 != pack.A3)
            {
                return(false);
            }
            if (this.A2 != pack.A2)
            {
                return(false);
            }
            if (this.A1 != pack.A1)
            {
                return(false);
            }
            if (this.CenterAddr != pack.CenterAddr)
            {
                return(false);
            }
            if (this.PWD[0] != pack.PWD[0])
            {
                return(false);
            }
            if (this.PWD[1] != pack.PWD[1])
            {
                return(false);
            }
            if (this.FUNCODE != pack.FUNCODE)
            {
                return(false);
            }
            if (this.SNum[0] != pack.SNum[0])
            {
                return(false);
            }
            if (this.SNum[1] != pack.SNum[1])
            {
                return(false);
            }
            if (!PasswordEquals(this.data, pack.data))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
 /// <summary>
 /// 通过指定的byte[]数组创建帧
 /// </summary>
 /// <param name="bytes">byte[]数组</param>
 /// <param name="result">传出结果</param>
 /// <returns>是否成功</returns>
 public static bool TryParse(byte[] bytes, out Package651 result, out bool havesubsequent, out string subsequentmsg)
 {
     havesubsequent = false;
     subsequentmsg  = "";
     try
     {
         result = FromBytes(bytes, out havesubsequent, out subsequentmsg);
         return(true);
     }
     catch (ArgumentException argex)
     {
         throw argex;
     }
     catch (Exception)
     {
         result = default(Package651);
         return(false);
     }
 }
Beispiel #4
0
        /// <summary>
        ///  获得校验码
        /// </summary>
        /// <param name="package">帧</param>
        /// <returns></returns>
        public static byte[] CreateCS(Package651 package)
        {
            byte[] code;

            int sum = PackageDefine.BeginByte651[0] + PackageDefine.BeginByte651[1];

            sum = sum + package.CenterAddr;
            sum = sum + package.A1 + package.A2 + package.A3 + package.A4 + package.A5;
            sum = sum + package.PWD[0] + package.PWD[1];
            sum = sum + package.FUNCODE;
            sum = sum + (package.IsUpload ? 0x80 : 0x00) + package.L0 + package.L1;
            sum = sum + package.CStart;
            sum = sum + package.SNum[0] + package.SNum[1];
            sum = sum + package.dt[0] + package.dt[1] + package.dt[2] + package.dt[3] + package.dt[4];
            sum = sum + package.AddrFlag[0] + package.AddrFlag[1];
            sum = sum + package.A1 + package.A2 + package.A3 + package.A4 + package.A5;
            if (package.Classific != null)
            {
                sum = sum + package.Classific;
            }
            if (package.ObservationFlag != null)
            {
                sum = sum + package.ObservationFlag[0] + package.ObservationFlag[1];
            }
            if (package.ObservationTime != null)
            {
                sum = sum + package.ObservationTime[0] + package.ObservationTime[1] + package.ObservationTime[2] + package.ObservationTime[3] + package.ObservationTime[4];
            }

            if (package.data != null)
            {
                foreach (var item in package.data)
                {
                    sum = sum + item;
                }
            }

            code = BitConverter.GetBytes((short)sum);
            return(code);
        }
Beispiel #5
0
        /// <summary>
        /// 通过指定的Byte[]数组创建帧
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Package651 FromBytes(byte[] bytes, out bool ismultipack, out string subsequentmsg)
        {
            ismultipack   = false;
            subsequentmsg = "";
            if (bytes == null || bytes.Length == 0)
            {
                throw new ArgumentException("数据为空");
            }
            Package651 package = new Package651();

            if (bytes.Length < PackageDefine.MinLenth651)
            {
                throw new ArgumentException("数据帧长度低于最短帧");
            }
            if (bytes[0] != PackageDefine.BeginByte651[0] && bytes[1] != PackageDefine.BeginByte651[1])
            {
                throw new ArgumentException("未能在开始位找到帧头");
            }

            package.CenterAddr = bytes[2];
            package.A5         = bytes[3];
            package.A4         = bytes[4];
            package.A3         = bytes[5];
            package.A2         = bytes[6];
            package.A1         = bytes[7];

            package.PWD      = new byte[2];
            package.PWD[1]   = bytes[8];
            package.PWD[0]   = bytes[9];
            package.FUNCODE  = bytes[10];
            package.IsUpload = (Convert.ToInt16(bytes[11] >> 4) == 8) ? false : true;
            package.L1       = (byte)(bytes[11] & (byte)0x0f);
            package.L0       = bytes[12];

            package.CStart = bytes[13];

            package.SNum = new byte[2];

            package.dt = new byte[6];

            if (package.DataLength > 8)
            {
                if (package.CStart == PackageDefine.CStart)
                {
                    package.SNum[1] = bytes[14];
                    package.SNum[0] = bytes[15];
                    if (bytes.Length - PackageDefine.MinLenth651 != (package.DataLength - 8))
                    {
                        throw new ArgumentException("数据长度和数据不匹配");
                    }
                    package.dt[0] = bytes[16];
                    package.dt[1] = bytes[17];
                    package.dt[2] = bytes[18];
                    package.dt[3] = bytes[19];
                    package.dt[4] = bytes[20];
                    package.dt[5] = bytes[21];
                    package.data  = new byte[package.DataLength - 8];
                    for (int i = 0; i < (package.DataLength - 8); i++)
                    {
                        package.data[i] = bytes[22 + i];
                    }
                }
                else if (package.CStart == PackageDefine.CStart_Pack)  //多包发送
                {
                    if (bytes.Length - PackageDefine.MinLenth_Pack != package.DataLength)
                    {
                        throw new ArgumentException("数据长度和数据不匹配");
                    }
                    package.dt[0] = bytes[19];
                    package.dt[1] = bytes[20];
                    package.dt[2] = bytes[21];
                    package.dt[3] = bytes[22];
                    package.dt[4] = bytes[23];
                    package.dt[5] = bytes[24];
                    byte b          = (byte)((bytes[14] << 4) | (bytes[15] >> 4));
                    int  pack_l1    = BitConverter.ToInt16(new byte[] { b, (byte)(bytes[14] >> 4) }, 0);
                    int  pack_index = BitConverter.ToInt16(new byte[] { bytes[16], (byte)(bytes[15] & 0x0F) }, 0);
                    if (pack_l1 != pack_index || pack_l1 > 0)
                    {
                        package.SumPackCount = pack_l1;
                        package.CurPackCount = pack_index;
                        ismultipack          = true;
                    }
                    subsequentmsg = "总包数:" + pack_l1 + "、当前第" + pack_index + "包";
                    if (pack_index != 1)   //多包,除了第一包,正文都是数据,比正常的单包或第一包少
                    {
                        package.data = new byte[package.DataLength - 3];
                        for (int i = 0; i < package.DataLength - 3; i++)
                        {
                            package.data[i] = bytes[17 + i];
                        }
                    }
                    else
                    {
                        package.data = new byte[package.DataLength - 11];
                        for (int i = 0; i < (package.DataLength - 11); i++)
                        {
                            package.data[i] = bytes[25 + i];  //17->24
                        }
                    }
                }
            }
            //else if (package.DataLength == 8)
            //{
            //    package.data = new byte[8];
            //    for (int i = 0; i < 8 && i < bytes.Length; i++)
            //    {
            //        package.data[i] = bytes[i + 14];
            //    }
            //}

            package.CS    = new byte[2];//校验码
            package.CS[0] = bytes[bytes.Length - 2];
            package.CS[1] = bytes[bytes.Length - 1];

            if ((bytes[bytes.Length - 3] != PackageDefine.EndByte651) && (bytes[bytes.Length - 3] != PackageDefine.EndByte_Continue) &&
                (bytes[bytes.Length - 3] != PackageDefine.ENQ) && (bytes[bytes.Length - 3] != PackageDefine.EOT) &&
                (bytes[bytes.Length - 3] != PackageDefine.ESC))
            {
                throw new ArgumentException("数据帧不完整或已损坏");
            }

            byte[] crc_result = crc16(bytes, bytes.Length - 2);

            if (crc_result == package.CS)
            {
                throw new ArgumentException("数据帧校验失败");
            }
            package.OriginalData = bytes;

            return(package);
        }
Beispiel #6
0
 /// <summary>
 /// 生成校验码
 /// </summary>
 /// <returns></returns>
 public byte[] CreateCS()
 {
     return(Package651.CreateCS(this));
 }
 public SendPackageEntity(Package651 package)
 {
     this._sendPackage651 = package;
 }