/** * @brief Scans a project location of file and fills the BBCodeBase. * @param fileName is path to a directory or a single file. * @param bbCodeBase clears all entries in this object and adds the new entries. * @return true on successful scan. */ private static bool ScanSingleFile(string fileName, ref BBCodeBase bbCodeBase) { try { // Open the file as text. StreamReader sr = File.OpenText(fileName); // Iterate all lines until the end. string lineText; int lineNumber = 0; while (!sr.EndOfStream) { // Read single line. lineText = sr.ReadLine(); lineNumber++; // Try to parse the line with descriptor parser. Result is not null if successful. BBCodeObject bbCode = BBCodeDescriptorParser.Parse(lineText, fileName, lineNumber); // Add to the database (only if not null). bbCodeBase.AddBBCode(bbCode); } // Close the file. sr.Close(); // Everything is OK. return(true); } catch { } // Problem occured. return(false); }
/** * @brief Periodically called serial data receiver and parser. * @param bbCodeBase BBCode database to process the received data. * @param maxDataRows data table line number limit. * @return true if new message received and processed. */ public bool GetSerialData(ref BBCodeBase bbCodeBase, int maxDataRows) { // Save the time on entry to measure performance. DateTime timeEntry = DateTime.Now; // Keep a flag that is true when a new line received. bool newDataReceived = false; // Null check for serial port. Then check if there is data ready to be read. if ((serialPort != null) && (serialPort.IsOpen) && (serialPort.BytesToRead > 0)) { // Read and append all available data into buffer. serialData += serialPort.ReadExisting(); // Split the the buffer data into lines. string[] serialLines = serialData.Split('\n'); // Start dataTable update. dataTable.BeginLoadData(); // Process all lines except the last element, because it is not completed yet. for (int i = 0; i < (serialLines.Length - 1); i++) { // Process the received line of text. newDataReceived |= ProcessReceivedLine(serialLines[i], ref bbCodeBase, ref dataTable); // Remove the old lines to limit the data table size. DeleteOldLines(ref dataTable, maxDataRows); } // Assign the unused (last) element to the buffer. serialData = serialLines[serialLines.Length - 1]; } // If new data received and processed successfully. if (newDataReceived) { // Conclude dataTable update. dataTable.EndLoadData(); // Log the time spent here. TimeSpan timeSpan = DateTime.Now.Subtract(timeEntry); Console.WriteLine("GetSerialData time[ms]: " + timeSpan.TotalMilliseconds + " Rows: " + dataTable.Rows.Count); } // Return. return(newDataReceived); }
/** * @brief Process the received line of data. * @param receivedLine the unprocessed line data. * @param dataTable Data table to fill the received data. * @param bbCodeBase BBCode database to process the received data. */ private bool ProcessReceivedLine(string receivedLine, ref BBCodeBase bbCodeBase, ref DataTable dataTable) { // Decode the message. string decodedMessage = bbCodeBase.GetDecodedMessage(receivedLine); // Check message length. if (decodedMessage != "") { // Add time. dataTable.Rows.Add(new object[] { DateTime.Now.ToString("HH:mm:ss.ffffff"), decodedMessage }); // Return true. return(true); } else { // Return false. return(false); } }
/** * @brief Scans a project location of file and fills the BBCodeBase. * @param projectPath is path to a directory or a single file. * @param supportedFileExtensions string array of extensions. * @param bbCodeBase clears all entries in this object and adds the new entries. * @return true if entries are found. */ public static bool Scan(string projectPath, string[] supportedFileExtensions, ref BBCodeBase bbCodeBase) { // Create an empty BBCode database. bbCodeBase = new BBCodeBase(); // Check if the given is a file or directory. if (File.Exists(projectPath)) { // Scan single file. ScanSingleFile(projectPath, ref bbCodeBase); } else { // Create empty file list. string[] fileList = { }; // Try to get file names under the selected path. try { fileList = Directory.GetFiles(projectPath, "*.*", SearchOption.AllDirectories); } catch { }; // Get the selected path and make sure that its not empty. if (fileList.Length > 0) { // Iterate all files under the selected path. foreach (string fileName in fileList) { // Check the file extension for allowed extensions. if (File.Exists(fileName) && ((fileList.Length == 1) || supportedFileExtensions.Contains(Path.GetExtension(fileName).TrimStart('.')))) { ScanSingleFile(fileName, ref bbCodeBase); } } } } // Success if descriptors are found. return(bbCodeBase.GetCodeCount() > 0); }