public bool DisConnect(out string msg) { try { if (this.Company == PlcCompany.Mitsubishi.ToString()) { melsec_net.ConnectClose();// 关闭长连接,并切换为短连接,在系统退出时可以调用 IsAlive = false; } else { TcpClient.Close(); TcpClient = null; IsAlive = false; } } catch (Exception ex) { msg = ex.Message; IsAlive = true; return(false); } msg = string.Empty; return(true); }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show("Ip地址输入不正确!"); return; } melsec_net.PLCIpAddress = address; if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show("端口输入格式不正确!"); return; } melsec_net.PortRead = port; melsec_net.ConnectClose( ); try { OperateResult connect = melsec_net.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show("连接成功!"); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; } else { MessageBox.Show("连接失败!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public bool TcpDisConnect(out string msg) { try { if (this.Company == PlcCompany.Mitsubishi.ToString()) { if (melsec_net != null) { melsec_net.ConnectClose();// 关闭长连接,并切换为短连接,在系统退出时可以调用 } IsAlive = false; } else if (this.Company == PlcCompany.Siemens.ToString()) { if (siemensS7Net != null) { siemensS7Net.ConnectClose(); siemensS7Net = null; } IsAlive = false; } else if (this.Company == PlcCompany.OMRON.ToString() && (this.Model == "SYSMAC CP1H" || this.Model == "NX102")) { if (omron_net != null) { omron_net.ConnectClose(); omron_net = null; } IsAlive = false; } else if (this.Company == PlcCompany.OMRON.ToString() && this.Model == "NX1P2") { if (Socket != null) { Socket.Close(); Socket.Dispose(); Socket = null; } if (point != null) { point = null; } IsAlive = false; } else if (this.Company == "MCGS" && this.Model == "TPC1061TI(Hi)") { if (busTcpClient != null) { busTcpClient.ConnectClose(); busTcpClient = null; } IsAlive = false; } else if (Socket != null) { Socket.Close(); Socket.Dispose(); Socket = null; IsAlive = false; } } catch (Exception ex) { msg = ex.Message; IsAlive = true; return(false); } msg = string.Empty; return(true); }