public void ReadMem(HexMemoryBlock newValues) { if (newValues == null || newValues.DataBlock?.Length == 0) { return; } uint from = Math.Max(this.BaseAddress, newValues.BaseAddress); uint to = Math.Min(this.LastAddress, newValues.LastAddress); for (uint i = from; i < to; i++) { newValues.DataBlock[i - newValues.BaseAddress] = DataBlock[i - BaseAddress]; } }
public void WriteMem(HexMemoryBlock newValues) { if (newValues == null) { return; } uint from = Math.Max(this.BaseAddress, newValues.BaseAddress); uint to = Math.Min(this.LastAddress, newValues.LastAddress); for (uint i = from; i < to; i++) { DataBlock[i - BaseAddress] = newValues.DataBlock[i - newValues.BaseAddress]; } }
private void buttonProgram_Click(object sender, EventArgs e) { try { if (tabControl1.SelectedTab == tabPageOriginal) { this.DialogResult = DialogResult.OK; return; } if (tabControl1.SelectedTab == tabPageNumber) { // Write 8 equal telegrams to the tx encoded without status int txNumber = -1; if (!int.TryParse(textBoxNumber.Text.Trim(), out txNumber)) { throw new Exception("Invalid transponder number"); } if (txNumber < 2097152 || txNumber > 9999999) { throw new ArgumentException("Transponder number must be between 2097152 and 9999999"); } HexMemoryBlock h = new HexMemoryBlock(); h.DataBlock = new ushort[12]; byte[] b = TransponderEncoding.encodeTransponderNoStatus((uint)txNumber); for (int i = 0; i < b.Length; i++) { h.DataBlock[i] = (ushort)(0x03400 | b[i]); } uint baseAddress = 0x703; for (int i = 0; i < 8; i++, baseAddress += 12) { h.BaseAddress = baseAddress; Program.WriteMem(h); } this.DialogResult = DialogResult.OK; return; } if (tabControl1.SelectedTab == tabPageAdvanced) { HexMemoryBlock h = new HexMemoryBlock(); h.DataBlock = new ushort[12]; uint baseAddress = 0x703; for (int i = 0; i < 8; i++, baseAddress += 12) { h.BaseAddress = baseAddress; String val = dt.Rows[i][2].ToString().Trim().ToUpperInvariant(); if (val.Length != 24) { throw new Exception($"Wrong value {dt.Rows[i][1]}"); } try { for (int j = 0; j < 12; j++) { h.DataBlock[j] = (ushort)(0x03400 | Convert.ToInt16(val.Substring(2 * j, 2), 16)); } } catch { throw new Exception($"Wrong value {dt.Rows[i][1]}"); } Program.WriteMem(h); } this.DialogResult = DialogResult.OK; return; } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
/// <summary> /// Fills the memory blocks from the stream /// </summary> /// <param name="s"></param> public void ParseHex(StreamReader s) { uint address = 0; byte [] buffer = new byte[255]; Records = new MemoryContent(); int lineNumber = 0; while (!s.EndOfStream) { try { String line = s.ReadLine().Trim(); lineNumber++; if (String.IsNullOrEmpty(line)) { continue; } if (!line.StartsWith(":")) { continue; } if (line.Length < 11) { throw new Exception("Unexpected end of line"); } uint byteCount = Convert.ToUInt16(line.Substring(1, 2), 16); uint lineAddress = Convert.ToUInt16(line.Substring(3, 4), 16); uint recordType = Convert.ToUInt16(line.Substring(7, 2), 16); if (line.Length < 11 + byteCount) { throw new Exception("Line too short"); } for (int i = 0; i < byteCount; i++) { buffer[i] = Convert.ToByte(line.Substring(9 + 2 * i, 2), 16); } switch (recordType) { case 1: return; // Eof record case 0: // Data record { HexMemoryBlock b = new HexMemoryBlock(); b.BaseAddress = (address | lineAddress) / 2; // Pic specific addressing b.DataBlock = new UInt16[byteCount / 2]; for (int i = 0; i < byteCount; i += 2) { UInt16 number = buffer[i + 1]; number = (UInt16)(number << 8 | buffer[i]); b.DataBlock[i / 2] = number; } Records.Add(b); break; } case 4: // Extended linear address { if (byteCount != 2) { throw new Exception("Wrong byte count in Extended Linear Address record"); } address = buffer[0]; address = (address << 8 | buffer[1]) << 16; break; } } } catch (Exception ex) { throw new Exception($"Parsing error at line {lineNumber}. Cause: {ex.Message}", ex); } } }