المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Looking for help deciphering this code class



C# Programming
01-14-2012, 06:21 PM
Hi,

I know this may be asking a lot, I found this code class and it makes little sense to me in some places, so I was looking for some help deciphering it. It is designed to query a serial port and communicate with a microchip.


Some of the confusing parts http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

private const uint COMMAND_LOAD_RAM_AND_RUN = 1; private const uint COMMAND_LOAD_EEPROM_AND_RUN = 3;
lfsrData[0] = 0xF9; // 0xF9 is 249 in hex for (int i = 1; i < 251; ++i) lfsrData[i] = (byte)(0xFE | (IterateLFSR() ? 1 : 0)); // 0xFE is 254 in hex
byte[] buffer = new byte[258]; for (int i = 0; i < 258; ++i) buffer[i] = 0xF9; // 0xF9 is 249 in hex

This part really throws me ...

for (int i = 0; i < 250; ++i) { if (ReceiveBit(false, 100) != IterateLFSR()) throw new ApplicationException("Receiving LFSR data failed!"); } //Receive pr version byte version = 0; for (int i = 0; i < 8; ++i) { version >>= 1; version += (byte)(ReceiveBit(false, 100) ? 128 : 0); }


And here is the whole class itself, thank you for reading.

using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.IO; using System.Threading; namespace PrBooter { public class Booter { /// /// Creates the booter, but doesn't open serial port and won't do anything. /// /// Eg. "COM1" public Booter(string portName) { m_SerialPort = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One); m_SerialPort.ReadTimeout = 3000; } #region Constants private const uint COMMAND_LOAD_RAM_AND_RUN = 1; private const uint COMMAND_LOAD_EEPROM_AND_RUN = 3; #endregion private SerialPort m_SerialPort = null; #region Public stuff /// /// Just redirects to SerialPort.GetPortNames() /// /// public static string[] GetPortNames() { return SerialPort.GetPortNames(); } /// /// Gets or sets the portname (eg. "COM1") /// public string PortName { get { return m_SerialPort.PortName; } set { if (m_SerialPort.IsOpen) throw new InvalidOperationException("Cannot set PortName while Serial is open!"); m_SerialPort.PortName = value; } } public void ResetPr() { m_SerialPort.DtrEnable = true; Thread.Sleep(10); //Wait 10 msec m_SerialPort.DtrEnable = false; } /// /// Programs EEPROM and runs. /// public void Reprogram(string filename) { Stream s = File.OpenRead(filename); try { Reprogram(s); } finally { s.Close(); } } /// /// Programs EEPROM and runs. Doesn't close the stream!! /// /// public void Reprogram(Stream stream) { try { m_SerialPort.Open(); byte[] programData = new byte[(int)stream.Length]; stream.Read(programData, 0, (int)programData.Length); StartComm(COMMAND_LOAD_EEPROM_AND_RUN); SendProgram(programData); //Check EEPROM Checksums if (ReceiveBit(true, 5000)) throw new ApplicationException("EEPROM Programming Failed!"); if (ReceiveBit(true, 2500)) throw new ApplicationException("EEPROM Verification Failed!"); } finally { m_SerialPort.Close(); } } /// /// Loads ram and runs /// /// public void Boot(string filename) { Stream s = File.OpenRead(filename); try { Boot(s); } finally { s.Close(); } } /// /// Loads RAM and runs. Doesn't close the stream! /// /// public void Boot(Stream stream) { try { m_SerialPort.Open(); byte[] programData = new byte[(int)stream.Length]; stream.Read(programData, 0, (int)programData.Length); StartComm(COMMAND_LOAD_RAM_AND_RUN); SendProgram(programData); } finally { m_SerialPort.Close(); } } #endregion #region High level protocol private void SendProgram(byte[] programData) { //Transmit number of LONGS in the image int longCount = (programData.Length + 3) >> 2; TransmitEncodedLong((uint)longCount); //Transmit the program image as encoded LONGs for (int i = 0; i < longCount; ++i) { int byteIdx = (i >= 1; version += (byte)(ReceiveBit(false, 100) ? 128 : 0); } TransmitEncodedLong(command); return version; } #endregion #region Low level protocol /// /// Receives an encoded bit, if echo on, then constantly writes stuff to serial. /// /// /// /// private bool ReceiveBit(bool echoOn, int msecs) { unchecked { DateTime entered = System.DateTime.Now; while (!echoOn | DateTime.Now - entered < TimeSpan.FromMilliseconds(msecs)) { if (echoOn) { m_SerialPort.Write(new byte[] { 0xF9 }, 0, 1); // 0xF9 is 249 in hex Thread.Sleep(25); } if (!echoOn || m_SerialPort.BytesToRead > 0) { byte receivedBit = (byte)(m_SerialPort.ReadByte() - 0xFE); // 0xFE is 254 in hex if (receivedBit > 1) { //Reception error throw new ApplicationException("Receiving bit failed!"); } return receivedBit == 1; } } throw new TimeoutException("Receiving bit in echo mode timed out!"); } } /// /// Encodes the long into 11 bytes and transmits it. /// /// private void TransmitEncodedLong(uint data) { byte[] buffer = new byte[11]; for (int i = 0; i < 11; ++i) { buffer[i] = (byte)(0x92 | ((byte)(i == 10 ? 255 : 0) & 0x60) | (data & 1) | (data & 2) = 3; } m_SerialPort.Write(buffer, 0, 11); } #endregion #region Linear Feedback Shift Register private byte _LFSR = 80; //LFSR is initially 80 or ASCII for P /// /// Resets LFSR to 'P'. /// private void ResetLFSR() { _LFSR = 80; } /// /// Gets a bit from LFSR & advances it by one step. /// /// private bool IterateLFSR() { unchecked { bool result = (_LFSR & 1) == 1; _LFSR = (byte)((_LFSR > 7 ^ _LFSR >> 5 ^ _LFSR >> 4 ^ _LFSR >> 1) & 1); return result; } } #endregion } }