private void SendRecord(HexRecord hexRecord) { using (var memoryStream = new MemoryStream()) { hexRecord.Serialize(memoryStream); Send(memoryStream.ToArray()); } }
/// 将数据读取到数组 public byte[] ReadHexFileToByte(string HexFileName) { byte[] BinBuff = new byte[0]; try { ///文件读入list using (StreamReader Reader = new StreamReader(HexFileName)) { string Line = ""; while ((Line = Reader.ReadLine()) != "") { HexRecord hexRecord = new HexRecord(Line); if (hexRecord.DataType == 0x01) { break; } HexRecords.Add(hexRecord); } } ///统计最大地址和最小地址 int MinAddr = int.MaxValue; int MaxAddr = int.MinValue; int Shift = 0; foreach (HexRecord Record in HexRecords) { if (Record.DataType == 0x04) { Shift = ((Record.Data[0] << 8) | Record.Data[1]) << 16; } int CurAddrStart = Shift + Record.Address; int CurAddrEnd = Shift + Record.Address + Record.DataLen; MinAddr = (MinAddr > CurAddrStart ? CurAddrStart : MinAddr); MaxAddr = (MaxAddr < CurAddrEnd ? CurAddrEnd : MaxAddr); } ///填充 BinBuff = new byte[MaxAddr - MinAddr]; Shift = 0; foreach (HexRecord Record in HexRecords) { if (Record.DataType == 0x04) { Shift = ((Record.Data[0] << 8) | Record.Data[1]) << 16; } if (Record.DataType == 0x00) { int CurAddr = Shift + Record.Address - MinAddr; Array.Copy(Record.Data, 0, BinBuff, CurAddr, Record.DataLen); } } } catch { Console.WriteLine("ERROR"); Console.ReadKey(); } return(BinBuff); }
public static bool Verify(string hexFileName) { HexParser hexParser = new HexParser(hexFileName); hexParser.StartReading(); int pageSize = 32; HexRecord record = hexParser.ReadLine(); byte[] data = new byte[pageSize]; Commander cmd = new Commander(); try { FileStream usb = cmd.GetUSB(); byte[] nill = new byte[100]; // usb.Read(nill, 0, 100); } catch { } bool success = true; if (cmd.BeginProgramming()) { while (record.Type != HexRecord.EOF && success) { switch (record.Type) { case HexRecord.DataType: cmd.Address = record.Address / 2; cmd.Read(data, record.Length); for (int i = 0; i < record.Length; i++) { if (data[i] != record.Data[i]) { success = false; break; } } break; case HexRecord.ExtendedLinearAddressRecord: case HexRecord.ExtendedSegmentAddress: throw new Exception("Not Supporte Yet"); } record = hexParser.ReadLine(); } } cmd.Dispose(); hexParser.Dispose(); return(success); }
public ResultEnum FileCheck_Keil(StreamReader SR, out int ErrorLineNo) { HexRecord record = new HexRecord(); ResultEnum result = ResultEnum.Success; ErrorLineNo = -1; int no = 0; while (!SR.EndOfStream) { string s = SR.ReadLine(); result = Parse(s, out record); if (result != ResultEnum.Success) { ErrorLineNo = no; return(result); } if (no == 0) { if (record.Type != HexRecordType.ExtenLinAdd) { ErrorLineNo = no; return(ResultEnum.FormatWrong); } } else { if (record.Type == HexRecordType.Data) { //continue; } else if (record.Type == HexRecordType.EOF) { break; } else { ErrorLineNo = no; return(ResultEnum.FormatWrong); } } no++; } if (record.Type != HexRecordType.EOF || !SR.EndOfStream) { ErrorLineNo = no; return(ResultEnum.FormatWrong); } return(ResultEnum.Success); }
public async Task ProgramHexFile(Stream stream) { Send(new [] { (byte)Commands.Ready }); HexRecord record = null; using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream && record?.Type != LAST_RECORD) { var command = (Commands)ReceiveByte(); Console.WriteLine(command); switch (command) { case Commands.Ready: { var line = await reader.ReadLineAsync(); if (line == null) { return; } record = new HexRecord(line); Console.WriteLine($"Sending {line}"); SendRecord(record); break; } case Commands.Resent: { if (record == null) { throw new InvalidOperationException("Received a resent command however not even one record has been sent yet."); } SendRecord(record); break; } } } Console.WriteLine($"Done"); } }
public void HexRecordParse() { Action <HexRecord, string> func = (expectedHexRecord, record) => { IHexRecordParser parser = new HexRecordParser(); HexRecord r = parser.Parse(record); Assert.AreEqual <HexRecordType>(expectedHexRecord.RecordType, r.RecordType); Assert.AreEqual <long>(expectedHexRecord.Address, r.Address); if (expectedHexRecord is DataHexRecord) { Assert.IsTrue(r is DataHexRecord); Assert.IsTrue(Enumerable.SequenceEqual(((DataHexRecord)expectedHexRecord).Data, ((DataHexRecord)r).Data)); } }; func(new AddressHexRecord(HexRecordType.ExtendedSegmentAddress, 0x10000), ":020000021000EC"); Assert.AreEqual(":00000001FF", HexRecord.EOF); func(new AddressHexRecord(HexRecordType.ExtendedSegmentAddress, 0), ":020000020000FC"); func(new DataHexRecord(HexRecordType.Data, 0, new byte[] { 0xFA, 0x00, 0x00, 0x02 }), ":04000000FA00000200"); func(new DataHexRecord(HexRecordType.Data, 0xC220, new byte[] { 0xF0, 0x4E, 0xF0, 0x5F, 0xF0, 0x6C, 0xF0, 0x7D, 0xCA, 0x00, 0x50, 0xC2, 0xF0, 0x86, 0xF0, 0x97 }), ":10C22000F04EF05FF06CF07DCA0050C2F086F097DF"); }
public ResultEnum Parse(string RecordString, out HexRecord Record) { Record = new HexRecord(); int offset = 0; if (RecordString == null) { return(ResultEnum.LengthWrong); } if (RecordString.Length < 11) { return(ResultEnum.LengthWrong); } if (RecordString.Length % 2 != 1) { return(ResultEnum.LengthWrong); } if (NumCheck(RecordString, 1) == false) { return(ResultEnum.NumWrong); } if (SumCheck(RecordString, 1) == false) { return(ResultEnum.SumWrong); } //Start ':' if (RecordString[offset++] != ':') { return(ResultEnum.FlagWrong); } //Byte Count int byteCount = HexByteParse(RecordString, offset); offset += 2; if (RecordString.Length - 11 != byteCount * 2) { return(ResultEnum.LengthWrong); } //Address byte[] address = HexStringParseMSB(RecordString, offset, 2); offset += 4; Record.Address = BitConverter.ToUInt16(address, 0); //Type byte type = HexByteParse(RecordString, offset); offset += 2; if (Enum.IsDefined(typeof(HexRecordType), (int)type) == false) { return(ResultEnum.TypeWrong); } Record.Type = (HexRecordType)type; //Data Record.Data = HexStringParse(RecordString, offset, byteCount); offset += 2 * byteCount; //ExState if (Record.Type == HexRecordType.Data) { if (_OffsetAddress + _ByteCount == Record.Address) { _IsAddressJump = false; } else { _IsAddressJump = true; } _OffsetAddress = Record.Address; _ByteCount = Record.ByteCount; } else if (Record.Type == HexRecordType.ExtenSegAdd) { _IsAddressJump = true; _BaseAddress = (Record.Data[0] * 256 + Record.Data[1]) * 16; _OffsetAddress = 0; _ByteCount = 0; } else if (Record.Type == HexRecordType.StartSegAdd) { _IsAddressJump = true; _BaseAddress = (Record.Data[0] * 256 + Record.Data[1]) * 16; _OffsetAddress = 0; _ByteCount = 0; } else if (Record.Type == HexRecordType.ExtenLinAdd) { _IsAddressJump = true; _BaseAddress = (Record.Data[0] * 256 + Record.Data[1]) * 65536; _OffsetAddress = 0; _ByteCount = 0; } else if (Record.Type == HexRecordType.StartLinAdd) { _IsAddressJump = true; _BaseAddress = (Record.Data[0] * 256 + Record.Data[1]) * 65536; _OffsetAddress = 0; _ByteCount = 0; } else if (Record.Type == HexRecordType.EOF) { _IsAddressJump = false; _BaseAddress = 0; _OffsetAddress = 0; _ByteCount = 0; } return(ResultEnum.Success); }
public string UnParse(HexRecord hexRecord) { return(hexRecord.ToString()); }
public string UnParse(HexRecord hexRecord) { return hexRecord.ToString(); }
public static void Write(string hexFileName) { HexParser hexParser = new HexParser(hexFileName); hexParser.StartReading(); int pageSize = 32; HexRecord record = hexParser.ReadLine(); byte[] data = new byte[pageSize]; int totalWritten = 0; Commander cmd = new Commander(); try { FileStream usb = cmd.GetUSB(); byte[] nill = new byte[100]; // usb.Read(nill, 0, 100); } catch { } if (cmd.BeginProgramming()) { cmd.ChipErase(); while (record.Type != HexRecord.EOF) { switch (record.Type) { case HexRecord.DataType: for (int i = 0; i < record.Length; i++) { if (totalWritten == pageSize || (((record.FullAddress + i) / (2 * pageSize)) != (cmd.Address / pageSize))) { cmd.Write(data, totalWritten); cmd.Address = (int)(cmd.Address) + (totalWritten / 2); totalWritten = 0; } data[totalWritten++] = record.Data[i]; } break; case HexRecord.ExtendedLinearAddressRecord: case HexRecord.ExtendedSegmentAddress: throw new Exception("Not Supporte Yet"); } record = hexParser.ReadLine(); } if (totalWritten != 0) { cmd.Write(data, totalWritten); } } cmd.Dispose(); hexParser.Dispose(); }
protected void OnSave(object sender, EventArgs e) { HexRecord hr = new HexRecord(); this.textview_debug.Buffer.Text = "Hex Record Test:\n"; string a = " :10010000214601360121470136007EFE09D2190140"; string b = ":00000001FF "; string c = " :100110002146017EB7C20001FF5F16002148011988"; this.textview_debug.Buffer.Text += (a + "\n"); hr.parse(a); this.textview_debug.Buffer.Text += hr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (b + "\n"); hr.parse(b); this.textview_debug.Buffer.Text += hr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (c + "\n"); hr.parse(c); this.textview_debug.Buffer.Text += hr.generate(); SRecord sr = new SRecord(); this.textview_debug.Buffer.Text += "\nS Record Test:\n"; string d = " S00F000068656C6C6F202020202000003C"; string ee = "S11F00007C0802A6900100049421FFF07C6C1B787C8C23783C6000003863000026"; string f = " S5030003F9 "; this.textview_debug.Buffer.Text += (d + "\n"); sr.parse(d); this.textview_debug.Buffer.Text += sr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (ee + "\n"); sr.parse(ee); this.textview_debug.Buffer.Text += sr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (f + "\n"); sr.parse(f); this.textview_debug.Buffer.Text += sr.generate(); this.textview_debug.Buffer.Text += "\n"; TiTxtRecord tr = new TiTxtRecord(); this.textview_debug.Buffer.Text += "TI Txt Record Test:\n"; string g = "@FABC"; string h = "31 40 00 03 B2 40 80 5A 20 01 D2 D3 22 00 D2 E3 "; string i = "q "; string j = " 21 00 3F 40 E8 FD 1F 83 FE 23 F9 3F "; this.textview_debug.Buffer.Text += (g + "\n"); tr.parse(g); this.textview_debug.Buffer.Text += tr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (h + "\n"); tr.parse(h); this.textview_debug.Buffer.Text += tr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (i + "\n"); tr.parse(i); this.textview_debug.Buffer.Text += tr.generate(); this.textview_debug.Buffer.Text += "\n"; this.textview_debug.Buffer.Text += (j + "\n"); tr.parse(j); this.textview_debug.Buffer.Text += tr.generate(); this.textview_debug.Buffer.Text += "\n"; Console.WriteLine("have a try"); }