コード例 #1
0
        /// <summary>
        /// 数据扩展段地址
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public static string ToHexLineExternSegmentAddrRecord(long addr)
        {
            string _return = "";

            byte[] line = new byte[2] {
                (byte)((addr >> 8) & 0xFF), (byte)(addr & 0xFF)
            };
            _return = HexLine.ToHexLine(0, HexType.EXTEND_SEGMENT_ADDRESS_RECORD, line);
            return(_return);
        }
コード例 #2
0
 /// <summary>
 /// 数据记录行
 /// </summary>
 /// <param name="addr"></param>
 /// <param name="buffer"></param>
 /// <returns></returns>
 public static string ToHexLineDataRecord(long addr, byte[] buffer)
 {
     return(HexLine.ToHexLine(addr, HexType.DATA_RECORD, buffer));
 }
コード例 #3
0
        /// <summary>
        /// 解析Hex文件数据行
        /// </summary>
        /// <param name="hexLine"></param>
        /// <returns></returns>
        public bool GetHexLine(string hexLine)
        {
            //---将数据记录行的首位空格去除并装换成大写模式
            hexLine = hexLine.Trim().ToUpper();
            //---判断字符信息是否合法
            if ((hexLine == string.Empty) || (hexLine == ""))
            {
                this._errMsg = "数据记录行的信息为空!";
                return(false);
            }
            //---获取数据记录行的头
            if (hexLine[0] != ':')
            {
                this._errMsg = "数据记录行的头格式错误!";
                return(false);
            }

            //---检查数据是否符合十六进制个数
            if (!HexLine.IsHexNumber(hexLine.Substring(1)))
            {
                this._errMsg = "数据记录的信息中含有不合法的信息!";
                return(false);
            }
            //---获取数据的长度
            try
            {
                this._length = byte.Parse(hexLine.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }
            catch
            {
                this._errMsg = "数据记录行的长度错误!";
                return(false);
            }
            this._crcVal = this._length;

            //---获取数据地址
            try
            {
                this._addr = UInt32.Parse(hexLine.Substring(3, 4), System.Globalization.NumberStyles.AllowHexSpecifier);
            }
            catch
            {
                this._errMsg = "数据记录行的地址错误!";
                return(false);
            }
            //---开始极端校验和
            this._crcVal += (byte)(this._addr >> 8);
            this._crcVal += (byte)(this._addr);

            //---获取数据类型
            try
            {
                this._hexType = (HexType)(byte.Parse(hexLine.Substring(7, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
            }
            catch
            {
                this._errMsg = "数据记录行的类型错误!";
                return(false);
            }

            //---判断数据类型是否符合格式
            if (!Enum.IsDefined(typeof(HexType), this._hexType))
            {
                this._errMsg = "数据记录行的数据类型不能被识别!";
                return(false);
            }
            this._crcVal += (byte)this._hexType;

            //---申请存储数据的空间
            this._hexByte = new byte[this._length];
            int i = 0;

            for (i = 0; i < this._length; i++)
            {
                try
                {
                    this._hexByte[i] = byte.Parse(hexLine.Substring(9 + i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    this._crcVal    += this._hexByte[i];
                }
                catch
                {
                    this._errMsg = "数据记录行的数据信息错误";
                    return(false);
                }
            }

            //---获取数据记录行的记录信息
            byte tempCRC = 0;

            try
            {
                tempCRC = byte.Parse(hexLine.Substring(9 + this._length * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }
            catch
            {
                this._errMsg = "数据记录行的校验信息错误";
                return(false);
            }

            //---计算校验和
            this._crcVal = (byte)(0x100 - this._crcVal);
            //---校验和比对
            if (this._crcVal != tempCRC)
            {
                this._errMsg = "数据记录行的校验错误!";
                return(false);
            }
            //---数据类型比较----判断数据记录行的信息是否正确
            switch (this._hexType)
            {
            case HexType.DATA_RECORD:
                if (this._length != this._hexByte.Length)
                {
                    this._errMsg = "数据文件数据记录标识错误!";
                    return(false);
                }
                break;

            case HexType.END_OF_FILE_RECORD:
                if (this._length != 0)
                {
                    this._errMsg = "数据文件结束标识错误!";
                    return(false);
                }
                break;

            case HexType.EXTEND_SEGMENT_ADDRESS_RECORD:
                if (this._length != 2)
                {
                    this._errMsg = "数据文件扩展段地址标识错误!";
                    return(false);
                }
                break;

            case HexType.START_SEGMENT_ADDRESS_RECORD:
                if (this._length != 4)
                {
                    this._errMsg = "数据文件段地址标识错误!";
                    return(false);
                }
                break;

            case HexType.EXTEND_LINEAR_ADDRESS_RECORD:
                if (this._length != 2)
                {
                    this._errMsg = "数据文件扩展线性地址标识错误!";
                    return(false);
                }
                break;

            case HexType.START_LINEAR_ADDRESS_RECORD:
                if (this._length != 4)
                {
                    this._errMsg = "数据文件开始线性地址标识错误!";
                    return(false);
                }
                break;

            default:
                this._errMsg = "不能识别的数据文件!";
                return(false);
            }
            return(true);
        }