using System; using System.Net; using System.Security.Cryptography; using HslCommunication; using HslCommunication.Profinet.Siemens; using iTextSharp.text.pdf; using iWare_SCADA_BusinessLogical.Utils; using iWare_SCADA_Model; namespace iWare_SCADA_BusinessLogical { /// /// 西么子PLC /// public class Siemens_HslCommunicationService : PLCService { private SiemensS7Net siemensTcpNet = null; private readonly SiemensPLCS _cpu; private readonly string _ip; private readonly int _port; public Siemens_HslCommunicationService(HslCommunicationParam hslcpParam) { _cpu = hslcpParam.cpu; _ip = hslcpParam.ip; _port = hslcpParam.port; _LogType = LogType.HslCommunicationService; } /// /// 打开连接 /// /// public override MessageModel OpenService() { MessageModel fre = new MessageModel(); try { //创建连接对象 siemensTcpNet = new SiemensS7Net(_cpu, _ip); siemensTcpNet.IpAddress = _ip; siemensTcpNet.Port = Convert.ToInt16(_port); //siemensTcpNet.Rack = Convert.ToInt16(tb_Rack.Text); //siemensTcpNet //不记录日志!!!!否则文件太大导致问题! //siemensTcpNet.LogNet = new HslCommunication.LogNet.LogNetSingle("Siemens_HslCommunicationService_logs.txt"); OperateResult operateResult = siemensTcpNet.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, "初始化西门子PLC出现异常", ex); throw ex; } } public override MessageModel WriteValuePointNew(string dbNumber, string offset, object value) {//此方法不再使用西门子PLC if (dbNumber.IndexOf(SystemValue.PLCDBADDRESS_SEPARATE.ToString()) > -1) { throw new Exception("参数不正确,不应该带" + SystemValue.PLCDBADDRESS_SEPARATE.ToString()); } MessageModel fre = new MessageModel(); OperateResult operateResult = null; //Type proObjType = proObj.GetType(); var address = GetAddress(dbNumber, offset, value.GetType()); try { if (value is bool) { bool myData = Convert.ToBoolean(value); operateResult = siemensTcpNet.Write(address, myData); } else if (value is int) { operateResult = siemensTcpNet.Write(address, Int16.Parse(value.ToString())); } else if (value is short) { operateResult = siemensTcpNet.Write(address, (short)value); } else if (value is string) { operateResult = siemensTcpNet.Write(address, (string)value); } else if (value is char) { operateResult = siemensTcpNet.Write(address, (char)value); } else if (value is double|| value is float) { operateResult = siemensTcpNet.Write(address, (double)value); } else { operateResult = siemensTcpNet.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; } /// /// 写入 /// /// 地址 /// 值 /// public override MessageModel WriteValuePoint(string dbNumber, string offset, object value, Object proObj) {//此方法不再使用西门子PLC if (dbNumber.IndexOf(SystemValue.PLCDBADDRESS_SEPARATE.ToString()) > -1) { throw new Exception("参数不正确,不应该带" + SystemValue.PLCDBADDRESS_SEPARATE.ToString()); } MessageModel fre = new MessageModel(); OperateResult operateResult = null; Type proObjType = proObj.GetType(); var address = GetAddress(dbNumber, offset, value.GetType()); try { if (proObjType == typeof(Boolean)) {//布尔类型 bool myData = Convert.ToBoolean(value); operateResult = siemensTcpNet.Write(address, myData); } //浮点 else if (proObjType == typeof(double) || proObjType == typeof(float)) { double MyData = Convert.ToDouble(value); operateResult = siemensTcpNet.Write(address, MyData); } //整数 else if (proObjType == typeof(Int16) || proObjType == typeof(short)) { short MyData = Convert.ToInt16(value); operateResult = siemensTcpNet.Write(address, MyData);//此处需要注意,短整型的此处不确定是否会报错,需要测试 //operateResult = siemensTcpNet.Write(address, MyData.ConvertToUshort()); } //双整数 else if (proObjType == typeof(Int32)) { int myData = Convert.ToInt32(value); operateResult = siemensTcpNet.Write(address, myData); } //Char else if (proObjType == typeof(Char)) {//char要转换成int类型,写给PLC int myData = Convert.ToInt32(value); byte b = (byte)myData; operateResult = siemensTcpNet.Write(address, b); } //String else if (proObjType == typeof(String)) { operateResult = siemensTcpNet.Write(address, value.ToString()); } else { operateResult = siemensTcpNet.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; } public override MessageModel WriteValuePoint(string fullAddress, object value, object proObj) { var arr = fullAddress.Split(SystemValue.PLCDBADDRESS_SEPARATE); string dbNumber = arr[0]; string offset = arr[1]; return WriteValuePoint(dbNumber, offset, value, proObj); } /// /// 读取 /// /// DB块名 /// 偏移量 /// /// public override object ReadValuePoint(string dbNumber, string offset, int stringlen, Type type = default(Type)) { if (dbNumber.IndexOf(SystemValue.PLCDBADDRESS_SEPARATE.ToString()) > -1) { throw new Exception("参数不正确,不应该带" + SystemValue.PLCDBADDRESS_SEPARATE.ToString()); } var address = GetAddress(dbNumber, offset, type); try { if (type == typeof(bool)) {//布尔要转换成1和0写入 bool MyPlcData = siemensTcpNet.ReadBool(address).Content; return MyPlcData; } //浮点 else if (type == typeof(double) || type == typeof(float)) { var MyPlcData = siemensTcpNet.ReadFloat(address).Content; return MyPlcData; } //整数 else if (type == typeof(short)) { int MyPlcData = siemensTcpNet.ReadInt16(address).Content; return MyPlcData; } //双整数 else if (type == typeof(Int32)) { long MyPlcData = siemensTcpNet.ReadInt32(address).Content; return MyPlcData; } //Char else if (type == typeof(Char)) {//char要转换成int类型,写给PLC char MyPlcData = (char)(siemensTcpNet.ReadByte(address).Content); return MyPlcData.ToString(); } //String else if (type == typeof(String)) { var data = siemensTcpNet.ReadString(address, (ushort)stringlen); var MyPlcData = siemensTcpNet.ReadString(address, (ushort)stringlen).Content; //write.Message = siemensTcpNet.ReadString(write.Name).Message; //string MyPlcData = siemensTcpNet.ReadString(address).Content; return MyPlcData == null ? "" : MyPlcData.ToString(); } else { var MyPlcData = siemensTcpNet.ReadString(address).Content; return MyPlcData == null ? "" : MyPlcData.ToString(); } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex); throw; } } /// /// 读取 /// /// DB块名 /// 偏移量 /// /// public override object ReadValuePoint(string dbNumber, string offset, Type type = default(Type)) { if (dbNumber.IndexOf(SystemValue.PLCDBADDRESS_SEPARATE.ToString()) > -1) { throw new Exception("参数不正确,不应该带" + SystemValue.PLCDBADDRESS_SEPARATE.ToString()); } var address = GetAddress(dbNumber, offset, type); try { if (type == typeof(bool)) {//布尔要转换成1和0写入 var MyPlcData11 = siemensTcpNet.ReadBool(address); bool MyPlcData = siemensTcpNet.ReadBool(address).Content; return MyPlcData; } //浮点 else if (type == typeof(double) || type == typeof(float)) { var MyPlcData = siemensTcpNet.ReadFloat(address).Content; return MyPlcData; } //整数 else if (type == typeof(short)) { int MyPlcData = siemensTcpNet.ReadInt16(address).Content; return MyPlcData; } //双整数 else if (type == typeof(Int32)) { long MyPlcData = siemensTcpNet.ReadInt32(address).Content; return MyPlcData; } //Char else if (type == typeof(Char)) {//char要转换成int类型,写给PLC char MyPlcData = (char)(siemensTcpNet.ReadByte(address).Content); return MyPlcData.ToString(); } //String else if (type == typeof(String)) { string MyPlcData = siemensTcpNet.ReadString(address).Content; return MyPlcData == null ? "" : MyPlcData.ToString(); } else { var MyPlcData = siemensTcpNet.ReadString(address).Content; return MyPlcData == null ? "" : MyPlcData.ToString(); } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex); throw; } } public override object ReadValuePointV2(string fullAddress, Type type = default(Type), int stringlen=0) { if (fullAddress.IndexOf(SystemValue.PLCDBADDRESS_SEPARATE.ToString()) > -1) { throw new Exception("参数不正确,不应该带" + SystemValue.PLCDBADDRESS_SEPARATE.ToString()); } var address = fullAddress; try { if (type == typeof(bool)) {//布尔要转换成1和0写入 bool MyPlcData = siemensTcpNet.ReadBool(address).Content; return MyPlcData; } //浮点 else if (type == typeof(double) || type == typeof(float)) { var MyPlcData = siemensTcpNet.ReadFloat(address).Content; return MyPlcData; } //整数 else if (type == typeof(short)) { int MyPlcData = siemensTcpNet.ReadInt16(address).Content; return MyPlcData; } //双整数 else if (type == typeof(Int32)) { long MyPlcData = siemensTcpNet.ReadInt32(address).Content; return MyPlcData; } //Char else if (type == typeof(Char)) {//char要转换成int类型,写给PLC char MyPlcData = (char)(siemensTcpNet.ReadByte(address).Content); return MyPlcData.ToString(); } //String else if (type == typeof(String)) { var data = siemensTcpNet.ReadString(address, (ushort)stringlen); var MyPlcData = siemensTcpNet.ReadString(address, (ushort)stringlen).Content; //write.Message = siemensTcpNet.ReadString(write.Name).Message; //string MyPlcData = siemensTcpNet.ReadString(address).Content; return MyPlcData == null ? "" : MyPlcData.ToString(); } else { var MyPlcData = siemensTcpNet.ReadString(address).Content; return MyPlcData == null ? "" : MyPlcData.ToString(); } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex); throw; } } public override object ReadValuePoint(string fullAddress, Type type = default(Type)) { var arr = fullAddress.Split(SystemValue.PLCDBADDRESS_SEPARATE); string dbNumber = arr[0]; string offset = arr[1]; return ReadValuePoint(dbNumber, offset, type); } public override string GetAddress(string dbNumber, string offset, Type type = default(Type)) { return "DB" + dbNumber + "." + offset; } public override void Close() { if (siemensTcpNet != null) siemensTcpNet.ConnectClose(); } } }