using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkDriver.ModbusTcpMaster; using Repository.Station; using Service.Execute.Api; using Service.Plc.Msg; namespace Service.Plc.Client; /// /// /// [Scope] public class PlcClient : ModbusTcpMaster { private BinInfoRepository _binInfoRepository; public PlcClient(BinInfoRepository binInfoRepository) { ReadAction = BatchRead; Ip = "172.0.20.66"; Port = 502; Duration = 2000; AutoReConnect = true; _binInfoRepository = binInfoRepository; //DataFormat = HslCommunication.Core.DataFormat.DCBA; } private void BatchRead(ModbusTcpMaster master) { PlcMgr.SendHear(); var bytes01 = master.BatchReadHolderRegister(1, 115); if (bytes01 != null) { PlcMgr.DataValidityTime = DateTime.Now; ModbusDecoder.Decode(bytes01, PlcMgr.HostToPlcData); } var bytes02 = master.BatchReadHolderRegister(201, 222); if (bytes02 != null) { PlcMgr.DataValidityTime = DateTime.Now; ModbusDecoder.Decode(bytes02, PlcMgr.PlcToHostData); if (PlcMgr.PlcToHostData.MaterialDetec01.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec01.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "1"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec01.Value, it => it.No == "1"); if (PlcMgr.PlcToHostData.MaterialDetec02.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec02.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "2"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec02.Value, it => it.No == "2"); if (PlcMgr.PlcToHostData.MaterialDetec03.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec03.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "3"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec03.Value, it => it.No == "3"); if (PlcMgr.PlcToHostData.MaterialDetec04.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec04.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "4"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec04.Value, it => it.No == "4"); if (PlcMgr.PlcToHostData.MaterialDetec05.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec05.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "5"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec05.Value, it => it.No == "5"); if (PlcMgr.PlcToHostData.MaterialDetec06.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec06.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "6"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec06.Value, it => it.No == "6"); if (PlcMgr.PlcToHostData.MaterialDetec07.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec07.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "7"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec07.Value, it => it.No == "7"); if (PlcMgr.PlcToHostData.MaterialDetec08.Value == 1) _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec08.Value && it.BatteryNo == "-1" && it.Soc == (decimal)(-1) && it.Soe == (decimal)(-1) && it.Soh == (decimal)(-1), it => it.No == "8"); else _binInfoRepository.Update(it => it.Exists == PlcMgr.PlcToHostData.MaterialDetec08.Value, it => it.No == "8"); } var bytes03 = master.BatchReadHolderRegister(701, 10); if (bytes03 != null) { PlcMgr.DataValidityTime = DateTime.Now; ModbusDecoder.Decode(bytes03, PlcMgr.PlcFaultData); } } }