增加是否开启心跳检查

This commit is contained in:
gongxh
2022-01-19 14:59:16 +08:00
parent f802acfd18
commit 7ab0346321

View File

@@ -1,4 +1,7 @@
#define USE_LOCAL_TCP_DEBUG //使用本地调式 //使用本地调式
// #define USE_LOCAL_TCP_DEBUG
//启用心跳检查
// #define UseHeartbeat
using CleanMachine.Model; using CleanMachine.Model;
using LeadDataBaseUtility; using LeadDataBaseUtility;
@@ -96,8 +99,8 @@ namespace CleanMachineHMI
var adaptet = new NModbus.IO.TcpClientAdapter(port); var adaptet = new NModbus.IO.TcpClientAdapter(port);
#else #else
port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis); port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
var adaptet = new NModbus.Serial.SerialPortAdapter(port); var adaptet = new NModbus.Serial.SerialPortAdapter(port);
#endif #endif
var factory = new ModbusFactory(); var factory = new ModbusFactory();
@@ -111,11 +114,11 @@ namespace CleanMachineHMI
master = factory.CreateAsciiMaster(adaptet); master = factory.CreateAsciiMaster(adaptet);
} }
#if USE_LOCAL_TCP_DEBUG #if USE_LOCAL_TCP_DEBUG
port.Connect("10.0.0.95", 5002); port.Connect("127.0.0.1", 5002);
this._isConn = true; this._isConn = true;
#else #else
port.Open(); port.Open();
#endif #endif
return true; return true;
@@ -131,7 +134,11 @@ namespace CleanMachineHMI
} }
public void ClosePlcConn() public void ClosePlcConn()
{ {
#if USE_LOCAL_TCP_DEBUG
if (port.Connected) if (port.Connected)
#else
if (port.IsOpen)
#endif
{ {
port.Close(); port.Close();
master = null; master = null;
@@ -866,22 +873,21 @@ namespace CleanMachineHMI
WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartDataNew.ToString(), PlcItemTypeDesc.D); WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartDataNew.ToString(), PlcItemTypeDesc.D);
} }
#if USE_LOCAL_TCP_DEBUG #if UseHeartbeat
#else
if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时 if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时
{
isConn = false;
//报警处理。
frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
if (ReConnNum % 5 == 0)
{ {
isConn = false; ClosePlcConn();
//报警处理。 PlcInit();
frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
if (ReConnNum % 5 == 0)
{
ClosePlcConn();
PlcInit();
}
ReConnNum++;
} }
ReConnNum++;
}
#endif #endif
#if !DEBUG #if !DEBUG