bool ParaseFileLine(string inoutstr, out DataLineMessage formatnew) { formatnew.length = 0; formatnew.type = 0; formatnew.addr = 0; formatnew.ExtAddr = 0; formatnew.checksum = 0; try { //DataLineMessage line=new DataLineMessage(); byte[] data = HexStringToByteArray(inoutstr.Substring(1)); if ((inoutstr.Substring(0, 1) != ":")) { return(false); } if (data.Length != 1 + 2 + 1 + 1 + data[0]) { return(false); } //长度 formatnew.length = data[0]; //数据地址 formatnew.addr = (UInt32)((data[1] << 8) | (data[2] << 0)); //数据类型 formatnew.type = data[3]; if ((formatnew.type <= 0x05) && (formatnew.type >= 0x02)) { //扩展地址 if (formatnew.length == 2) { formatnew.ExtAddr = (UInt32)((data[4] << 8) | (data[5] << 0)); formatnew.ExtAddr <<= 16; } else if (formatnew.length == 4) { formatnew.ExtAddr = (UInt32)((data[4] << 8) | (data[5] << 0)); formatnew.ExtAddr |= (UInt32)((data[6] << 8) | (data[7] << 0)); } } formatnew.checksum = data[data.Length - 1]; return(true); } catch { return(false); } finally { } }
void OpenDropFile(string file1) { FileStream filefd = null; StreamReader streamReader = null; DataLineMessage line = new DataLineMessage(); filefd = new FileStream(file1, FileMode.Open, FileAccess.Read); streamReader = new StreamReader(filefd, Encoding.Default); filefd.Seek(0, SeekOrigin.Begin); string content = streamReader.ReadLine(); if (ParaseFileLine(content, out line)) { UInt32 BaseAddr = line.ExtAddr; Debug.WriteLine("line.ExtAddr=0x{0:x8}", BaseAddr);//基地址 content = streamReader.ReadLine(); if (ParaseFileLine(content, out line)) { BaseAddr |= line.addr;//加上偏移地址 if (BaseAddr == 0x08000000) { textBox1.Text = file1; } else { textBox2.Text = file1; } } else { MessageBox.Show("file is not correct", "error"); } } else { MessageBox.Show("file is not correct", "error"); } if (filefd != null) { filefd.Close(); } if (streamReader != null) { streamReader.Close(); } }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog open_fd = new OpenFileDialog(); open_fd.Multiselect = true; open_fd.Title = @"请选择文件"; open_fd.Filter = @"所有文件(*.*)|*.*|HEX文件(*.hex)|*.hex"; open_fd.FilterIndex = 2; if (open_fd.ShowDialog() == DialogResult.OK) { //Multiselect=true后 open_fd.FileNames是一个字符串数组 FileStream filefd = null; StreamReader streamReader = null; if (open_fd.FileNames.Length <= 2) { DataLineMessage line = new DataLineMessage(); filefd = new FileStream(open_fd.FileNames[0], FileMode.Open, FileAccess.Read); streamReader = new StreamReader(filefd, Encoding.Default); filefd.Seek(0, SeekOrigin.Begin); string content = streamReader.ReadLine(); if (ParaseFileLine(content, out line)) { UInt32 BaseAddr = line.ExtAddr; Debug.WriteLine("line.ExtAddr=0x{0:x8}", BaseAddr);//基地址 content = streamReader.ReadLine(); if (ParaseFileLine(content, out line)) { BaseAddr |= line.addr;//加上偏移地址 if (BaseAddr == 0x08000000) { //IsBootLoader = true; textBox1.Text = open_fd.FileNames[0]; if (open_fd.FileNames.Length == 2) { textBox2.Text = open_fd.FileNames[1]; } } else { textBox2.Text = open_fd.FileNames[0]; if (open_fd.FileNames.Length == 2) { textBox1.Text = open_fd.FileNames[1]; } } //textOutPath.Text = GetNewPathForDupes(System.IO.Path.GetDirectoryName(open_fd.FileNames[0]) + @"\merge.hex"); } else { textBox1.Text = ""; textBox2.Text = ""; MessageBox.Show("file is not correct", "error"); } } else { textBox1.Text = ""; textBox2.Text = ""; MessageBox.Show("file is not correct", "error"); } if (filefd != null) { filefd.Close(); } if (streamReader != null) { streamReader.Close(); } } else { MessageBox.Show("there are too many files", "error"); } } }
private void btn_merge_Click(object sender, EventArgs e) { string str1, str2; str1 = textBox1.Text.Trim(); str2 = textBox2.Text.Trim(); if ((str1 == str2) && (str1 == "")) { MessageBox.Show("please select one file at least", "error"); return; } StreamReader fileReader = null; StreamWriter Newfile = null; DataLineMessage line = new DataLineMessage(); string savepath = GetNewPathForDupes(textOutPath.Text); //if (savepath==null) //{ // savepath = GetNewPathForDupes(Application.StartupPath + @"/merge.hex"); //} try { if (str1 == "") { File.Copy(str2, savepath); if (checkBox2.Checked == true)//确定转换为bin文件 { ReadHexFile(savepath); MessageBox.Show("已保存为\n\r" + savepath); } return; } else if (str2 == "") { if (checkBox2.Checked == true)//确定是否合并文件 { ReadHexFile(savepath); MessageBox.Show("已保存为\n\r" + savepath); } return; } //读取文件一 fileReader = new StreamReader(str1); Newfile = new StreamWriter(savepath); string strline = null; UInt32 Nowaddr = 0; do { strline = fileReader.ReadLine(); if (ParaseFileLine(strline, out line)) { if (line.type == 0x01)//结束标志 { break; } else if (line.type == 0x05) //入口地址 { Newfile.WriteLine(strline); //继续保存该行数据 } else//数据等 { Nowaddr = line.addr; Newfile.WriteLine(strline);//保存数据 } } else { fileReader.Close(); Newfile.Close(); MessageBox.Show("There have some error in file", "merge faild"); return; } } while (strline != null); //读取文件二 fileReader = new StreamReader(str2); do { strline = fileReader.ReadLine(); Newfile.WriteLine(strline); } while (strline != null); fileReader.Close(); Newfile.Close(); MessageBox.Show("merge successful"); if (checkBox2.Checked == true) //确定是否合并文件 { ReadHexFile(savepath); //转换成bin } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (fileReader != null) { fileReader.Close(); } if (Newfile != null) { Newfile.Close(); } } }