public void ConvertSniffFileToNoEscapeMessages(string path, bool keepTimeStamp, bool asHex) { StreamReader tr = new StreamReader(path); string line; while ((line = tr.ReadLine()) != null) { if (!line.Contains(':')) { continue; } var time = Regex.Split(line, @" :")[0]; // get the time stamp line = Regex.Split(line, @" :")[1]; // get everything after the colon // remove whitespace string rxMessage = String.Concat(line.Where(c => !Char.IsWhiteSpace(c))); // remove white space // convert to bytes var justBytes = StringToByteArray(rxMessage); var noEscapes = RemoveEscapes(justBytes); if (noEscapes[3] != 0x90) { continue; } string newSuffix = keepTimeStamp ? "_proc_w_time.txt" : "_proc.txt"; using (StreamWriter sw = new StreamWriter(path.Replace(".txt", newSuffix), true)) { long unixTime = new DateTimeOffset(DateTime.Parse(time)).ToUnixTimeMilliseconds(); if (keepTimeStamp) { sw.Write(unixTime + " :"); } if (asHex) { sw.WriteLine(DebugHelper.ByteArrayToHexString(noEscapes)); } else { sw.WriteLine(new NodeDataBySerialNumber(noEscapes).ToString()); } sw.Flush(); sw.Close(); // don't care how inefficient this is } } }
private void PortOnDataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; byte[] rxData = new byte[sp.BytesToRead]; sp.Read(rxData, 0, rxData.Length); foreach (var b in rxData) // gotta go through each one in case there is no break between messages { if (b == 0x7E && rxBytes.Count > 0) { var lastLine = RemoveEscapes(rxBytes.ToArray()); string diagOutput = $"{DateTime.Now:O} :{DebugHelper.ByteArrayToHexString(lastLine)}"; // using (StreamWriter sw = new StreamWriter( path + "\\live_asc_with_time.txt", true)) // { // // sw.WriteLine(diagOutput); // sw.Flush(); // sw.Close(); // } Debug.WriteLine(diagOutput); // ProcessLiveData(lastLine, DateTime.Now); rxBytes.Clear(); } rxBytes.Add(b); var readyBytes = RemoveEscapes(rxBytes.ToArray()); if (readyBytes.Length >= 48 && readyBytes[2] == 0x2C) { ProcessLiveData(readyBytes, DateTime.Now); } } }
public void ReadFile(string path) { // read file remove escapes, sort message types, parse time, serial number and packet number List <Metrics> metrics = new List <Metrics>(); using (StreamReader tr = new StreamReader(path)) { // divide file into individual messages string wholeFile = tr.ReadToEnd(); string[] wholeFileAsStrings = Regex.Split(wholeFile, @"7E"); // using (StreamWriter sw = new StreamWriter("c:\\temp\\messages.txt")) { foreach (string s in wholeFileAsStrings) { if (s == "") { continue; } // remove whitespace string message = String.Concat(s.Where(c => !Char.IsWhiteSpace(c))); // remove white space // convert to bytes var asBytes = StringToByteArray(message); // remove escapes var noEscapes = RemoveEscapes(asBytes); NodeDataBySerialNumber nd = new NodeDataBySerialNumber(noEscapes); if (nd.Code != 0) { var metric = metrics.FirstOrDefault(o => o.SerialId == nd.SerialId.Value); if (metric == null) { metrics.Add(new Metrics(nd)); } else { metric.Add(nd); } } // convert back to a string, lol string noEscapesString = DebugHelper.ByteArrayToHexString(noEscapes); // write it to file sw.WriteLine($"7E{noEscapesString}".Trim()); } foreach (var m in metrics) { m.PrintMetrics(); } sw.Flush(); sw.Close(); } // using (StreamWriter sw = new StreamWriter("C:\\temp\\Roof.txt")) // { // sw.Write(wholeFileAsString); // // sw.Flush(); // sw.Close(); // } } // using (BinaryWriter writer = new BinaryWriter(File.Open("C:\\temp\\Roof.bin", FileMode.Create))) // { // var messageBytes = RemoveEscapes(wholeFileAsBytes); // // writer.Flush(); // writer.Close(); // } }