Exemple #1
0
 public void ClearSelf()
 {
     if (ComDevice.IsOpen)
     {
         ComDevice.Close();
     }
 }
Exemple #2
0
        public String ComOpen(bool b)
        {
            String str = b?"打开串口成功":"关闭串口成功";

            try
            {
                if (b)
                {
                    timerAutoClose.Enabled = true;
                    timerCount             = 4;
                    if (!ComDevice.IsOpen)
                    {
                        ComDevice.PortName = cbbComList.Text.ToString();
                        ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.Text.ToString());
                        ComDevice.Parity   = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                        ComDevice.DataBits = Convert.ToInt32(cbbDataBits.Text.ToString());
                        ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.Text.ToString());
                        ComDevice.Open();
                    }
                }
                else
                {
                    if (ComDevice.IsOpen)
                    {
                        ComDevice.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //AddContent("串口打开错误:" + ex.Message + "\r\n");
                str = "错误:" + ex.Message + "\r\n";
            }
            finally
            {
                if (ComDevice.IsOpen == false)
                {
                    btnSend.Enabled = false;
                    btnOpen.Text    = "打开串口";
                    btnOpen.Image   = ZWebCom.Properties.Resources.close;
                    Log.Text        = "串口已关闭";
                }
                else
                {
                    btnSend.Enabled = true;
                    btnOpen.Text    = "关闭串口";
                    btnOpen.Image   = ZWebCom.Properties.Resources.open;
                    // 串口号,波特率,数据位,停止位.校验位
                    Log.Text = "串口已开启:" + cbbComList.Text + "," + cbbBaudRate.Text + "," + cbbDataBits.Text + "," + cbbStopBits.Text + "," + cbbParity.Text;
                }
            }
            return(str);
        }
Exemple #3
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (cbbComList.Items.Count <= 0)
            {
                //MessageBox.Show("没有发现串口,请检查线路!");
                Log.Text = "没有发现串口,请检查线路!";
                return;
            }

            try
            {
                //if (ComDevice.IsOpen == false)
                if (btnOpen.Text == "打开串口")
                {
                    ComDevice.PortName = cbbComList.Text.ToString();
                    ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.Text.ToString());
                    ComDevice.Parity   = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                    ComDevice.DataBits = Convert.ToInt32(cbbDataBits.Text.ToString());
                    ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.Text.ToString());

                    ComDevice.Open();
                }
                else
                {
                    ComDevice.Close();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AddContent("串口打开错误:" + ex.Message + "\r\n");
            }
            finally
            {
                if (ComDevice.IsOpen == false)
                {
                    btnSend.Enabled   = false;
                    btnOpen.Text      = "打开串口";
                    btnOpen.Image     = Properties.Resources.close;
                    Log.Text          = "串口已关闭";
                    timerIcon.Enabled = false;
                    this.Icon         = Properties.Resources.zuart;
                }
                else
                {
                    btnSend.Enabled = true;
                    btnOpen.Text    = "关闭串口";
                    btnOpen.Image   = Properties.Resources.open;
                    // 串口号,波特率,数据位,停止位.校验位
                    Log.Text          = "串口已开启:" + cbbComList.Text + "," + cbbBaudRate.Text + "," + cbbDataBits.Text + "," + cbbStopBits.Text + "," + cbbParity.Text;
                    timerIcon.Enabled = true;
                }
            }
        }
 /// <summary>
 /// 打开串口
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Btn_Open_Click(object sender, EventArgs e)
 {
     if (ComDevice.IsOpen == false)
     {
         try
         {
             string[] portList = SerialPort.GetPortNames();
             if (portList.Length > 0)
             {
                 ComDevice.PortName = portList[0];
             }
             //打开串口
             ComDevice.Open();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
             return;
         }
         Btn_Open.Text      = "关闭串口";
         Lab_Flag.BackColor = Color.Green;   //标志显示绿色,表示串口已成功打开
     }
     else
     {
         try
         {
             ComDevice.Close();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         Btn_Open.Text      = "打开串口";
         Lab_Flag.BackColor = Color.Red;     //标识显示红色,表示串口未打开
     }
 }
        private void btnOpen_Click_1(object sender, EventArgs e)
        {
            if (ComDevice.IsOpen == false)
            {
                ComDevice.PortName = cbbComList.Text;// cbbComList.SelectedItem.ToString();
                ComDevice.BaudRate = Convert.ToInt32(cbbbaut.Text);

                if (ccbEO.Text == "无")
                {
                    ComDevice.Parity = Parity.None; //(Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                }
                else if (ccbEO.Text == "偶")
                {
                    ComDevice.Parity = Parity.Even;
                }
                else if (ccbEO.Text == "奇")
                {
                    ComDevice.Parity = Parity.Odd;
                }
                else if (ccbEO.Text == "0")
                {
                    ComDevice.Parity = Parity.Space;
                }
                else
                {
                    ComDevice.Parity = Parity.Mark;
                }


                ComDevice.DataBits = Convert.ToInt16(ccbDb.Text);//Convert.ToInt32(cbbDataBits.SelectedItem.ToString());

                if (cbbStp.Text == "1")
                {
                    ComDevice.StopBits = StopBits.One;// 1;//(StopBits)Convert.ToInt32(cbbStopBits.SelectedItem.ToString());
                }
                else if (cbbStp.Text == "1.5")
                {
                    ComDevice.StopBits = StopBits.OnePointFive;
                }
                else if (cbbStp.Text == "2")
                {
                    ComDevice.StopBits = StopBits.Two;
                }
                else
                {
                    ComDevice.StopBits = StopBits.None;
                }

                try
                {
                    ComDevice.Open();

                    btnSnd.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                btnOpen.Text = "关闭串口";
                // pictureBox1.BackgroundImage = Properties.Resources.green;
            }
            else
            {
                try
                {
                    ComDevice.Close();
                    btnSnd.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                btnOpen.Text = "打开串口";
                //pictureBox1.BackgroundImage = Properties.Resources.red;
            }
        }