// Use this string when setting up internal database connection functions. // readonly string connString = ServiceMain.YamlSettings?.ServiceConfig?.SqlitePath!; public CommFacilitator(Serial serialSettings) { portSettings = serialSettings; if (serialSettings.UseLegacyFrameSize) { frameSize = 240; } else { frameSize = 63993; } password = serialSettings.Password; try { // Set the serial port properties and try to open it. receiver_id = serialSettings.ReceiverId; ComPort = new CommPort(serialSettings); CommState = new LisCommState(this); rcvTimer = new CountdownTimer(-1, ReceiptTimedOut); transTimer = new CountdownTimer(-1, TransactionTimedOut); CurrentMessage = new Message(this); // Set the handler for the DataReceived event. ComPort.PortDataReceived += CommPortDataReceived !; ComPort.Open(); AppendToLog($"Port opened: {serialSettings.Portname}"); idleTimer.AutoReset = true; idleTimer.Elapsed += new System.Timers.ElapsedEventHandler(IdleTime); if (serialSettings.AutoSendOrders > 0) { idleTimer.Elapsed += WorklistTimedEvent; idleTimer.Interval = serialSettings.AutoSendOrders; } else { idleTimer.Interval = 10000; } idleTimer.Start(); } catch (Exception ex) { HandleEx(ex); throw; } }
void CommPortDataReceived(object sender, EventArgs e) { /* When new data is received, * parse the message line-by-line. */ IPortAdapter port = (IPortAdapter)sender; StringBuilder buffer = new StringBuilder(); try { idleTimer.Stop(); #if DEBUG System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); // This stopwatch is an attempt at performance optimization. #endif /* There are a few messages that won't end in a NewLine, * so we have to read one character at a time until we run out of them. */ // Read one char at a time until the ReadChar times out. try { buffer.Append(port.ReadChars()); } catch (Exception) { #if DEBUG stopwatch.Stop(); #endif } #if DEBUG ServiceMain.AppendToLog($"Elapsed port read time: {stopwatch.ElapsedMilliseconds}"); #endif ServiceMain.AppendToLog($"In: \t{buffer}"); CommState.RcvInput(buffer.ToString()); idleTimer.Start(); } catch (Exception ex) { ServiceMain.HandleEx(ex); throw; } }