【问题标题】:Serial Communication Port will get Closed after clicking on Close button in C#单击 C# 中的关闭按钮后,串行通信端口将关闭
【发布时间】:2018-06-05 07:29:33
【问题描述】:

我正在开发 C# windows 窗体应用程序中的项目。

在这个项目中,主窗体包含串口通信的子窗体。子窗体“连接”有两个按钮连接和关闭。还有 5 个组合框用于选择波特率、通信名称、奇偶校验、停止位和数据位。

当我从组合框中选择所有设置后单击连接按钮时。端口连接,连接按钮变为断开连接。我将关闭连接表单

现在我的问题是,当我重新打开表单时,当我关闭表单时没有单击“断开连接”按钮,Comport 将断开连接。我不希望 ComPort 关闭。

请帮我解决这个错误。我不知道我在哪里做错了。提前致谢。

连接类代码

 public partial class Connect : Form
{
    public bool Connect_Status = false;

    public Connect()
    {
        InitializeComponent();
        COM_List();

    }

   private void COM_List()
    {
        for (int i = 0; i < CommPortManager.Instance.GetCommList().Count; i++)
        {
            cb_CommPort.Items.Add(CommPortManager.Instance.GetCommList()[i]);
        }
    }


   private void btn_Connect_Click(object sender, EventArgs e)
   {
       if (btn_Connect.Text == "Connect")
       {
           CommPortManager.Instance.PortName = cb_CommPort.Text;
           CommPortManager.Instance.BaudRate = cb_BaudRate.Text;
           CommPortManager.Instance.Parity = cb_Parity.Text;
           CommPortManager.Instance.StopBits = cb_StopBits.Text;
           CommPortManager.Instance.DataBits = cb_DataBits.Text;

           if ((cb_CommPort.Text == "") || (cb_BaudRate.Text == "") || (cb_Parity.Text == "") || (cb_DataBits.Text == "") || (cb_StopBits.Text == ""))
           {
               if (cb_CommPort.Text == "")
               {
                   MessageBox.Show("Please select COM Port and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
               else if (cb_BaudRate.Text == "")
               {
                   MessageBox.Show("Please select BaudRate and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
               else if (cb_Parity.Text == "")
               {
                   MessageBox.Show("Please select Parity and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
               else if (cb_DataBits.Text == "")
               {
                   MessageBox.Show("Please select DataBits and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
               else if(cb_StopBits.Text == "")
               {
                   MessageBox.Show("Please select StopBits and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }

               Connect_Status = false;
           }
           else
           {
               if (CommPortManager.Instance.COM_Open() == false)
               {
                   MessageBox.Show("Could not open the COM port. Most likely it is already in use, has been removed, or is unavailable.", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   Connect_Status = false;
               }
               else
               {
                   //CommPortManager.Instance.COM_Close();
                   Connect_Status = true;
                   btn_Connect.Text = "Disconnect";
                   cb_CommPort.Enabled = false;
                   cb_BaudRate.Enabled = false;
                   cb_DataBits.Enabled = false;
                   cb_Parity.Enabled = false;
                   cb_StopBits.Enabled = false;
                   btn_Connect.BackColor = System.Drawing.Color.Salmon;
               }

           }
       }
       else
       {
           CommPortManager.Instance.COM_Close();
           btn_Connect.Text = "Connect";
           Connect_Status = false;
           cb_CommPort.Enabled = true;
           cb_BaudRate.Enabled = true;
           cb_DataBits.Enabled = true;
           cb_Parity.Enabled = true;
           cb_StopBits.Enabled = true;
           btn_Connect.BackColor = System.Drawing.Color.DarkTurquoise;
       }
   }

   private void btn_Close_Click(object sender, EventArgs e)
   {
       this.Close();
   }

 private void Connect_Load(object sender, EventArgs e)
 {
      //code here to setup the value;
     cb_CommPort.Text = CommPortManager.Instance.PortName;
     cb_BaudRate.Text = CommPortManager.Instance.BaudRate;
     cb_Parity.Text = CommPortManager.Instance.Parity;
     cb_StopBits.Text = CommPortManager.Instance.StopBits;
     cb_DataBits.Text = CommPortManager.Instance.DataBits;

     if (CommPortManager.Instance.IsOpen == true)
     {
         btn_Connect.Text = "Disconnect";
         btn_Connect.BackColor = System.Drawing.Color.Salmon;
         cb_CommPort.Enabled = false;
         cb_BaudRate.Enabled = false;
         cb_DataBits.Enabled = false;
         cb_Parity.Enabled = false;
         cb_StopBits.Enabled = false;
     }
     else
     {
         btn_Connect.Text = "Connect";
         Connect_Status = false;
         cb_CommPort.Enabled = true;
         cb_BaudRate.Enabled = true;
         cb_DataBits.Enabled = true;
         cb_Parity.Enabled = true;
         cb_StopBits.Enabled = true;
         btn_Connect.BackColor = System.Drawing.Color.DarkTurquoise;

     }
 }


}

【问题讨论】:

    标签: c# winforms button serial-port


    【解决方案1】:

    我怀疑是表单加载事件。您需要在打开时将连接状态设置为 true

        private void Connect_Load(object sender, EventArgs e)
        {
            //code here to setup the value;
            cb_CommPort.Text = CommPortManager.Instance.PortName;
            cb_BaudRate.Text = CommPortManager.Instance.BaudRate;
            cb_Parity.Text = CommPortManager.Instance.Parity;
            cb_StopBits.Text = CommPortManager.Instance.StopBits;
            cb_DataBits.Text = CommPortManager.Instance.DataBits;
    
            if (CommPortManager.Instance.IsOpen == true)
            {
                Connect_Status = true;
                btn_Connect.Text = "Disconnect";
                btn_Connect.BackColor = System.Drawing.Color.Salmon;
                cb_CommPort.Enabled = false;
                cb_BaudRate.Enabled = false;
                cb_DataBits.Enabled = false;
                cb_Parity.Enabled = false;
                cb_StopBits.Enabled = false;
            }
            else
            {
                btn_Connect.Text = "Connect";
                Connect_Status = false;
                cb_CommPort.Enabled = true;
                cb_BaudRate.Enabled = true;
                cb_DataBits.Enabled = true;
                cb_Parity.Enabled = true;
                cb_StopBits.Enabled = true;
                btn_Connect.BackColor = System.Drawing.Color.DarkTurquoise;
    
            }
        }
    

    【讨论】:

      【解决方案2】:

      看起来,如果您按 Connect,它会将您的 btn_Connect.Text 更改为 断开连接(如果端口打开) 现在按钮文本是“断开连接”,if (btn_Connect.Text == "Connect") 现在是 false 并且 else 被称为 CommPortManager.Instance.COM_Close();

      【讨论】:

      • 如果我取消注释这个 //CommPortManager.Instance.COM_Close();仍然没有得到结果。
      猜你喜欢
      • 2020-06-29
      • 1970-01-01
      • 2011-07-03
      • 2012-09-26
      • 1970-01-01
      • 2012-09-23
      • 2014-07-21
      • 2018-12-31
      • 1970-01-01
      相关资源
      最近更新 更多