public override bool LoadFromConfig(XmlNode node) { var xmlElement = (XmlElement)node; string _IPAddress = xmlElement.GetAttribute("IPAddress"); PLC = new AllenBradleyNetNew { IpAddress = _IPAddress, }; if (xmlElement.HasAttribute("Slot")) { PLC.Slot = byte.TryParse(xmlElement.GetAttribute("Slot"), out byte slot) ? slot : (byte)0; } if (xmlElement.HasAttribute("Port")) { PLC.Port = int.TryParse(xmlElement.GetAttribute("Port"), out int port) ? port : 44818; } else { PLC.Port = 44818; } return(base.LoadFromConfig(xmlElement)); }
public bool Connect() { Connected = false; try { allenBradleyNet = new AllenBradleyNetNew(IP); // 连接 if (!System.Net.IPAddress.TryParse(IP, out System.Net.IPAddress address)) { LOG.Error("Ip地址输入不正确!"); } if (!int.TryParse(Port, out int port)) { LOG.Error("端口号输入不正确!"); } if (!byte.TryParse(Slot, out byte slot)) { LOG.Error("slot输入不正确!"); } allenBradleyNet.IpAddress = IP; allenBradleyNet.Port = port; allenBradleyNet.Slot = slot; OperateResult connect = allenBradleyNet.ConnectServer(); if (connect.IsSuccess) { LOG.Info("连接成功!"); Connected = true; } else { LOG.Error("连接失败!" + connect.ToMessageShowString()); } } catch (Exception ex) { LOG.Error(ex.Message); } return(Connected); }