using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using S7.Net; using logtxtWrite; using iWareCommon.Utils; using HslCommunication.Profinet.Siemens; using HslCommunication; using iWareModel; using HslCommunication.Profinet.AllenBradley; using iWareCommon; namespace iWareSda { /// /// 罗克韦尔PLC (Allen-Bradley) /// public class AB_HslCommunicationService : PLCService { private AllenBradleyNet abTcpNet = null; private readonly string _ip; private readonly int _port; public AB_HslCommunicationService(HslCommunicationParam hslcpParam) { _ip = hslcpParam.ip; _port = hslcpParam.port; _LogType = LogType.HslCommunicationService; } /// /// 打开S7连接 /// /// public override MessageModel OpenService() { MessageModel fre = new MessageModel(); try { //创建连接对象 abTcpNet = new AllenBradleyNet(); abTcpNet.IpAddress = _ip; if (_port > 0) abTcpNet.Port = Convert.ToInt32(_port); //不记录日志!!!!否则文件太大导致问题! //abTcpNet.LogNet = new HslCommunication.LogNet.LogNetSingle("AB_HslCommunicationService_logs.txt"); OperateResult operateResult = abTcpNet.ConnectServer(); if (operateResult.IsSuccess) { base.IsConnected = true; fre.result = true; return fre; } base.IsConnected = false; fre.result = false; fre.resMsg = "打开失败," + operateResult.Message; return fre; } catch (Exception ex) { base.IsConnected = false; Log4NetHelper.WriteErrorLog(_LogType, "初始化AB PLC 出现异常", ex); throw ex; } } /// /// 写入 /// /// 地址 /// 值 /// public override MessageModel WriteValuePoint(string dbNumber, string offset, object value, Object proObj) { MessageModel fre = new MessageModel(); OperateResult operateResult = null; Type proObjType = proObj.GetType(); var address = dbNumber + offset; try { if (proObjType == typeof(bool)) {//布尔类型 bool myData = Convert.ToBoolean(value); operateResult = abTcpNet.Write(address, myData); } //浮点 else if (proObjType == typeof(double) || proObjType == typeof(float)) { double MyData = Convert.ToDouble(value); operateResult = abTcpNet.Write(address, MyData); } //整数 else if (proObjType == typeof(Int16) || proObjType == typeof(short)) { short MyData = Convert.ToInt16(value); operateResult = abTcpNet.Write(address, MyData.ConvertToUshort()); } //双整数 else if (proObjType == typeof(Int32)) { int myData = Convert.ToInt32(value); operateResult = abTcpNet.Write(address, myData); } //Char else if (proObjType == typeof(Char)) {//char要转换成int类型,写给PLC int myData = Convert.ToInt32(value); byte b = (byte)myData; operateResult = abTcpNet.Write(address, b); } else { operateResult = abTcpNet.Write(address, value.ToString()); } if (!operateResult.IsSuccess) { Log4NetHelper.WriteErrorLog(_LogType, String.Format("写入出现异常:" + operateResult.Message + ",地址:{0},值:{1}", address, value.ToString()), null); fre.result = false; fre.resMsg = "写入失败:" + operateResult.Message; } else { fre.result = true; Log4NetHelper.WriteInfoLog(_LogType, String.Format("写入成功,写入地址:{0},值:{1}", address, value.ToString())); } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, String.Format("写入出现异常:" + ex.Message + ",地址:{0},值:{1}", address, value.ToString()), ex); fre.result = false; fre.resMsg = "写入失败:" + ex.Message; } return fre; } /// /// 读取 /// /// DB块名 /// 偏移量 /// /// public override object ReadValuePoint(string dbNumber, string offset, Type type = default(Type)) { var address = dbNumber + offset; try { if (type == typeof(bool)) {//布尔要转换成1和0写入 bool MyPlcData = abTcpNet.ReadBool(address).Content; return MyPlcData; } //浮点 else if (type == typeof(double) || type == typeof(float)) { var MyPlcData = abTcpNet.ReadFloat(address).Content; return MyPlcData; } //整数 else if (type == typeof(short)) { short MyPlcData = abTcpNet.ReadInt16(address).Content; return MyPlcData; } //双整数 else if (type == typeof(Int32)) { int MyPlcData = abTcpNet.ReadInt32(address).Content; return MyPlcData; } //Char else if (type == typeof(Char)) {//char要转换成int类型,写给PLC char MyPlcData = (char)(abTcpNet.ReadByte(address).Content); return MyPlcData.ToString(); } else { var MyPlcData = abTcpNet.ReadString(address, 5000).Content.ToString(); return MyPlcData.ToString(); } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex); throw; } } public override void Close() { if (abTcpNet != null) abTcpNet.ConnectClose(); } } }