using CompressorXN_Communication.MyPlc; using CompressorXN_Model.ViewModel.Response; using CompressorXN_Service; using System.Collections.Generic; namespace CompressorXN.Untils { public class WriteToPlcHelper { private static readonly ParaPlcPointService _paraPlcPointService = new ParaPlcPointService(); public static (bool isOk, string msg) WriteParaToPlc(string productTypeName) { return (true, "参数写入PLC成功"); List paraPointVMList = _paraPlcPointService.QueryParaPointByProductTypeName(productTypeName); string errorMsg = string.Empty; if (paraPointVMList == null || paraPointVMList.Count <= 0) { return (false, $"未查询到【{productTypeName}】机型的配置参数"); } foreach (var item in paraPointVMList) { if (!string.IsNullOrEmpty(item.TargetAddress) && item.TargetVal.HasValue) { var result = OmronCIPHelper.SingleWriteValToPlc(item.TargetAddress, "float", item.TargetVal.Value); if (!result.isOk) { errorMsg += $"【{item.TargetAddress}】写入失败;"; } } if (!string.IsNullOrEmpty(item.MinAddress) && item.MinVal.HasValue) { var result = OmronCIPHelper.SingleWriteValToPlc(item.MinAddress, "float", item.MinVal.Value); if (!result.isOk) { errorMsg += $"【{item.MinAddress}】写入失败;"; } } if (!string.IsNullOrEmpty(item.MaxAddress) && item.MaxVal.HasValue) { var result = OmronCIPHelper.SingleWriteValToPlc(item.MaxAddress, "float", item.MaxVal.Value); if (!result.isOk) { errorMsg += $"【{item.MaxAddress}】写入失败;"; } } if (!string.IsNullOrEmpty(item.CheckAddress)) { float val = item.IsCheck ? 1 : 0; var result = OmronCIPHelper.SingleWriteValToPlc(item.CheckAddress, "float", val); if (!result.isOk) { errorMsg += $"【{item.CheckAddress}】写入失败;"; } } } if (string.IsNullOrEmpty(errorMsg)) { return (true, "参数写入PLC成功"); } else { return (false, errorMsg); } } } }