void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e) { if (this.InvokeRequired) { // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway. this.BeginInvoke(new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e }); return; } int maxTextLength = 100000; // maximum text length in text box if (tbData.TextLength > maxTextLength) { tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength); } string str = (e.Str + "\r\n"); tbData.AppendText(str); tbData.ScrollToCaret(); /************************************** Verify the boot **************************************/ // If one of these phrases comes through the serial port, then the boot is verified. int versiontime = 0; int foundit = 0; if (str.Contains("version")) { foundit = 1; Regex rx = new Regex("deviceid"); foreach (Match match in rx.Matches(str)) { int i = match.Index; devIDreturned = str.Substring(i + 12, 8); //tbData.AppendText(devIDreturned + "\r\n"); txtDeviceID.Text = devIDreturned; } } if (str.Contains("FMT personalAlarm")) { versiontime = 1; } if (foundit == 1) { if (!basicFlag) { string temp = _testdata.content.ToString(); Regex rx = new Regex(txtDeviceID.Text.ToString()); foreach (Match match in rx.Matches(temp)) { int i = match.Index; // idData.AppendText("\r\n" + i.ToString()); idData.AppendText(temp.Substring(i, 25) + "\r\n"); } File.WriteAllText(cd + "\\firmware\\idData.txt", idData.Text.ToString()); } progressBar1.Value = 1000; txtLoadComplete.Visible = true; _spManager.StopListening(); //clear any exisitng test data initializeForTest(); } if (versiontime == 1) { _spManager.SendString("VERSION"); } /********************** CODE SECTION TO GATHER ARBITRARY TEXT DATA FROM SERIAL STREAM ***************************************/ //get the deviceID Device ID: 002C1E76 if (str.Contains("Device ID:") & (_testdata._testState == 1)) { char[] arr = str.Where(c => (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-')).ToArray(); str = new string(arr); _testdata.DeviceID = str.Substring(str.Length - 9); // txtDeviceID.Text = _testdata.DeviceID.ToString(); } }