using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.IO.Ports; using System.IO; namespace CPF_Console { public partial class Form1 : Form, IForm1 { #region interface properties public struct PIDStruct { public string SetPoint; public string PlatformVelocity; public string Y1; public string Y2; public string I; public string v; public string u; public string uCPS; } public struct CTDStruct { public string pressure; public string RawPressure; public string salinity; public string temperature; public string LPFPressure; public string LPFVelocity; public string DiffVelocity; public string Diff2Velocity; public string LPFPressureSlope; public string samplePeriod; } public string ReceivedLine { get { return rtbReceivedLine.Text; } set { rtbReceivedLine.Text = value; } } public string OpenSP { get { return buttonOpenSP.Text; } set { buttonOpenSP.Text = value; } } private bool mscMessageScrollToBottom = true; public string mscMessage { get { return buttonOpenSP.Text; } set { if (mscMessageScrollToBottom) tbMSCMessage.AppendText(value); else tbMSCMessage.Text = tbMSCMessage.Text + value; } } public string DateAndTime { get { return tbDateAndTime.Text; } set { tbDateAndTime.Text = value; } } public ComboBox PortNameCmb { get { return cmbPortName; } } public ComboBox BaudRateCmb { get { return cmbBaudRate; } } public ComboBox ParityCmb { get { return cmbParity; } } public ComboBox StopBitsCmb { get { return cmbStopBits; } } public ComboBox DataBitsCmb { get { return cmbDataBits; } } #endregion private BluetoothSP bluetoothSP; private Parser parser; public Form1() { InitializeComponent(); bluetoothSP = new BluetoothSP(this); parser = new Parser(this); } private void Form1_Load(object sender, EventArgs e) { bluetoothSP.init(); } private void buttonOpenSP_Click(object sender, EventArgs e) { bluetoothSP.openPort(); this.Refresh(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { bluetoothSP.closePort(); if (readingFromFile) { fileReadTimer.Stop(); fileReadTimer.Dispose(); engFile.Close(); } //logFile.Close(); } private void btnGo_Click(object sender, EventArgs e) { bluetoothSP.sendGo(); } private void btnRecoveryTrue_Click(object sender, EventArgs e) { bluetoothSP.sendRecoveryTrue(); } private void btnRecoveryFalse_Click(object sender, EventArgs e) { bluetoothSP.sendRecoveryFalse(); } private void btnSendExit_Click(object sender, EventArgs e) { bluetoothSP.sendExit(); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } public void showMessage( string msg ) { MessageBox.Show(msg); } public void updatePID(PIDStruct inData) { tbPIDSP.Text = inData.SetPoint; tbPIDPV.Text = inData.PlatformVelocity; tbPIDY1.Text = inData.Y1; tbPIDY2.Text = inData.Y2; tbPIDI.Text = inData.I; tbPIDv.Text = inData.v; tbPIDu.Text = inData.u; tbPIDuCPS.Text = inData.uCPS; } public void updateCTD(CTDStruct inData) { tbCTDPressure.Text = inData.pressure; tbCTDRawPressure.Text = inData.RawPressure; tbCTDDiffVelocity.Text = inData.DiffVelocity; tbCTDDiff2Velocity.Text = inData.Diff2Velocity; tbCTDLPFPressure.Text = inData.LPFPressure; tbCTDSamplePeriod.Text = inData.samplePeriod; tbCTDLPFVelocity.Text = inData.LPFVelocity; } private StreamReader engFile; public bool readingFromFile = false; private void buttonReadFromFile_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = @"eng files (*.txt)|*.eng"; openFileDialog1.Title = "Select an Engr Log File"; openFileDialog1.InitialDirectory = @"C:\Users\gene\Documents\CPF\CPFData\Data-2019Jan09"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { engFile = new StreamReader(openFileDialog1.OpenFile()); readingFromFile = true; } int interval; if (Int32.TryParse(tbReadFromFileTimerInterval.Text, out interval)) fileReadTimer.Interval = interval; else MessageBox.Show("Error in Timer Interval value: " + tbReadFromFileTimerInterval.Text); fileReadTimer.Enabled = true; fileReadTimer.Tick += fileReadTimer_Tick; } private String lineFromEngFile; void fileReadTimer_Tick(object sender, EventArgs e) { if ((lineFromEngFile = engFile.ReadLine()) == null) { fileReadTimer.Stop(); MessageBox.Show("End of File"); } else parser.parseLine(lineFromEngFile); } private void richTextBox1_TextChanged(object sender, EventArgs e) { } private void buttonSetRFFTimerInterval_Click(object sender, EventArgs e) { int interval; if (Int32.TryParse(tbReadFromFileTimerInterval.Text, out interval)) fileReadTimer.Interval = interval; else MessageBox.Show("Error in Timer Interval value: " + tbReadFromFileTimerInterval.Text); } private void buttonPauseFileRead_Click(object sender, EventArgs e) { fileReadTimer.Stop(); MessageBox.Show("Hit OK to Continue Reading"); fileReadTimer.Start(); } private void tbMSCMessage_TextChanged(object sender, EventArgs e) { mscMessageScrollToBottom = !mscMessageScrollToBottom; } } public interface IForm1 { string ReceivedLine { get; set; } string OpenSP { get; set; } string DateAndTime { get; set; } string mscMessage { get; set; } ComboBox PortNameCmb { get; } ComboBox BaudRateCmb { get; } ComboBox ParityCmb { get; } ComboBox StopBitsCmb { get; } ComboBox DataBitsCmb { get; } void showMessage(string msg); void updatePID(Form1.PIDStruct data); void updateCTD(Form1.CTDStruct data); } }