Beispiel #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (button1.Text == "接收图像")
     {
         Timer_Image.Start();
         LastReceiveCount = ReceiveDataCount;
         button1.Text     = "不接收图像";
     }
     else if (button1.Text == "不接收图像")
     {
         button1.Text = "接收图像";
         Timer_Image.Stop();
     }
 }
Beispiel #2
0
        private void Button_Open_Click(object sender, EventArgs e)
        {
            if (Button_Open.Text == "打开串口")
            {
                Port.PortName = ComboBox_COM.Text.Trim();
                Port.BaudRate = Convert.ToInt32(ComboBox_Band.Text.Trim());
                Port.DataBits = Convert.ToInt32(ComboBox_Data.Text.Trim());
                Port.Parity   = Parity.None;
                if (ComboBox_Verify.Text.Trim() == "无校验")
                {
                    Port.Parity = Parity.None;
                }
                else if (ComboBox_Verify.Text.Trim() == "偶校验")
                {
                    Port.Parity = Parity.Even;
                }
                else
                {
                    Port.Parity = Parity.Odd;
                }
                Port.StopBits = StopBits.One;
                if (ComboBox_Stop.Text.Trim() == "1")
                {
                    Port.StopBits = StopBits.One;
                }
                else if (ComboBox_Stop.Text.Trim() == "1.5")
                {
                    Port.StopBits = StopBits.OnePointFive;
                }
                else if (ComboBox_Stop.Text.Trim() == "2")
                {
                    Port.StopBits = StopBits.Two;
                }

                if (Port.IsOpen)
                {
                    Port.Close();
                }
                try
                {
                    Port.Open();
                }
                catch
                {
                    MessageBox.Show("端口打开失败");
                }
                if (Port.IsOpen)
                {
                    label5.Text      = "打开成功";
                    label5.ForeColor = Color.Red;
                    Button_Open.Text = "关闭串口";
                    PortEnable(false);
                }
                else
                {
                    label5.Text      = "打开失败";
                    label5.ForeColor = Color.Blue;
                }
            }
            else
            {
                Port.Close();
                Button_Open.Text = "打开串口";
                label5.Text      = "关闭成功";
                PortEnable(true);
                listRecvRecordData.Clear();
                Timer_Image.Stop();
            }
        }