Exemplo n.º 1
0
 private void OnCloseClicked(object sender, EventArgs e)
 {
     if (Transmitter.IsOpen)
     {
         Transmitter.Close();
     }
 }
Exemplo n.º 2
0
 //发送机部分*************************************************************************************************************
 #region
 private void button10_Click(object sender, EventArgs e)//打开串口Transmitter
 {
     if (!Transmitter.IsOpen)
     {
         Transmitter.PortName = comboBox1.Text;                             //设置串口名
         Transmitter.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
         Transmitter.Encoding = System.Text.Encoding.GetEncoding("GB2312"); //此行非常重要 可解决接收中文乱码问题
         try
         {
             if (comboBox4.Text == comboBox1.Text)
             {
                 Transmitter       = Receiver;
                 same_serial_flag  = true;
                 button17.Text     = "关闭串口";
                 comboBox4.Enabled = false;                                                            //关闭使能
                 comboBox3.Enabled = false;                                                            //关闭使能
             }
             Transmitter.Open();                                                                       //打开串口
             button10.Text             = "关闭串口";
             comboBox1.Enabled         = false;                                                        //关闭使能
             comboBox2.Enabled         = false;                                                        //关闭使能
             Transmitter.DataReceived += new SerialDataReceivedEventHandler(Transmitter_DataReceived); //串口接收处理函数
         }
         catch
         {
             MessageBox.Show("Transmitter串口打开失败!");
         }
     }
     else
     {
         try
         {
             if (Ack_thread != null)
             {
                 Ack_thread.Abort();   //无视之前的数据,关闭线程以停止重发
             }
             Transmitter.Close();      //关闭串口
             button10.Text     = "打开串口";
             comboBox1.Enabled = true; //打开使能
             comboBox2.Enabled = true; //打开使能
             if (same_serial_flag == true)
             {
                 button17.Text     = "打开串口";
                 comboBox4.Enabled = true; //打开使能
                 comboBox3.Enabled = true; //打开使能
             }
         }
         catch
         {
             MessageBox.Show("Transmitter串口关闭失败!");
         }
     }
 }