void Mnu_openHexdumpClick(object sender, EventArgs e) { OpenFileDialog opendia = new OpenFileDialog(); opendia.InitialDirectory = TBL.OperatingSystem.DesktopDirectory; opendia.Filter = "hex files (*.hex)|*.hex|All files (*.*)|*.*"; opendia.FilterIndex = 1; opendia.RestoreDirectory = false; if (opendia.ShowDialog() == DialogResult.OK) { int charCount = GetNumOfCharsInFile(opendia.FileName); int byteCount = charCount / 2; ThreadsafeReceiveBuffer recBuffer = new ThreadsafeReceiveBuffer(byteCount + 1); int currentCharacter = 0; string curFigure = ""; int ascii; // well.. now read try { using (System.IO.StreamReader sr = new System.IO.StreamReader(opendia.FileName)) { while ((ascii = sr.Read()) != -1) { curFigure += ((char)(ascii)).ToString(); if ((currentCharacter % 2) == 1) { int intval = Convert.ToInt32(curFigure, 16); //Using ToUInt32 not ToUInt64, as per OP comment recBuffer.AddByte((byte)(intval)); curFigure = ""; } currentCharacter++; } } } catch { stdOut.Error("Parsing error at Character " + currentCharacter.ToString()); } searchForFrames(recBuffer); this.Controls.Add(log.Visualisation); log.VisualizationFitWindowSize(this); stdOut.Info("should now open " + opendia.FileName); } }
/// <summary> /// Sucht die Position des ersten gültigen M3S-Frames in einem übergebenen Buffer /// </summary> /// <param name="rBuffer">Buffer, der durchsucht werden soll</param> /// <returns>Position des ersten gültigen M3S-Frames (-1 wenn nichts gefunden wurde)</returns> int GetFirstValidFramePosition(ThreadsafeReceiveBuffer rBuffer, out M3SProtocol oProtocol) { oProtocol = M3SProtocol.Invalid; if (!rBuffer.DataAvailable) // Buffer Empty { return(-1); } if (rBuffer.DataPtr < protocol.MinimumFrameLength) // not enough Data available { return(-1); } int upperBound = -1; for (int start = 0; start <= rBuffer.DataPtr - protocol.MinimumFrameLength; start++) { if (han.ExtractProtocol(rBuffer.ReadByte(start)) == M3SProtocol.Acknowledge) { if (protocol.IsFrame(rBuffer.readBytes(start, start + 4))) { oProtocol = M3SProtocol.Acknowledge; return(start); } } upperBound = rBuffer.ReadByte(start + protocol.UpperBoundPosition); // Ausgelesenes Upperbound aus angenommener Bitposition.. byte[] frameToTest = rBuffer.readBytes(start, start + upperBound + protocol.Overhead); // Zu testendes Frame if (protocol.IsFrame(frameToTest)) { oProtocol = han.ExtractProtocol(frameToTest[0]); return(start); } } return(-1); }
void searchForFrames(ThreadsafeReceiveBuffer rBuffer) { searching.WaitOne(); M3SProtocol lastAnalyzedProtocol; int firstAvailable = GetFirstValidFramePosition(rBuffer, out lastAnalyzedProtocol); while (firstAvailable != -1) { if (firstAvailable != 0) // ganz am Anfang im Buffer steht irgendwas, aber kein gültiges Frame { byte[] someStream = rBuffer.readBytes(0, firstAvailable - 1); log.Add(someStream); } int frameEndPosition; if (lastAnalyzedProtocol == M3SProtocol.Acknowledge) { frameEndPosition = firstAvailable + protocol.AcknowledgeFrameLength - 1; } else { frameEndPosition = firstAvailable + rBuffer.ReadByte(firstAvailable + protocol.UpperBoundPosition) + protocol.Overhead; } byte[] frame = rBuffer.readBytes(firstAvailable, frameEndPosition); log.Add(frame); // Entfernen der gelesenen Byte.. rBuffer.FreeBytes(0, frameEndPosition); rBuffer.Flush(); firstAvailable = GetFirstValidFramePosition(rBuffer, out lastAnalyzedProtocol); // Search for next files... } searching.Release(); }