示例#1
0
        void ConnectCOMClick(object sender, EventArgs e)
        {
            Button connect = (Button)sender;

            if (connect.Text == "connect")
            {
                // previously disconnected
                string comPort = string.Empty;
                try {
                    comPort = comboBoxSelectCOM.SelectedItem.ToString();
                } catch {
                    MessageBox.Show(text: "connect SDS011 and select COM port", caption: "COM list error", buttons: MessageBoxButtons.OK);
                }
                if (comPort != string.Empty)
                {
                    bool connectionEstablished = sds011.tryConnectCOM(comPort);
                    if (connectionEstablished)
                    {
                        sds011.startThread();
                        connect.Text      = "disconnect";
                        connect.BackColor = Color.LightGreen;
                    }
                }
                else /* something went wrong, refresh and connect again */ } {
        }