Ejemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (serial == null)
            {
                int databit = int.Parse(textBoxDatabit.Text);
                serial = new MySerialPort(this.comboBoxComs.SelectedItem.ToString(), (int)comboBoxBaud.SelectedItem,
                                          (Parity)comboBoxParity.SelectedItem, databit, (StopBits)comboBoxStopBits.SelectedItem);
                serial.SerialDataReceivedCompleteEventHandler += serial_SerialDataReceivedCompleteEventHandler;
                serial.DtrEnable = true;
                serial.RtsEnable = true;
                list.Add(serial);
            }
            if (serial.IsOpen)
            {
                serial.Close();
                button2.Text = "连接";
            }
            else
            {
                serial.Open();
                button2.Text = "关闭";
            }

            comboBoxComs_SelectedIndexChanged(null, null);
        }
Ejemplo n.º 2
0
        private void MySerialPort_PinChanged(object sender, SerialPinChangedEventArgs e)
        {
            switch (e.EventType)
            {
            case SerialPinChange.Break:
                break;

            case SerialPinChange.CDChanged:
            case SerialPinChange.CtsChanged:
            case SerialPinChange.DsrChanged:
                State = (this.CDHolding && this.CtsHolding) ? SerialConnectionState.Opened : SerialConnectionState.NoTXD;
                break;

            case SerialPinChange.Ring:
                break;
            }

            MySerialPort a = this;
        }
Ejemplo n.º 3
0
        void serial_SerialDataReceivedCompleteEventHandler(object arg1, SerialDataReceivedCompleteEventArgs arg2)
        {
            StringBuilder str  = new StringBuilder();
            MySerialPort  prot = (MySerialPort)arg1;

            if (radioButton1.Checked)
            {
                arg2.Data.Any <byte>(by => { str.Append(by.ToString("X2") + " "); return(false); });
            }
            else if (radioButton2.Checked)
            {
                arg2.Data.Any <byte>(by => { str.Append(by.ToString() + " "); return(false); });
            }
            else
            {
                str = new StringBuilder(encodingInfo.GetEncoding().GetString(arg2.Data));
            }

            this.Invoke(new Action(() => this.textBox1.Text = prot.PortName + "接收到数据--------->\r\n" + str.ToString() + "\r\n"));
        }
Ejemplo n.º 4
0
        private void comboBoxComs_SelectedIndexChanged(object sender, EventArgs e)
        {
            serial = null;
            if (comboBoxComs.SelectedItem == null)
            {
                return;
            }
            string comname = comboBoxComs.SelectedItem.ToString();

            serial = list.FirstOrDefault(o => o.PortName == comname);
            if (serial == null || !serial.IsOpen)
            {
                label6.ForeColor  = Color.Red;
                this.button2.Text = "连接";
            }
            else
            {
                this.button2.Text = "关闭";
                label6.ForeColor  = Color.Green;
            }
        }