// ########################################################################### // // P U B L I C // // ########################################################################### // =========================================================================== /// \brief Constructor // =========================================================================== public FormMain() { // Instantiate the application components pendulumInterface = new PendulumInterface(); pendulum = new Pendulum(pendulumInterface); // Instantiate HMI components ctrlPendulum = new CtrlPendulum(pendulum); ctrlParamDisplay = new CtrlParamDisplay(pendulum); // Designer InitializeComponent( ); // HMI disposal splitMain.Panel2.Controls.Add(ctrlPendulum); splitMain.Panel1.Controls.Add(ctrlParamDisplay); ctrlPendulum.Width = splitMain.Panel2.Width; ctrlPendulum.Height = splitMain.Panel2.Height; ctrlPendulum.Enabled = false; ctrlParamDisplay.Dock = DockStyle.Top; // Linking ctrlLog.StartLoggingEvent += EvLog_StartLogging; ctrlLog.StopLoggingEvent += EvLog_StopLogging; pendulumInterface.ConnectionChangedEvent += EvInterface_ConnectionChanged; ctrlScopes.AttachPendulum(pendulum); UpdateMenuState( ); }
// =========================================================================== /// \brief Constructor // =========================================================================== public Pendulum(PendulumInterface pendulumInterface) { // Connect to pendulum interface this.pendulumInterface = pendulumInterface; pendulumInterface.TelemetriesEvent += EvInterface_TelemetriesEvent; pendulumInterface.ConnectionChangedEvent += EvInterface_ConnectionChanged; // Timer to refresh data from the pendulum timer = new Timer(); timer.Interval = 1000; timer.Tick += EvTimer_Tick; timer.Start( ); }
// ########################################################################### // // P R I V A TE // // ########################################################################### #region PRIVATE // =========================================================================== // <EvButton_Connect> /// /// \brief Event: Button "Connect" has been pressed // =========================================================================== private void EvButton_Connect(object sender, EventArgs e) { if (cbPorts.Text == null || cbPorts.Text == "") { return; } cbPorts.Enabled = false; bttConnect.Enabled = false; lblError.Text = ""; // Open port with the required configuration port = new SerialPort(cbPorts.Text, 115200, Parity.Even, 8, StopBits.One); try { port.Open( ); } catch (UnauthorizedAccessException) { lblError.Text = "Unauthorize acess to this port"; } catch (ArgumentException) { lblError.Text = "Invalid port name"; } catch (InvalidOperationException) { lblError.Text = "This port is already opened"; } catch (System.IO.IOException) { lblError.Text = "This port doesn't exist"; } // Check there is someone responding if (port.IsOpen && PendulumInterface.TestConnection(port) == false) { lblError.Text = "Remote device isn't responding"; port.Close( ); } // Connection failed if (port.IsOpen == false) { cbPorts.Enabled = true; bttConnect.Enabled = true; } // Success else { lblError.ForeColor = Color.Green; lblError.Text = "Connection successful"; } t.Start( ); }