public BllResult Writes(List <EquipmentProp> equipmentProps) { if (equipmentProps == null || equipmentProps.Count() == 0) { return(BllResultFactory.Error($"未传递属性")); } var ips = equipmentProps.Select(t => t.Equipment).Select(t => t.IP).Distinct().ToList(); foreach (var item in ips) { var temp = S7PLCHelpers.FirstOrDefault(t => t.PLCIP == item); //确定属性中对应的PLC是否存在连接 if (!temp.S7Client.Connected) { return(BllResultFactory.Error($"存在IP为{item}的PLC未连接")); } } try { foreach (var item in ips) { var props = equipmentProps.Where(t => t.Equipment.IP == item).ToList(); var client = S7PLCHelpers.FirstOrDefault(t => t.PLCIP == item).S7Client; S7MultiVar s7MultiVar = new S7MultiVar(client); int count = 20; int i = 0; while (true) { var tempProps = props.Skip(i).Take(count); i += count; if (tempProps.Count() == 0) { break; } //添加dataitem foreach (var prop in tempProps) { var result = S7PLCHelper.AddressAnalyze(prop.Address); if (!result.Success) { return(BllResultFactory.Error($"地址解析错误,属性明细Id:{prop.Id},详情:{result.Msg}")); } var tran = result.Data; if (!Enum.TryParse <PLCDataType>(prop.EquipmentTypeTemplate.DataType, out PLCDataType dataType)) { return(BllResultFactory.Error($"未识别的数据类型{prop.EquipmentTypeTemplate.DataType}")); } var temp = S7PLCHelper.TransferStringToBuffer(dataType, prop.Value.PadRight(20, ' ')); if (!temp.Success) { return(BllResultFactory.Error($"转换数据出错:{temp.Msg}")); } var tempBytes = temp.Data; if (s7MultiVar.Add(tran.Area, tran.DataType, tran.DataNumber, tran.DataOffset, tran.DataAmount, ref tempBytes) == false) { return(BllResultFactory.Error("添加地址失败")); } } GetLock(item); int flag = s7MultiVar.Write(); ResetLock(item); if (flag != 0) { return(BllResultFactory.Error($"写入错误:{client.ErrorText(flag)}")); } var tempp = s7MultiVar.Results.ToList().Where(t => t != 0).ToList(); if (tempp.Count > 0) { return(BllResultFactory.Error($"存在写入错误:{client.ErrorText(tempp[0])}")); } s7MultiVar.Clear(); } ; } } catch (Exception ex) { return(BllResultFactory.Error($"写入出现异常:{ex.ToString() }")); } return(BllResultFactory.Sucess()); }
public BllResult Reads(List <EquipmentProp> equipmentProps) { if (equipmentProps == null || equipmentProps.Count() == 0) { return(BllResultFactory.Error($"未传递属性")); } //选取设备类 var ips = equipmentProps.Select(t => t.Equipment).Select(t => t.IP).Distinct().ToList(); //设备类是否对应PLC配置IP foreach (var item in ips) { var temp = S7PLCHelpers.FirstOrDefault(t => t.PLCIP == item); //确定属性中对应的PLC是否存在连接 if (temp.S7Client == null || !temp.S7Client.Connected) { return(BllResultFactory.Error($"存在IP为{item}的PLC未连接")); } } try { foreach (var item in ips) { //缓存 List <byte[]> list = new List <byte[]>(); var props = equipmentProps.Where(t => t.Equipment.IP == item).ToList(); var client = S7PLCHelpers.FirstOrDefault(t => t.PLCIP == item).S7Client; S7MultiVar s7MultiVar = new S7MultiVar(client); int count = 10; int i = 0; while (true) { var tempProps = props.Skip(i).Take(count); i += count; if (tempProps.Count() == 0) { break; } //添加dataitem foreach (var prop in tempProps) { var result = S7PLCHelper.AddressAnalyze(prop.Address); if (!result.Success) { return(BllResultFactory.Error($"地址解析错误,属性明细Id:{prop.Id},详情:{result.Msg}")); } var tran = result.Data; var temp = new byte[256]; if (s7MultiVar.Add(tran.Area, tran.DataType, tran.DataNumber, tran.DataOffset, tran.DataAmount, ref temp) == false) { return(BllResultFactory.Error("添加地址失败")); } list.Add(temp); } GetLock(item); int flag = s7MultiVar.Read(); ResetLock(item); if (flag != 0) { return(BllResultFactory.Error($"读取错误:{client.ErrorText(flag)}")); } var tempp = s7MultiVar.Results.ToList().Where(t => t != 0).ToList(); if (tempp.Count > 0) { return(BllResultFactory.Error($"存在读取错误:{client.ErrorText(tempp[0])}")); } s7MultiVar.Clear(); } ; //转换读取后的值 Console.WriteLine(DateTime.Now); for (int g = 0; g < props.Count; g++) { if (!Enum.TryParse <PLCDataType>(props[g].EquipmentTypeTemplate.DataType, out PLCDataType dataType)) { return(BllResultFactory.Error($"未识别的数据类型{props[g].EquipmentTypeTemplate.DataType}")); } var result = S7PLCHelper.TransferBufferToString(dataType, list[g]); if (result.Success) { props[g].Value = result.Data; Console.WriteLine(props[g].EquipmentTypeTemplateCode + ":" + props[g].Value); } else { return(BllResultFactory.Error($"读取数据出错:{result.Msg}")); } } s7MultiVar.Clear(); Console.WriteLine(""); } } catch (Exception ex) { return(BllResultFactory.Error($"读取出现异常:" + ex.ToString())); } return(BllResultFactory.Sucess()); }