Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新手小白,报错System.ObjectDisposedException:“已关闭 Safe handle” #49

Open
99tian opened this issue Feb 20, 2024 · 0 comments

Comments

@99tian
Copy link

99tian commented Feb 20, 2024

 private void opencomplc()//打开接口
        {
            try
            {
                if (ccbcom1.Text == "")
                {
                    MessageBox.Show("Select Port Settings First");
                }
                else
                {
                    FxSerial.SerialPortInni(spplc =>
                        {
                            serialPortEncoding = Encoding.GetEncoding("iso-8859-1");
                            spplc.Encoding = serialPortEncoding;
                            spplc.RtsEnable = true;
                            spplc.PortName = ccbcom1.Text;
                            spplc.BaudRate = Convert.ToInt32(cbbrate1.Text);
                            spplc.Parity = (Parity)Enum.Parse(typeof(Parity), "2");
                            //spplc.Parity = System.IO.Ports.Parity.Even;
                            spplc.DataBits = 7;
                            spplc.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "1");
                            //spplc.StopBits = System.IO.Ports.StopBits.One;
                            spplc.Handshake = (Handshake)Enum.Parse(typeof(Handshake), "None");
                            spplc.ReadTimeout = 100;
                            spplc.WriteTimeout = 200;
                        });
                    try
                    {
                        OperateResult connect = FxSerial.Open();
                        if (connect.IsSuccess)
                        {
                            // 串口打开成功
                            Console.WriteLine("Open success");
                        }
                        else
                        {
                            // 串口打开失败,输出原因,可能串口不存在,或是已经被其他程序打开了
                            Console.WriteLine("Open failed:" + connect.Message);
                        }
                        //FxSerial.Open();
                        timer.Start();
                        lbck1.Color = Color.Lime;
                        //MessageBox.Show("串口打开!");
                        if (cbAutoCon1.Checked)
                        {
                            Properties.Settings.Default.PlcPort = "0";
                            Properties.Settings.Default.Save();
                        }
                        
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Message ", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
```
      ```
  private void Closecomplc()//关闭接口
        {
                try
                {
                  
                    FxSerial.Close();
                    FxSerial.Dispose();
                    timer.Stop();
                    lbck1.Color = System.Drawing.Color.Gray;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "message");
                }
            
        }
```
 ```
 /// <summary>
        /// 读取和写入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn5xy_Click(object sender, EventArgs e)
        {
            if (FxSerial.IsOpen() == true)
            {
                OperateResult<bool> R_X1 = FxSerial.ReadBool("Y002");
                if (R_X1.IsSuccess)
                {
                    // success
                    bool value = R_X1.Content;
                    if (value == false)
                    {
                        OperateResult write = FxSerial.Write("M2", true);

                        if (write.IsSuccess)
                        {
                            //MessageBox.Show(write.IsSuccess.ToString());
                            OperateResult wrie = FxSerial.Write("M2", false);
                        }
                    }
                    else
                    {
                        MessageBox.Show("未停止增压");
                    }
                }
                else
                {
                    // failed
                    MessageBox.Show("Read failed: " + R_X1.ToMessageShowString());
                }
            }
        }

```
点击打开串口再关闭,再打开串口,

读取或者写入就报错System.ObjectDisposedException:“已关闭 Safe handle”




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant