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,6 +40,7 @@ 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);
@ -51,6 +53,7 @@ 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);
} }
@ -67,6 +70,7 @@ 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);
} }
@ -79,6 +83,7 @@ public class FireControlClient: ModbusTcpMaster
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];
@ -100,11 +105,13 @@ public class FireControlClient: ModbusTcpMaster
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>()
{ {
@ -144,16 +151,19 @@ public class FireControlClient: ModbusTcpMaster
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);
} }
@ -167,6 +177,7 @@ public class FireControlClient: ModbusTcpMaster
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)

@ -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,
@ -124,6 +128,7 @@ public class PlcClient : ModbusTcpMaster
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,
@ -185,6 +192,8 @@ public class PlcClient : ModbusTcpMaster
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)

Loading…
Cancel
Save