dathlin / HslCommunication

A very popular industrial Internet of Things communication plug-in. Using this dll can be very convenient, stable, and fast to obtain data from PLC equipment of multiple brands, and also supports redis, mqtt, websocket, etc., which can let your data on the network Free transmission, reducing enterprise development costs.
http://www.hslcommunication.cn
1.61k stars 622 forks source link

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

Open 99tian opened 8 months ago

99tian commented 8 months ago
 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");
            }

    }

///

/// 读取和写入 /// /// /// private void btn5xy_Click(object sender, EventArgs e) { if (FxSerial.IsOpen() == true) { OperateResult 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”