public int ReadS19orGlo(String pathname) { // FileStream f = new FileStream(pathname, FileMode.Open); String readData = null; // BinaryReader readbuf; // StreamReader readfile; FileStream fs = new FileStream(pathname, FileMode.Open); StreamReader reader = new StreamReader(fs, Encoding.UTF8); InitUserFile(); //初始化参数 gFileState = -1; //文件解析完成 if (File.Exists(pathname)) { // try { while ((readData = reader.ReadLine()) != null) { if (readData.StartsWith("S1")) //数据域 { Explain_S1(readData); } else if (readData.StartsWith("S2")) { Explain_S2(readData); } else if (readData.StartsWith("S0"))//起始域 { System.Console.Write("Start to explain the file......" + "\n");; } else if (readData.StartsWith("S9"))//结束域 { Flash_SectionNum = CurSecNum + 1; for (int i = 0; i < Flash_SectionNum; i++) { flash_Data[i].CRC = UserExplainFile.CalcDataCrc(flash_Data[i].data, flash_Data[i].SectionDataNum); } } else { reader.Close(); fs.Close(); return(-1); } } reader.Close(); fs.Close(); } } else { System.Console.Write("文件不存在或不允许读取" + "\n"); return(-1); } gFileState = 0; //文件解析完成 return(0); }
private void button_startdownload_Click(object sender, EventArgs e) { //timer_download.Enabled = true downflashdata = UserExplainFile.GetSectionData(gLoadingSection); if (downflashdata == null) { MessageBox.Show("文件未打开"); return; } updateStep = 0; gLoadingSection = 0; }
public void OpenCanLoaderFile() { // UInt32 crc32 = 0xffffffff; totalsize = 0; String Pathname = null; String[] filter = { "*.s19;*.glo;*.hex" }; //if (openhexFileDialog.ShowDialog() == null) //{ // return; //} openFileDialog_HEXFILE.ShowDialog(); if (openFileDialog_HEXFILE.FileName.EndsWith(".s19") || openFileDialog_HEXFILE.FileName.EndsWith(".glo") || openFileDialog_HEXFILE.FileName.EndsWith(".hex")) { //Pathname = openhexFileDialog.getFilterPath() + "\\" + openhexFileDialog.getFileName(); Pathname = openFileDialog_HEXFILE.FileName; if (testUser.ReadFile(Pathname) == 0)//文件解析成功 { textBox_totalframe.Text = (UserExplainFile.Flash_SectionNum).ToString(); for (int i = 0; i < UserExplainFile.CurSecNum; i++) { totalsize += UserExplainFile.flash_Data[i].SectionDataNum; } totalsize += UserExplainFile.flash_Data[UserExplainFile.CurSecNum].SectionDataNum; textBox_totalsize.Text = totalsize.ToString(); gCrc32 = 0xffffffff; for (int i = 0; i < UserExplainFile.CurSecNum; i++) { gCrc32 = CHECKSUM.CRC32_MPEG_2(gCrc32, UserExplainFile.GetSectionData(i).data, (ushort)UserExplainFile.flash_Data[i].SectionDataNum); } gCrc32 = CHECKSUM.CRC32_MPEG_2(gCrc32, UserExplainFile.GetSectionData(UserExplainFile.CurSecNum).data, (ushort)UserExplainFile.flash_Data[UserExplainFile.CurSecNum].SectionDataNum); // gCrc32 = crc32; 文件CRC32.Text = gCrc32.ToString("X8"); } else { MessageBox.Show("文件解析失败"); textBox_totalframe.Text = ("0"); } } else { MessageBox.Show("文件类型错误"); } }
public void run() { //在任务里进行下载 while (true) { switch (updateStep) { case 0: //发送F6 固件信息 firmdata[0] = 0xf6; firmdata[1] = 0; //默认值 firmdata[2] = 0; //默认值 firmdata[3] = (byte)(totalsize); //totalsize firmdata[4] = (byte)(totalsize >> 8); //totalsize firmdata[5] = (byte)(totalsize >> 16); //totalsize firmdata[6] = (byte)(UserExplainFile.Flash_SectionNum & 0x00ff); //totalframe firmdata[7] = (byte)(UserExplainFile.Flash_SectionNum >> 8); //totalframe firmdata[8] = (byte)gCrc32; //crc32 firmdata[9] = (byte)(gCrc32 >> 8); //crc32 firmdata[10] = (byte)(gCrc32 >> 16); //crc32 firmdata[11] = (byte)(gCrc32 >> 24); //crc32 firmdata[12] = 0x00; //默认值 firmdata[13] = 0x00; //默认值 firmdata[14] = 0x00; //默认值 firmdata[15] = 0x00; //默认值 firmdata[16] = 0x00; //默认值 WriteflashCommandAPI(0x11, firmdata); updateStep = 4; Cycletimer = 300; break; case 1: downflashdata = UserExplainFile.GetSectionData(gLoadingSection); // WriteflashDataAPI(0xf7, UserExplainFile.flash_Data[gLoadingSection].SectionDataNum+3, (int)gLoadingSection, downflashdata.data); WriteflashDataAPI(0xf7, 0x80 + 3, (int)gLoadingSection, downflashdata.data); updateStep = 4; Cycletimer = 300; break; case 2: break; case 3: ReadReceiveRS485Data(); //发完进行读取 txCmdData[0] = 0xf4; txCmdData[1] = 0x00; WriteflashCommandAPI(2, txCmdData); if (cmdF4reply == 0x00) { cmdF4reply = 0xff; updateStep = 2; } break; case 4: ReadReceiveRS485Data(); //发完进行读取 if (cmdF6reply == 0x00) { cmdF6reply = 0xff; updateStep = 1; } if (cmdF7reply == 0x00) { gTimeOut = 0; cmdF7reply = 0xff; updateStep = 1; gLoadingSection += 1; if (gLoadingSection >= UserExplainFile.Flash_SectionNum) { updateStep = 2; } } else if (gTimeOut++ > 60) { gTimeOut = 0; updateStep = 1; } Cycletimer = 50; break; case 5: ReadReceiveRS485Data(); //发完进行读取 if (cmdF1reply == 0x00) { cmdF1reply = 0xff; MessageBox.Show("enter boot success"); updateStep = 2; } break; case 6: ReadReceiveRS485Data(); //发完进行读取 if (cmdF5reply == 0x00) { cmdF5reply = 0xff; updateStep = 2; } break; case 7: ReadReceiveRS485Data(); //发完进行读取 if (cmdF4reply == 0x00) { cmdF4reply = 0xff; updateStep = 2; } break; } Thread.Sleep(Cycletimer); } }
public int ReadHex(String pathname) { FileStream f = new FileStream(pathname, FileMode.Open); String readData = null; BufferedStream readbuf; // StreamReader readfile; // int SectionDataNum = 0; //数据长度 int lineType = 0; /*数据类型*/ UInt16 offset = 0; /*起始地址*/ int AddrHighPart = 0; /*起始地址高位*/ int temp = 0; /*临时变量*/ int i = 0; byte[] flashdata = new byte[UserConfig.FLASH_DATA_BUF_SIZE];/*存储临时一段的数据*/ int StartAddress = 0; int CurSection = 0; InitUserFile(); //初始化文件 gFileState = -1; /*文件允许读写*/ if (File.Exists(pathname) && f.CanRead) { try { readfile = new StreamReader(f); MemoryStream memStream = new MemoryStream(); readbuf = new BufferedStream(memStream); while ((readData = readfile.ReadLine()) != null) { /*读取数据长度,起始地址,及类型*/ CurSection++; offset = Convert.ToUInt16(readData.Substring(3, 4), 16); lineType = Convert.ToInt16(readData.Substring(7, 2), 16); SectionDataNum = Convert.ToInt16(readData.Substring(1, 2), 16); if (lineType == UserConfig.INTER_HEX_DATA)/*数据段 00 */ { for (i = 0; i < SectionDataNum; i++) { flashdata[i] = (byte)Convert.ToInt16(readData.Substring(9 + i * 2, 2), 16); } StartAddress = AddrHighPart + offset; if (CurSecNum == 0xffffff) { CurSecNum = 0; flash_Data[CurSecNum].StartAddress = StartAddress; flash_Data[CurSecNum].SectionDataNum = SectionDataNum; for (i = 0; i < SectionDataNum; i++) { flash_Data[CurSecNum].data[i] = flashdata[i]; } }//合并到一起 else { if ((StartAddress == (flash_Data[CurSecNum].StartAddress + flash_Data[CurSecNum].SectionDataNum)) && ((SectionDataNum + flash_Data[CurSecNum].SectionDataNum) <= UserConfig.FLASH_DATA_BUF_SIZE)) { for (i = 0; i < SectionDataNum; i++) { flash_Data[CurSecNum].data[flash_Data[CurSecNum].SectionDataNum + i] = flashdata[i]; } flash_Data[CurSecNum].SectionDataNum += SectionDataNum; } else { CurSecNum++; flash_Data[CurSecNum].StartAddress = StartAddress; flash_Data[CurSecNum].SectionDataNum = SectionDataNum; for (i = 0; i < SectionDataNum; i++) { flash_Data[CurSecNum].data[i] = flashdata[i]; } } } } else if (lineType == UserConfig.INTER_HEX_END) /*文件结束*/ // 01 { Flash_SectionNum = CurSecNum + 1; for (i = 0; i < Flash_SectionNum; i++) { flash_Data[i].CRC = UserExplainFile.CalcDataCrc(flash_Data[i].data, flash_Data[i].SectionDataNum); System.Console.Write("段号为:" + i + ",起始地址为:" + Convert.ToString((flash_Data[i].StartAddress), 16) + ",长度为:" + flash_Data[i].SectionDataNum + "\n"); } System.Console.Write("文件加载完成\n"); } /* 不存在*/ else if (lineType == UserConfig.INTER_HEX_SECTION) /*偏移段地址*/ { temp = Convert.ToInt16(readData.Substring(7, 4), 16); AddrHighPart = temp << 4; } else if (lineType == UserConfig.INTER_LINE_FLAG) /*偏移段线性地址*/ //04 文件开头 { temp = Convert.ToInt32(readData.Substring(9, 4), 16); // AddrHighPart = temp << 16; AddrHighPart = 0x8000000; System.Console.Write("AddrHighPart地址为:" + AddrHighPart.ToString() + "\n"); } else if (lineType == UserConfig.INTER_HEX_STARTADDR) //05 文件结束 { temp = Convert.ToInt32(readData.Substring(9, 8), 16); System.Console.Write("程序入口地址为:0x" + temp.ToString() + "\n"); } else { System.Console.Write("异常标识符:" + lineType + ",第" + CurSection + "行" + "\n"); readbuf.Close(); readfile.Close(); return(-1); } } readbuf.Close(); readfile.Close(); } catch (FileNotFoundException e) { System.Console.Write("转换异常" + "\n"); return(-1); } catch (IOException e) { System.Console.Write("转换异常" + "\n"); return(-1); } catch (NullReferenceException e) { System.Console.Write("转换异常" + "\n"); return(-1); } } gFileState = 0; //文件解析完成 return(0); }