/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { int _return = -1; byte[] flash = null; OpenFileDialog flashFile = new OpenFileDialog(); flashFile.AddExtension = true; flashFile.DefaultExt = "hex"; flashFile.Filter = "Intel Hex(*.hex)|*.hex|All files(*.*)|*.*"; flashFile.FilterIndex = 0; //---选择文件 if ((flashFile.ShowDialog() == DialogResult.OK) && (!string.IsNullOrEmpty(flashFile.FileName))) { CHexFile loadFlash = new CHexFile(flashFile.FileName); //---校验文件的解析 if (loadFlash.mIsOK) { flash = new byte[loadFlash.mSTOPAddr]; //---填充默认数据是0xFF CGenFuncMem.GenFuncMemset(ref flash, 0xFF); //---数组拷贝 Array.Copy(loadFlash.mDataMap, 0, flash, loadFlash.mSTARTAddr, loadFlash.mDataMap.Length); _return = 0; if (flash != null) { this.cHexBox1.AddData(flash); } } else { MessageBox.Show("加载Flash文件失败", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// 添加数据 /// </summary> /// <param name="dat"></param> /// <param name="isShowDifference"></param> public bool AddData(byte[] dat, bool isShowDifference) { if (dat == null) { return(false); } else { //---获取焦点 this.Focus(); if (this.defaultNowData.Length != dat.Length) { //---重新置位缓存区的大小 Array.Resize <byte>(ref this.defaultNowData, dat.Length); Array.Resize <byte>(ref this.defaultLastData, dat.Length); } //---填充默认数据是0xFF CGenFuncMem.GenFuncMemset(ref this.defaultNowData, 0xFF); //---数组拷贝 Array.Copy(dat, this.defaultNowData, dat.Length); //---判断是否显示不同 if (isShowDifference != true) { Array.Copy(dat, this.defaultLastData, dat.Length); } //---判断地址 if ((dat.Length < 0x100)) { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X2; } else if (dat.Length < 0x10000) { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X4; } else if (dat.Length <= 0x1000000) { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X6; } else { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X8; } this.defaultShowChangeFlag = isShowDifference; //---数据显示的最大行数 this.defaultTotalRow = this.CalcYScaleTotalRow(); //---创建并显示光标 this.OnFindCaret(); //---滑块指向开始 this.defaultVScrollBar.Value = 0; //---置位当前显示的行号位0 this.defaultRowNowNum = 0; //---显示垂直滚动条 this.VScrollBarShow(); //---重新绘制窗体 this.Invalidate(); } return(true); }
/// <summary> /// 添加数据 /// </summary> /// <param name="dat"></param> /// <param name="length"></param> /// <returns></returns> public bool AddData(byte[] dat, long length) { //--数据的合法性 if ((dat == null) || (dat.Length > length)) { return(false); } else { //---获取焦点 this.Focus(); if ((this.defaultNowData == null) || (this.defaultNowData.Length != length)) { //---重新置位缓存区的大小 Array.Resize <byte>(ref this.defaultNowData, (int)length); Array.Resize <byte>(ref this.defaultLastData, (int)length); } //---填充默认数据是0xFF CGenFuncMem.GenFuncMemset(ref this.defaultNowData, 0xFF); //---数组拷贝 Array.Copy(dat, this.defaultNowData, dat.Length); //---判断是否显示不同 if (this.defaultShowChangeFlag != true) { Array.Copy(dat, this.defaultLastData, dat.Length); } //---数据显示的最大行数 this.defaultTotalRow = this.CalcYScaleTotalRow(); //---创建并显示光标 this.OnFindCaret(); //---滑块指向开始 this.defaultVScrollBar.Value = 0; //---置位当前显示的行号位0 this.defaultRowNowNum = 0; //---显示垂直滚动条 this.VScrollBarShow(); //---重新绘制窗体 this.Invalidate(); } return(true); }
/// <summary> /// 添加数据 /// </summary> /// <param name="length"></param> public bool AddData(long length) { //---获取焦点 this.Focus(); //----数据赋初值为0xFF CGenFuncMem.GenFuncMemset(ref this.defaultNowData, length, 0xFF); CGenFuncMem.GenFuncMemset(ref this.defaultLastData, length, 0xFF); this.defaultShowChangeFlag = false; this.defaultNewDataChange = false; //---判断地址 if ((length < 0x100)) { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X2; } else if (length < 0x10000) { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X4; } else if (length <= 0x1000000) { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X6; } else { this.defaultYScaleShowBit4 = YScaleShowBit4.BIT4X8; } //---数据显示的最大行数 this.defaultTotalRow = this.CalcYScaleTotalRow(); //---创建并显示光标 this.OnFindCaret(); //---滑块指向开始 this.defaultVScrollBar.Value = 0; //---置位当前显示的行号位0 this.defaultRowNowNum = 0; //---显示垂直滚动条 this.VScrollBarShow(); //---重新绘制窗体 this.Invalidate(); return(true); }
/// <summary> /// 获取解析后的数据,空白位置用指定的数据填充 /// </summary> /// <param name="maxSize">Hex文件最大容量</param> /// <param name="isHighFirst">true---高位在前,false---地位在前</param> /// <param name="fillVal">填充的数据</param> /// <returns></returns> public byte[] GetHexFileDataMap(long maxSize, bool isHighFirst = false, byte fillVal = 0xFF) { //---创建缓存区 byte[] _return = null; byte[] buffer = null; long myAddr = this.mSTOPAddr; //---校验数据长度 if (maxSize < myAddr) { maxSize = myAddr; } _return = new byte[maxSize]; //---校验缓存区的申请 if (_return == null) { this.defaultLogMessage = "缓存区申请失败!\r\n"; return(null); } //---校验固件文件信息 if ((this.defaultCHexLine == null) || (this.defaultCHexLine.Count == 0)) { this.defaultLogMessage = "固件文件无有效信息!\r\n"; return(null); } //---获取起始地址 myAddr = this.mSTARTAddr; //---用指定的数据填充缓存区 CGenFuncMem.GenFuncMemset(ref _return, _return.Length, fillVal); //---将解析后的数据填充到数据缓存区 for (int i = 0; i < this.defaultCHexLine.Count; i++) { //---数据类型的解析 switch (this.defaultCHexLine[i].Type) { case HexType.DATA_RECORD: //---拷贝数据 Array.Copy(this.defaultCHexLine[i].InfoData, 0, _return, ((this.defaultCHexLine[i].Addr) + myAddr), this.defaultCHexLine[i].Length); break; case HexType.END_OF_FILE_RECORD: //1 break; case HexType.EXTEND_SEGMENT_ADDRESS_RECORD: //2 buffer = new byte[2] { this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; myAddr = BitConverter.ToUInt16(buffer, 0); myAddr <<= 4; break; case HexType.START_SEGMENT_ADDRESS_RECORD: //3 break; case HexType.EXTEND_LINEAR_ADDRESS_RECORD: //4 buffer = new byte[2] { this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; //---将字节地址转换成字地址 myAddr = BitConverter.ToUInt16(buffer, 0); //---将拓展线性地址转换成实际的对应的地址段 myAddr <<= 16; break; case HexType.START_LINEAR_ADDRESS_RECORD: //5 break; default: break; } } //---Hex文件默认是低位在前高位在后,判断是否需要将数据转换成高位在前低位在后的格式 if (isHighFirst == true) { byte[] _returnTemp = new byte[_return.Length]; } return(_return); }
/// <summary> /// 获取解析后的数据 /// </summary> /// <param name="length"></param> /// <returns></returns> public byte[] GetHexFileDataMap() { byte[] buffer = null; long myAddr = 0; //---创建缓存区 byte[] _return = new byte[this.mSTOPAddr]; //---校验缓存区的申请 if ((_return == null) || (_return.Length == 0)) { this.defaultLogMessage = "缓存区申请失败!\r\n"; return(null); } //---校验固件文件信息 if ((this.defaultCHexLine == null) || (this.defaultCHexLine.Count == 0)) { this.defaultLogMessage = "固件文件无有效信息!\r\n"; return(null); } myAddr = this.mSTARTAddr; //---用指定的数据填充缓存区 CGenFuncMem.GenFuncMemset(ref _return, _return.Length, 0xFF); //---将解析后的数据填充到数据缓存区 for (int i = 0; i < this.defaultCHexLine.Count; i++) { //---数据类型的解析 switch (this.defaultCHexLine[i].Type) { //---记录数据 case HexType.DATA_RECORD: //---拷贝数据 Array.Copy(this.defaultCHexLine[i].InfoData, 0, _return, ((this.defaultCHexLine[i].Addr) + myAddr), this.defaultCHexLine[i].Length); break; //---文件结束记录 case HexType.END_OF_FILE_RECORD: //1 break; //---扩展段地址 case HexType.EXTEND_SEGMENT_ADDRESS_RECORD: //2 //--解析段地址 buffer = new byte[2] { this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; myAddr = BitConverter.ToUInt16(buffer, 0); myAddr <<= 4; break; //---开始段地址 case HexType.START_SEGMENT_ADDRESS_RECORD: //3 break; //---扩展线性地址 case HexType.EXTEND_LINEAR_ADDRESS_RECORD: //4 //---解析拓展线性地址 buffer = new byte[2] { this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; //---将字节地址转换成字地址 myAddr = BitConverter.ToUInt16(buffer, 0); //---将拓展线性地址转换成实际的对应的地址段 myAddr <<= 16; break; //---开始线性地址 case HexType.START_LINEAR_ADDRESS_RECORD: //5 break; default: break; } } return(_return); }
/// <summary> /// 获取解析后的数据 /// </summary> /// <param name="length"></param> /// <returns></returns> public virtual byte[] GetDataMap() { //---创建缓存区 byte[] _return = new byte[this.mSTOPAddr]; if (_return == null) { return(null); } if ((this.defaultCHexLine == null) || (this.defaultCHexLine.Count == 0)) { return(null); } //---用指定的数据填充缓存区 CGenFuncMem.GenFuncMemset(ref _return, _return.Length, 0xFF); //---初始化数据的地址 long baseAddr = 0; //---将解析后的数据填充到数据缓存区 int i = 0; for (i = 0; i < this.defaultCHexLine.Count; i++) { byte[] buffer = null; //---数据类型的解析 switch (this.defaultCHexLine[i].Type) { case HexType.DATA_RECORD: //---拷贝数据 Array.Copy(this.defaultCHexLine[i].InfoData, 0, _return, (baseAddr + this.defaultCHexLine[i].Addr), this.defaultCHexLine[i].Length); break; case HexType.END_OF_FILE_RECORD: break; case HexType.EXTEND_SEGMENT_ADDRESS_RECORD: //---获取数据的地址 buffer = new byte[2] { this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; baseAddr = BitConverter.ToUInt16(buffer, 0); baseAddr <<= 4; break; case HexType.START_SEGMENT_ADDRESS_RECORD: //---获取数据的地址 buffer = new byte[4] { this.defaultCHexLine[i].InfoData[3], this.defaultCHexLine[i].InfoData[2], this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; baseAddr = BitConverter.ToUInt32(buffer, 0); baseAddr <<= 4; break; case HexType.EXTEND_LINEAR_ADDRESS_RECORD: //---获取数据的地址 buffer = new byte[2] { this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; baseAddr = BitConverter.ToUInt16(buffer, 0); baseAddr <<= 16; break; case HexType.START_LINEAR_ADDRESS_RECORD: //---获取数据的地址 buffer = new byte[4] { this.defaultCHexLine[i].InfoData[3], this.defaultCHexLine[i].InfoData[2], this.defaultCHexLine[i].InfoData[1], this.defaultCHexLine[i].InfoData[0] }; baseAddr = BitConverter.ToUInt32(buffer, 0); break; default: return(null); } } return(_return); }