From 36e49162805408ec036098308e4583b5aa84c3e8 Mon Sep 17 00:00:00 2001 From: Edward Wright Date: Mon, 10 May 2021 21:22:02 -0400 Subject: [PATCH] Added timeouts to force-flush the log buffer --- SimplySerial/SimplySerial.cs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/SimplySerial/SimplySerial.cs b/SimplySerial/SimplySerial.cs index b16b69f..c552199 100644 --- a/SimplySerial/SimplySerial.cs +++ b/SimplySerial/SimplySerial.cs @@ -32,6 +32,7 @@ namespace SimplySerial static string logFile = string.Empty; static string logData = string.Empty; static int bufferSize = 4096; + static DateTime lastFlush = DateTime.Now; static void Main(string[] args) @@ -184,6 +185,12 @@ namespace SimplySerial (logging == true) ? ($"Logfile : {logFile} (Mode = " + ((logMode == FileMode.Create) ? "OVERWRITE" : "APPEND") + ")\n" ) : "" ), flush: true); + + lastFlush = DateTime.Now; + DateTime start = DateTime.Now; + TimeSpan timeSinceRX = new TimeSpan(); + TimeSpan timeSinceFlush = new TimeSpan(); + // this is the core functionality - loop while the serial port is open while (serialPort.IsOpen) { @@ -226,6 +233,22 @@ namespace SimplySerial // write what was received to console Output(received, force: true, newline: false); + start = DateTime.Now; + } + else + { + Thread.Sleep(1); + if (logging) + { + timeSinceRX = DateTime.Now - start; + timeSinceFlush = DateTime.Now - lastFlush; + if ((timeSinceRX.TotalSeconds >= 2) || (timeSinceFlush.TotalSeconds >= 10)) + { + if (logData.Length > 0) + Output("", force: true, newline: false, flush: true); + start = DateTime.Now; + } + } } } catch (Exception e) @@ -490,7 +513,8 @@ namespace SimplySerial if (newline) message += "\n"; - Console.Write(message); + if (message.Length > 0) + Console.Write(message); if (logging) { @@ -504,6 +528,7 @@ namespace SimplySerial { writer.Write(logData); } + lastFlush = DateTime.Now; } catch {