1
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using LeadDataBaseUtility;
|
||||
#define USE_LOCAL_TCP_DEBUG //使用本地调式
|
||||
|
||||
using LeadDataBaseUtility;
|
||||
using Modbus.Device;
|
||||
using NX_LogUtility;
|
||||
using System;
|
||||
@@ -60,8 +62,15 @@ namespace CleanMachineHMI
|
||||
set { this._isConn = value; }
|
||||
}
|
||||
private static IModbusMaster master;
|
||||
#if USE_LOCAL_TCP_DEBUG
|
||||
private static System.Net.Sockets.TcpClient port;
|
||||
|
||||
#else
|
||||
private static SerialPort port;
|
||||
#endif
|
||||
/// <summary>
|
||||
/// 初始化PLC
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool PlcInit()
|
||||
{
|
||||
try
|
||||
@@ -72,8 +81,12 @@ namespace CleanMachineHMI
|
||||
StopBits stopbis = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'StopBits'")[0]["PARAMETER_VALUE"]) == 0 ? StopBits.None : (Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'StopBits'")[0]["PARAMETER_VALUE"]) == 1 ? StopBits.One : StopBits.Two);
|
||||
Parity paritymsg = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ParityMsg'")[0]["PARAMETER_VALUE"]) == 0 ? Parity.None : (Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ParityMsg'")[0]["PARAMETER_VALUE"]) == 1 ? Parity.Odd : Parity.Even);
|
||||
int modbusPattern = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ModbusPattern'")[0]["PARAMETER_VALUE"]);
|
||||
// port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
|
||||
#if USE_LOCAL_TCP_DEBUG
|
||||
port = new System.Net.Sockets.TcpClient();
|
||||
#else
|
||||
port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
|
||||
#endif
|
||||
|
||||
if (modbusPattern == 0)
|
||||
{
|
||||
master = ModbusSerialMaster.CreateRtu(port);
|
||||
@@ -82,9 +95,13 @@ namespace CleanMachineHMI
|
||||
{
|
||||
master = ModbusSerialMaster.CreateAscii(port);
|
||||
}
|
||||
#if USE_LOCAL_TCP_DEBUG
|
||||
port.Connect("127.0.0.1", 5002);
|
||||
// port.Open();
|
||||
this._isConn = true;
|
||||
#else
|
||||
port.Open();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -795,18 +812,25 @@ namespace CleanMachineHMI
|
||||
WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartDataNew.ToString(), PlcItemTypeDesc.D);
|
||||
}
|
||||
|
||||
//if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时
|
||||
//{
|
||||
// isConn = false;
|
||||
// //报警处理。
|
||||
// frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
|
||||
// if (ReConnNum % 5 == 0)
|
||||
// {
|
||||
// ClosePlcConn();
|
||||
// PlcInit();
|
||||
// }
|
||||
// ReConnNum++;
|
||||
//}
|
||||
|
||||
|
||||
#if USE_LOCAL_TCP_DEBUG
|
||||
|
||||
#else
|
||||
|
||||
if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时
|
||||
{
|
||||
isConn = false;
|
||||
//报警处理。
|
||||
frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
|
||||
if (ReConnNum % 5 == 0)
|
||||
{
|
||||
ClosePlcConn();
|
||||
PlcInit();
|
||||
}
|
||||
ReConnNum++;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user