You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.8 KiB

using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Const;
using HybirdFrameworkDriver.ModbusTcpMaster;
using Repository.Station;
using Service.Plc.Msg;
namespace Service.Plc.Client;
/// <summary>
///
/// </summary>
[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<HostToPlc>(bytes01, PlcMgr.HostToPlcData, EndingConst.ByteSeq.AB,
EndingConst.WordSeq.DC);
}
var bytes02 = master.BatchReadHolderRegister(201, 222);
if (bytes02 != null)
{
PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<PlcToHost>(bytes02, PlcMgr.PlcToHostData, EndingConst.ByteSeq.AB,
EndingConst.WordSeq.DC);
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec01.Value, "1");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec02.Value, "2");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec03.Value, "3");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec04.Value, "4");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec05.Value, "5");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec06.Value, "6");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec07.Value, "7");
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec08.Value, "8");
}
var bytes03 = master.BatchReadHolderRegister(701, 10);
if (bytes03 != null)
{
PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<PlcFault>(bytes03, PlcMgr.PlcFaultData, EndingConst.ByteSeq.AB,
EndingConst.WordSeq.DC);
}
}
private void UpdateBinInfo(int exists, string binNo)
{
if (exists == 0)
_binInfoRepository.Update(
it =>
new BinInfo(){
Exists = 0,
BatteryNo = "-1",
Soc = (decimal)-1,
Soe = (decimal)-1,
Soh = (decimal)-1,
},
it => it.No == binNo);
else
_binInfoRepository.Update(it => it.Exists == 1,
it => it.No == binNo);
}
}