예제 #1
0
        private void serialConnectBtn_Click(object sender, EventArgs e)
        {
            if (ConnectionsManager.Inst.IsSomeConnectionInitiated())
            {
                MsgBox.WarningMsg("Multiple Connections Are Not Allowed",
                                  "Some other connection type (TCP or UDP) is already open. Close it and try again");
                return;
            }

            // no port was selected
            if (serialComCmBx.Text == "")
            {
                MsgBox.WarningMsg("Serial Connection Failed", "No port was selected");
                serialConnectionStateLbl.Text      = "Failed";
                serialConnectionStateLbl.BackColor = Color.Tomato;
                return;
            }

            SaveSerialSettings();
            string detailedPort = serialComCmBx.Text;
            string portName     = SerialCom.DetailedToSimplefiedPortName(detailedPort);

            // selected port is not a serial port (doesn't contain 'COM')
            if (portName == null)
            {
                MsgBox.WarningMsg("Serial Connection Failed", "Invalid port");
                serialConnectionStateLbl.Text      = "Failed";
                serialConnectionStateLbl.BackColor = Color.Tomato;
                return;
            }


            int      baud     = int.Parse(serialBaudCmBx.SelectedItem.ToString());
            StopBits stopBits = SerialCom.StringToStopBits(serialStopBitsCmBx.SelectedItem.ToString());
            int      dataBits = (int)serialDataBitsTxt.Value;
            Parity   parity   = SerialCom.StringToParity(serialParityCmBx.SelectedItem.ToString());

            ConnectionsManager.Inst.InitSerial();
            if (ConnectionsManager.Inst.Serial.Connect(portName, baud, parity, dataBits, stopBits))
            {
                serialPortLbl.Text                 = portName;
                serialConnectionStateLbl.Text      = "Connected";
                serialConnectionStateLbl.BackColor = Color.LimeGreen;
                serialIndicatorLbl.BackColor       = Color.LimeGreen;
                serialDisconnectBtn.Enabled        = true;
                serialConnectBtn.Enabled           = false;
                ConnectionsManager.Inst.Serial.Subscribe(OnIncomingBytes);
                // disable pkg parsing on serial
                pkgParseChkBx.Checked = false;
                pkgParseChkBx.Enabled = false;
            }
            else
            {
                serialConnectionStateLbl.Text      = "Failed";
                serialConnectionStateLbl.BackColor = Color.Tomato;
            }
        }