diff --git a/CleanMachineHMI/Utils/PlcOperationHelper.cs b/CleanMachineHMI/Utils/PlcOperationHelper.cs index 2afe91d..2dede3f 100644 --- a/CleanMachineHMI/Utils/PlcOperationHelper.cs +++ b/CleanMachineHMI/Utils/PlcOperationHelper.cs @@ -1,4 +1,7 @@ -#define USE_LOCAL_TCP_DEBUG //使用本地调式 +//使用本地调式 +// #define USE_LOCAL_TCP_DEBUG +//启用心跳检查 +// #define UseHeartbeat using CleanMachine.Model; using LeadDataBaseUtility; @@ -96,8 +99,8 @@ namespace CleanMachineHMI var adaptet = new NModbus.IO.TcpClientAdapter(port); #else - port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis); - var adaptet = new NModbus.Serial.SerialPortAdapter(port); + port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis); + var adaptet = new NModbus.Serial.SerialPortAdapter(port); #endif var factory = new ModbusFactory(); @@ -111,11 +114,11 @@ namespace CleanMachineHMI master = factory.CreateAsciiMaster(adaptet); } #if USE_LOCAL_TCP_DEBUG - port.Connect("10.0.0.95", 5002); + port.Connect("127.0.0.1", 5002); this._isConn = true; #else - port.Open(); + port.Open(); #endif return true; @@ -131,7 +134,11 @@ namespace CleanMachineHMI } public void ClosePlcConn() { +#if USE_LOCAL_TCP_DEBUG if (port.Connected) +#else + if (port.IsOpen) +#endif { port.Close(); master = null; @@ -866,22 +873,21 @@ namespace CleanMachineHMI 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; - //报警处理。 - frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!"); - if (ReConnNum % 5 == 0) - { - ClosePlcConn(); - PlcInit(); - } - ReConnNum++; + ClosePlcConn(); + PlcInit(); } + ReConnNum++; + } #endif #if !DEBUG