public bool Connect(out string msg) { try { if (this.Company == PlcCompany.Mitsubishi.ToString()) { //初始化 melsec_net.PLCIpAddress = System.Net.IPAddress.Parse(this.IP); // PLC的IP地址 melsec_net.PortRead = Port; // 端口 melsec_net.PortWrite = 6001; // 写入端口,最好和读取分开 melsec_net.NetworkNumber = 0; // 网络号 melsec_net.NetworkStationNumber = 0; // 网络站号 melsec_net.ConnectTimeout = 500; // 连接超时时间 // 如果需要长连接,就取消下面这行代码的注释,对于数据读写的代码,没有影响 melsec_net.ConnectServer(); // 切换长连接,这行代码可以放在其他任何地方 IsAlive = true; } else if (!TcpClient.Connected) { TcpClient.Connect(IpAddress, Port); IsAlive = true; } } catch (Exception ex) { msg = ex.Message; IsAlive = false; 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 TcpConnect(out string msg) { msg = string.Empty; try { if (this.Company == PlcCompany.Mitsubishi.ToString()) { //初始化 melsec_net.PLCIpAddress = System.Net.IPAddress.Parse(this.IP); // PLC的IP地址 melsec_net.PortRead = Port; // 端口 melsec_net.PortWrite = 6001; // 写入端口,最好和读取分开 melsec_net.NetworkNumber = 0; // 网络号 melsec_net.NetworkStationNumber = 0; // 网络站号 melsec_net.ConnectTimeout = 500; // 连接超时时间 // 如果需要长连接,就取消下面这行代码的注释,对于数据读写的代码,没有影响 melsec_net.ConnectServer(); // 切换长连接,这行代码可以放在其他任何地方 IsAlive = true; } else if (this.Company == PlcCompany.Siemens.ToString()) { siemensS7Net = new HslCommunice533.Profinet.Siemens.SiemensS7Net(HslCommunice533.Profinet.Siemens.SiemensPLCS.S1200); siemensS7Net.IpAddress = this.IP; // PLC的IP地址 //siemens_net.PortRead = Port; // 端口 //siemens_net.PortWrite = 102; // 写入端口,最好和读取分开 //siemens_net.ConnectTimeout = 500; // 连接超时时间 siemensS7Net.ConnectServer(); IsAlive = true; } else if (this.Company == PlcCompany.OMRON.ToString() && (this.Model == "SYSMAC CP1H" || this.Model == "NX102")) { if (omron_net == null) { omron_net = new HslCommunice533.Profinet.Omron.OmronFinsNet(this.IP, this.Port); var ips = this.IP.Split('.'); omron_net.SA1 = TengDa.Net.GetIpLastValue(Net.GetLocalIpByRegex(string.Format("{0}.{1}.{2}.*", ips[0], ips[1], ips[2]))); // PC网络号,PC的IP地址的最后一个数 omron_net.DA1 = TengDa.Net.GetIpLastValue(this.IP); // PLC网络号,PLC的IP地址的最后一个数0 omron_net.DA2 = 0x00; // PLC单元号,通常为0 omron_net.ReceiveTimeOut = 1000; omron_net.ConnectTimeOut = 1000; HslCommunice533.OperateResult result = omron_net.ConnectServer(); if (!result.IsSuccess) { msg = result.Message; IsAlive = false; return(false); } } IsAlive = true; } else if (this.Company == "MCGS" && this.Model == "TPC1061TI(Hi)") { if (busTcpClient == null) { busTcpClient?.ConnectClose(); busTcpClient = new HslCommunice533.ModBus.ModbusTcpNet(this.IP, this.Port, 1); busTcpClient.AddressStartWithZero = true; busTcpClient.IsStringReverse = false; HslCommunice533.OperateResult result = busTcpClient.ConnectServer(); if (!result.IsSuccess) { msg = result.Message; IsAlive = false; return(false); } } IsAlive = true; } else if (this.Company == PlcCompany.OMRON.ToString() && this.Model == "NX1P2") { point = new IPEndPoint(IPAddress.Parse(this.IP), this.Port); IsAlive = true; } else if (!Socket.Connected) { Socket.Connect(IpAddress, Port); IsAlive = true; } } catch (Exception ex) { msg = ex.Message; IsAlive = false; return(false); } return(true); }