plc重连机制

master
rszn 4 months ago
parent 803cc739ba
commit 45e8818eee

@ -27,8 +27,9 @@ public class FireControlClient: ModbusTcpMaster
WordSeq= EndingConst.WordSeq.CD; WordSeq= EndingConst.WordSeq.CD;
} }
private void BatchRead(ModbusTcpMaster master) private bool BatchRead(ModbusTcpMaster master)
{ {
bool connectResult = false;
//1电表 //1电表
//2消防 //2消防
//3温湿度 //3温湿度
@ -39,9 +40,10 @@ public class FireControlClient: ModbusTcpMaster
var readPowerValue = master.Read("x=3;39424", 2).Content; var readPowerValue = master.Read("x=3;39424", 2).Content;
if (readPowerValue != null) if (readPowerValue != null)
{ {
connectResult = true;
Array.Reverse(readPowerValue); Array.Reverse(readPowerValue);
//ModbusDecoder.Decode<PowerValue>(readPowerValue, FireControlMgr.PowerValue, EndingConst.ByteSeq.BA, EndingConst.WordSeq.DC); //ModbusDecoder.Decode<PowerValue>(readPowerValue, FireControlMgr.PowerValue, EndingConst.ByteSeq.BA, EndingConst.WordSeq.DC);
FireControlMgr.PowerValue.FPowerValue.Value=BitConverter.ToSingle(readPowerValue, 0); FireControlMgr.PowerValue.FPowerValue.Value=BitConverter.ToSingle(readPowerValue, 0);
} }
#endregion #endregion
@ -51,12 +53,13 @@ public class FireControlClient: ModbusTcpMaster
var readTem = master.Read("s=3;x=3;0", 2).Content; var readTem = master.Read("s=3;x=3;0", 2).Content;
if (readTem!=null) if (readTem!=null)
{ {
connectResult = true;
ModbusDecoder.Decode<TemperatureHumidity>(readTem, FireControlMgr.TemperatureHumidity, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<TemperatureHumidity>(readTem, FireControlMgr.TemperatureHumidity, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
} }
#endregion #endregion
var bytes02 = master.ReadCoil("s=2;x=1;0", 17).Content;//0x01 var bytes02 = master.ReadCoil("s=2;x=1;0", 17).Content;//0x01
var bytes03 = master.Read("s=2;x=3;1", 3).Content;//0x03 读1 3 var bytes03 = master.Read("s=2;x=3;1", 3).Content;//0x03 读1 3
var bytes04 = master.Read("s=2;x=3;257", 383).Content;//0x03 读256 383 var bytes04 = master.Read("s=2;x=3;257", 383).Content;//0x03 读256 383
var bytes05 = master.Read("s=2;x=4;1", 115).Content;//0x04 可以 var bytes05 = master.Read("s=2;x=4;1", 115).Content;//0x04 可以
@ -67,18 +70,20 @@ public class FireControlClient: ModbusTcpMaster
var bytesAdd2 = master.Read("s=2;x=4;3048", 1048).Content;//0x04 可以 var bytesAdd2 = master.Read("s=2;x=4;3048", 1048).Content;//0x04 可以
if (bytesAdd1 != null && bytesAdd2 != null) if (bytesAdd1 != null && bytesAdd2 != null)
{ {
connectResult = true;
bytes06.AddRange(bytesAdd1); bytes06.AddRange(bytesAdd1);
bytes06.AddRange(bytesAdd2); bytes06.AddRange(bytesAdd2);
} }
var bytes07 = master.Read("s=2;x=4;4096", 32).Content;//0x04 可以 var bytes07 = master.Read("s=2;x=4;4096", 32).Content;//0x04 可以
var bytes08 = master.Read("s=2;x=5;0", 134).Content;//0x05 写 var bytes08 = master.Read("s=2;x=5;0", 134).Content;//0x05 写
var bytes09 = master.Read("s=2;x=6;256", 64).Content;//0x06 写 var bytes09 = master.Read("s=2;x=6;256", 64).Content;//0x06 写
if (bytes02 != null) if (bytes02 != null)
{ {
Log.Info("fire read bytes02 success"); Log.Info("fire read bytes02 success");
connectResult = true;
FireControlMgr.Subzone2.GiveAnAlarm.Value = bytes02[0]; FireControlMgr.Subzone2.GiveAnAlarm.Value = bytes02[0];
FireControlMgr.Subzone2.NumberOfFailures.Value = bytes02[1]; FireControlMgr.Subzone2.NumberOfFailures.Value = bytes02[1];
FireControlMgr.Subzone2.SoundSuppression.Value = bytes02[2]; FireControlMgr.Subzone2.SoundSuppression.Value = bytes02[2];
@ -97,14 +102,16 @@ public class FireControlClient: ModbusTcpMaster
FireControlMgr.Subzone2.AudibleVisualAlarmStatus.Value = bytes02[15]; FireControlMgr.Subzone2.AudibleVisualAlarmStatus.Value = bytes02[15];
FireControlMgr.Subzone2.DeflatedDoNotEnter.Value = bytes02[16]; FireControlMgr.Subzone2.DeflatedDoNotEnter.Value = bytes02[16];
} }
if (bytes03 != null) if (bytes03 != null)
{ {
connectResult = true;
ModbusDecoder.Decode<Subzone3>(bytes03, FireControlMgr.Subzone3, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone3>(bytes03, FireControlMgr.Subzone3, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
} }
if (bytes04 != null) if (bytes04 != null)
{ {
connectResult = true;
ModbusDecoder.Decode<Subzone4>(bytes04, FireControlMgr.Subzone4, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone4>(bytes04, FireControlMgr.Subzone4, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
List<string> faultList = new List<string>() List<string> faultList = new List<string>()
{ {
@ -141,34 +148,38 @@ public class FireControlClient: ModbusTcpMaster
}; };
SaveAlarmInfo(faultList); SaveAlarmInfo(faultList);
} }
if (bytes05 != null) if (bytes05 != null)
{ {
connectResult = true;
ModbusDecoder.Decode<Subzone5>(bytes05, FireControlMgr.Subzone5, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone5>(bytes05, FireControlMgr.Subzone5, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
} }
if (bytes06 != null) if (bytes06 != null)
{ {
connectResult = true;
ModbusDecoder.Decode<Subzone6>(bytes06.ToArray(), FireControlMgr.Subzone6, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone6>(bytes06.ToArray(), FireControlMgr.Subzone6, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
} }
if (bytes07 != null) if (bytes07 != null)
{ {
connectResult = true;
ModbusDecoder.Decode<Subzone7>(bytes07, FireControlMgr.Subzone7, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone7>(bytes07, FireControlMgr.Subzone7, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
} }
/*if (bytes08 != null) /*if (bytes08 != null)
{ {
ModbusDecoder.Decode<Subzone8>(bytes08, FireControlMgr.Subzone8, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone8>(bytes08, FireControlMgr.Subzone8, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
} }
if (bytes09 != null) if (bytes09 != null)
{ {
ModbusDecoder.Decode<Subzone9>(bytes09, FireControlMgr.Subzone9, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC); ModbusDecoder.Decode<Subzone9>(bytes09, FireControlMgr.Subzone9, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}*/ }*/
return connectResult;
} }
private void SaveAlarmInfo(List<string> lstAlarm) private void SaveAlarmInfo(List<string> lstAlarm)
{ {
if (lstAlarm.Count > 0) if (lstAlarm.Count > 0)
@ -193,7 +204,7 @@ public class FireControlClient: ModbusTcpMaster
{ {
EquipAlarmDefine? alarmDefine = EquipAlarmDefine? alarmDefine =
EquipAlarmDefineRepository.QueryByClause(i=>i.EquipTypeCode==(int)EquipmentType.Fire&&i.ErrorCode==errorCode); EquipAlarmDefineRepository.QueryByClause(i=>i.EquipTypeCode==(int)EquipmentType.Fire&&i.ErrorCode==errorCode);
if (alarmDefine != null) if (alarmDefine != null)
{ {
EquipAlarmRecord record = new EquipAlarmRecord() EquipAlarmRecord record = new EquipAlarmRecord()
@ -207,7 +218,7 @@ public class FireControlClient: ModbusTcpMaster
StartTime = DateTime.Now StartTime = DateTime.Now
}; };
EquipAlarmRecordRepository.Insert(record); EquipAlarmRecordRepository.Insert(record);
} }
} }
} }
} }
@ -267,4 +278,4 @@ public class FireControlClient: ModbusTcpMaster
#endregion #endregion
} }
} }
} }

@ -53,16 +53,18 @@ public class PlcClient : ModbusTcpMaster
WordSeq = EndingConst.WordSeq.CD; WordSeq = EndingConst.WordSeq.CD;
} }
private void BatchRead(ModbusTcpMaster master) private bool BatchRead(ModbusTcpMaster master)
{ {
bool connectResult = false;
try try
{ {
Log.Info("Plc read satrt"); Log.Info("Plc read start");
if (master != null) if (master != null)
{ {
var bytes01 = master.BatchReadHolderRegister(1, 115); var bytes01 = master.BatchReadHolderRegister(1, 115);
if (bytes01 != null) if (bytes01 != null)
{ {
connectResult = true;
PlcMgr.DataValidityTime = DateTime.Now; PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<HostToPlc>(bytes01, PlcMgr.HostToPlcData, EndingConst.ByteSeq.AB, ModbusDecoder.Decode<HostToPlc>(bytes01, PlcMgr.HostToPlcData, EndingConst.ByteSeq.AB,
@ -72,6 +74,7 @@ public class PlcClient : ModbusTcpMaster
var bytes02 = master.BatchReadHolderRegister(201, 222); var bytes02 = master.BatchReadHolderRegister(201, 222);
if (bytes02 != null) if (bytes02 != null)
{ {
connectResult = true;
PlcMgr.DataValidityTime = DateTime.Now; PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<PlcToHost>(bytes02, PlcMgr.PlcToHostData, EndingConst.ByteSeq.AB, ModbusDecoder.Decode<PlcToHost>(bytes02, PlcMgr.PlcToHostData, EndingConst.ByteSeq.AB,
EndingConst.WordSeq.DC); EndingConst.WordSeq.DC);
@ -89,6 +92,7 @@ public class PlcClient : ModbusTcpMaster
var bytes03 = master.BatchReadHolderRegister(701, 32); var bytes03 = master.BatchReadHolderRegister(701, 32);
if (bytes03 != null) if (bytes03 != null)
{ {
connectResult = true;
PlcMgr.LastPlcFaultData = PlcMgr.PlcFaultData; PlcMgr.LastPlcFaultData = PlcMgr.PlcFaultData;
PlcMgr.DataValidityTime = DateTime.Now; PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<PlcFault>(bytes03, PlcMgr.PlcFaultData, EndingConst.ByteSeq.AB, ModbusDecoder.Decode<PlcFault>(bytes03, PlcMgr.PlcFaultData, EndingConst.ByteSeq.AB,
@ -114,16 +118,17 @@ public class PlcClient : ModbusTcpMaster
}; };
SaveAlarmInfo(alarm); SaveAlarmInfo(alarm);
} }
Log.Info("Plc read finish"); Log.Info("Plc read finish");
} }
else if(PlcMgr.PlcClient!=null) else if(PlcMgr.PlcClient!=null)
{ {
Log.Info("Plc use plcClient read"); Log.Info("Plc use plcClient read");
var bytes01 = PlcMgr.PlcClient.BatchReadHolderRegister(1, 115); var bytes01 = PlcMgr.PlcClient.BatchReadHolderRegister(1, 115);
if (bytes01 != null) if (bytes01 != null)
{ {
connectResult = true;
PlcMgr.DataValidityTime = DateTime.Now; PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<HostToPlc>(bytes01, PlcMgr.HostToPlcData, EndingConst.ByteSeq.AB, ModbusDecoder.Decode<HostToPlc>(bytes01, PlcMgr.HostToPlcData, EndingConst.ByteSeq.AB,
@ -133,6 +138,7 @@ public class PlcClient : ModbusTcpMaster
var bytes02 = PlcMgr.PlcClient.BatchReadHolderRegister(201, 222); var bytes02 = PlcMgr.PlcClient.BatchReadHolderRegister(201, 222);
if (bytes02 != null) if (bytes02 != null)
{ {
connectResult = true;
PlcMgr.DataValidityTime = DateTime.Now; PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<PlcToHost>(bytes02, PlcMgr.PlcToHostData, EndingConst.ByteSeq.AB, ModbusDecoder.Decode<PlcToHost>(bytes02, PlcMgr.PlcToHostData, EndingConst.ByteSeq.AB,
EndingConst.WordSeq.DC); EndingConst.WordSeq.DC);
@ -150,6 +156,7 @@ public class PlcClient : ModbusTcpMaster
var bytes03 = PlcMgr.PlcClient.BatchReadHolderRegister(701, 32); var bytes03 = PlcMgr.PlcClient.BatchReadHolderRegister(701, 32);
if (bytes03 != null) if (bytes03 != null)
{ {
connectResult = true;
PlcMgr.LastPlcFaultData = PlcMgr.PlcFaultData; PlcMgr.LastPlcFaultData = PlcMgr.PlcFaultData;
PlcMgr.DataValidityTime = DateTime.Now; PlcMgr.DataValidityTime = DateTime.Now;
ModbusDecoder.Decode<PlcFault>(bytes03, PlcMgr.PlcFaultData, EndingConst.ByteSeq.AB, ModbusDecoder.Decode<PlcFault>(bytes03, PlcMgr.PlcFaultData, EndingConst.ByteSeq.AB,
@ -175,16 +182,18 @@ public class PlcClient : ModbusTcpMaster
}; };
SaveAlarmInfo(alarm); SaveAlarmInfo(alarm);
} }
Log.Info("Plc read finish"); Log.Info("Plc read finish");
} }
} }
catch (Exception e) catch (Exception e)
{ {
Log.Info($"Plc read error:{e}"); Log.Info($"Plc read error:{e}");
throw; throw;
} }
return connectResult;
} }
private void SaveAlarmInfo(List<string> lstAlarm) private void SaveAlarmInfo(List<string> lstAlarm)
@ -359,4 +368,4 @@ public class PlcClient : ModbusTcpMaster
BinInfoRepository.Update(it => it.Exists == 1, BinInfoRepository.Update(it => it.Exists == 1,
it => it.No == binNo); it => it.No == binNo);
} }
} }

Loading…
Cancel
Save