865 lines
46 KiB
C#
865 lines
46 KiB
C#
#define USE_LOCAL_TCP_DEBUG //使用本地调式
|
||
|
||
using LeadDataBaseUtility;
|
||
using Modbus.Device;
|
||
using NX_LogUtility;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.IO.Ports;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace CleanMachineHMI
|
||
{
|
||
public class PlcOperationHelper
|
||
{
|
||
#region 单例模式
|
||
/// <summary>
|
||
/// 单例模式对象
|
||
/// </summary>
|
||
private static PlcOperationHelper _instance = null;
|
||
private static object lockObj = new object();
|
||
/// <summary>
|
||
/// 单例模式方法
|
||
/// </summary>
|
||
public static PlcOperationHelper Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
lock (lockObj)
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = new PlcOperationHelper();
|
||
}
|
||
}
|
||
}
|
||
return _instance;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
private bool _isConn = false;
|
||
|
||
|
||
public bool isConn
|
||
{
|
||
get
|
||
{
|
||
|
||
|
||
return this._isConn;
|
||
|
||
|
||
}
|
||
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
|
||
{
|
||
string portname = GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'PortName'")[0]["PARAMETER_VALUE"].ToString();
|
||
int baudrate = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'BaudRate'")[0]["PARAMETER_VALUE"]);
|
||
int databits = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'DataBits'")[0]["PARAMETER_VALUE"]);
|
||
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"]);
|
||
#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);
|
||
}
|
||
else
|
||
{
|
||
master = ModbusSerialMaster.CreateAscii(port);
|
||
}
|
||
#if USE_LOCAL_TCP_DEBUG
|
||
port.Connect("127.0.0.1", 5002);
|
||
this._isConn = true;
|
||
#else
|
||
port.Open();
|
||
#endif
|
||
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"初始化PLC连接时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
isConn = false;
|
||
return false;
|
||
}
|
||
}
|
||
public void ClosePlcConn()
|
||
{
|
||
if (port.Connected)
|
||
{
|
||
port.Close();
|
||
master = null;
|
||
isConn = false;
|
||
}
|
||
}
|
||
public void RefreshPlcItem_M()
|
||
{
|
||
try
|
||
{
|
||
bool[] readBytArr_M = master.ReadCoils(1, 2248, 50);
|
||
bool[] readBytArr_M1 = master.ReadCoils(1, 2048, 50);
|
||
if (readBytArr_M.Length > 0 && readBytArr_M1.Length > 0)
|
||
{
|
||
if (GlobalHelper.PlcItemMd_M_List != null && GlobalHelper.PlcItemMd_M_List.Count > 0)
|
||
{
|
||
foreach (BasPlcItemMd item in GlobalHelper.PlcItemMd_M_List)
|
||
{
|
||
if (Convert.ToInt32(item.PlcItemPriAddress) >= 200)
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
item.PlcItemValue = readBytArr_M[plcItemDeviationAddress].ToString();
|
||
GlobalHelper.ShowSensorStatus(item, new EventArgs());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
item.PlcItemValue = readBytArr_M1[plcItemDeviationAddress].ToString();
|
||
GlobalHelper.ShowSensorStatus(item, new EventArgs());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取M 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
}
|
||
public void RefreshPlcItem_D()
|
||
{
|
||
try
|
||
{
|
||
ushort[] tmpReadAlarmBytArr_D = master.ReadHoldingRegisters(1, 4106, 50);
|
||
ushort[] readBytArr_D = master.ReadHoldingRegisters(1, 4505, 50);
|
||
if (readBytArr_D.Length > 0 && tmpReadAlarmBytArr_D.Length > 0)
|
||
{
|
||
if (GlobalHelper.PlcItemMd_D_List != null && GlobalHelper.PlcItemMd_D_List.Count > 0)
|
||
{
|
||
foreach (BasPlcItemMd item in GlobalHelper.PlcItemMd_D_List)
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
if (Convert.ToInt32(item.PlcItemPriAddress) < 409)
|
||
{
|
||
item.PlcItemValue = tmpReadAlarmBytArr_D[plcItemDeviationAddress].ToString();
|
||
}
|
||
else
|
||
{
|
||
if (item.PlcItemDataLen == 2)
|
||
{
|
||
ushort[] tmp_1 = new ushort[] { readBytArr_D[plcItemDeviationAddress], readBytArr_D[plcItemDeviationAddress + 1] };
|
||
|
||
#region 方法一 :Short数组转换为int数据 20201014 孙亚龙
|
||
////低位 short 的字节数组。
|
||
//byte[] tmpByt_1 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress]);
|
||
////高位 short 的字节数组。
|
||
//byte[] tmpByt_2 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress + 1]);
|
||
////组合为int32的字节数组。
|
||
//byte[] tmpByt_3 = new byte[] { tmpByt_1[0], tmpByt_1[1], tmpByt_2[0], tmpByt_2[1] };
|
||
////readBytArr_D[plcItemDeviationAddress + 1] << 16 +
|
||
//item.PlcItemValue = BitConverter.ToUInt32(tmpByt_3, 0).ToString();
|
||
#endregion
|
||
|
||
#region 方法二 :Short数组转换为int数据 20201014 孙亚龙
|
||
//相较而言 使用位移的算法效率更高一些。
|
||
item.PlcItemValue = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
|
||
#endregion
|
||
|
||
}
|
||
else
|
||
{
|
||
item.PlcItemValue = readBytArr_D[plcItemDeviationAddress].ToString();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
HandleWaringLog(tmpReadAlarmBytArr_D);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取D 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
}
|
||
|
||
//报警信息记录
|
||
private void HandleWaringLog(ushort[] tmpReadAlarmBytArr_D)
|
||
{
|
||
try
|
||
{
|
||
int index = 10;
|
||
string priStr = "D";
|
||
string priWarnCode = "bit";
|
||
for (int i = 0; i < tmpReadAlarmBytArr_D.Length; i++)
|
||
{
|
||
if (i <= 1)
|
||
{//D10~D11 报警
|
||
char[] tmpCharArr = Convert.ToString(tmpReadAlarmBytArr_D[i], 2).PadLeft(16, '0').Reverse().ToArray();
|
||
for (int j = 0; j < tmpCharArr.Length; j++)
|
||
{
|
||
if (tmpCharArr[j] == '1')
|
||
{//当前报警
|
||
Bas_WarningMd tmpMd = GlobalHelper.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
|
||
if (tmpMd != null)
|
||
{
|
||
CM_WarningLogMd warnLogTmp = GlobalHelper.CmWarningLogMd_List.Find(y => y.WarningStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == tmpMd.PlcItemCode);
|
||
if (warnLogTmp != null)
|
||
{//找到报警项,并增加版本
|
||
warnLogTmp.UpdateTime = DateTime.Now;
|
||
warnLogTmp.DataVersion += 1;
|
||
}
|
||
else
|
||
{//新报警增加记录
|
||
GlobalHelper.CmWarningLogMd_List.Add(new CM_WarningLogMd
|
||
{
|
||
WarningCode = tmpMd.WarningCode,
|
||
WarningName = tmpMd.WarningName,
|
||
WarningType = tmpMd.WarningType,
|
||
WarningMessage = tmpMd.WarningMessage,
|
||
WarningTime = DateTime.Now,
|
||
PlcItemCode = tmpMd.PlcItemCode,
|
||
WarningStatus = 0,
|
||
WarningHandleResult = 0,
|
||
CreateBy = 1,
|
||
CreateTime = DateTime.Now,
|
||
UpdateBy = 1,
|
||
UpdateTime = DateTime.Now,
|
||
DataVersion = 0,
|
||
});
|
||
}
|
||
}
|
||
#region 已删除
|
||
|
||
// string sql = $@"SELECT * FROM BAS_WARNING WHERE PLC_ITEM_CODE = '{priStr + (index + i)}' and WARNING_CODE = '{priWarnCode + i}' and WARNING_TYPE = '1'";
|
||
// DataTable dt = LeadDbHelperSQLite.Query(sql).Tables[0];
|
||
// if (dt != null && dt.Rows.Count > 0)
|
||
// {
|
||
// string sql1 = $@"
|
||
// INSERT INTO CM_WARNING_LOG (
|
||
// WARNING_CODE,
|
||
// WARNING_NAME,
|
||
// WARNING_TYPE,
|
||
// WARNING_MESSAGE,
|
||
// WARNING_TIME,
|
||
// PLC_ITEM_CODE,
|
||
// WARING_STATUS,
|
||
// WARNING_HANDLE_RESULT,
|
||
// CREATE_BY,
|
||
// CREATE_TIME,
|
||
// UPDATE_BY,
|
||
// UPDATE_TIME,
|
||
// DATA_VERSION
|
||
// )
|
||
// SELECT
|
||
// '{dt.Rows[0]["WARNING_CODE"]}',
|
||
// '{dt.Rows[0]["WARNING_NAME"]}',
|
||
// '{dt.Rows[0]["WARNING_TYPE"]}',
|
||
// '{dt.Rows[0]["WARNING_MESSAGE"]}',
|
||
// datetime('now', 'localtime'),
|
||
// '{dt.Rows[0]["PLC_ITEM_CODE"]}',
|
||
// 0,
|
||
// 0,
|
||
// 1,
|
||
// datetime('now', 'localtime'),
|
||
// 1,
|
||
// datetime('now', 'localtime'),
|
||
// 0
|
||
// WHERE NOT EXISTS (SELECT 1 FROM CM_WARNING_LOG WHERE WARING_STATUS = 0 and WARNING_HANDLE_RESULT = 0 and WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' and PLC_ITEM_CODE = '{dt.Rows[0]["PLC_ITEM_CODE"]}')
|
||
// ";
|
||
// LeadDbHelperSQLite.ExecuteSql(sql1);
|
||
|
||
// string sql2 = $@"
|
||
//UPDATE CM_WARNING_LOG set UPDATE_TIME = datetime('now', 'localtime'),DATA_VERSION = DATA_VERSION + 1
|
||
// WHERE EXISTS(SELECT 1 FROM CM_WARNING_LOG WHERE WARING_STATUS = 0 and WARNING_HANDLE_RESULT = 0 and WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' and PLC_ITEM_CODE = '{dt.Rows[0]["PLC_ITEM_CODE"]}') and WARING_STATUS = 0 and WARNING_HANDLE_RESULT = 0 and WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' and PLC_ITEM_CODE = '{dt.Rows[0]["PLC_ITEM_CODE"]}';
|
||
// ";
|
||
// LeadDbHelperSQLite.ExecuteSql(sql2);
|
||
// }
|
||
|
||
#endregion
|
||
}
|
||
else
|
||
{//当前报警项没有报警
|
||
Bas_WarningMd tmpMd = GlobalHelper.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
|
||
if (tmpMd != null)
|
||
{//之前有报警,则结束当前报警项
|
||
CM_WarningLogMd warnLogTmp = GlobalHelper.CmWarningLogMd_List.Find(y => y.WarningStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == tmpMd.PlcItemCode);
|
||
if (warnLogTmp != null)
|
||
{
|
||
warnLogTmp.UpdateTime = DateTime.Now;
|
||
warnLogTmp.DataVersion += 1;
|
||
warnLogTmp.WarningStatus = 1;
|
||
warnLogTmp.WarningHandleResult = 1;
|
||
warnLogTmp.WarningHandleTime = DateTime.Now;
|
||
}
|
||
}
|
||
#region 已删除
|
||
//string sql = $@"SELECT * FROM BAS_WARNING WHERE PLC_ITEM_CODE = '{priStr + (index + i)}' and WARNING_CODE = '{priWarnCode + i}' and WARNING_TYPE = '1'";
|
||
//DataTable dt = LeadDbHelperSQLite.Query(sql).Tables[0];
|
||
//if (dt != null && dt.Rows.Count > 0)
|
||
//{
|
||
// string sql2 = $@"
|
||
// UPDATE CM_WARNING_LOG
|
||
// SET UPDATE_TIME = datetime('now', 'localtime'),
|
||
// DATA_VERSION = DATA_VERSION + 1,
|
||
// WARING_STATUS = 1,
|
||
// WARNING_HANDLE_RESULT = 1,
|
||
// WARNING_HANDLE_TIME = datetime('now', 'localtime')
|
||
// WHERE
|
||
// EXISTS ( SELECT 1 FROM CM_WARNING_LOG WHERE WARING_STATUS = 0 AND WARNING_HANDLE_RESULT = 0 AND WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' AND PLC_ITEM_CODE = '{dt.Rows[0]["PLC_ITEM_CODE"]}' )
|
||
// AND WARING_STATUS = 0
|
||
// AND WARNING_HANDLE_RESULT = 0
|
||
// AND WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}'
|
||
// AND PLC_ITEM_CODE = '{dt.Rows[0]["PLC_ITEM_CODE"]}';
|
||
// ";
|
||
// LeadDbHelperSQLite.ExecuteSql(sql2);
|
||
//}
|
||
#endregion
|
||
}
|
||
}
|
||
}
|
||
else if (i == 2)
|
||
{
|
||
//char[] tmpCharArr = Convert.ToString(tmpReadAlarmBytArr_D[i], 2).PadLeft(16, '0').Reverse().ToArray();
|
||
//if (tmpCharArr.Contains('1'))
|
||
//{
|
||
// List<Bas_WarningMd> warnLst = new List<Bas_WarningMd>();
|
||
// for (int j = 0; j < tmpCharArr.Length; j++)
|
||
// {
|
||
// if (tmpCharArr[j] == '1')
|
||
// {
|
||
// Bas_WarningMd tmpMd = GlobalHelper.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
|
||
// if (tmpMd != null)
|
||
// {
|
||
// warnLst.Add(tmpMd);
|
||
// }
|
||
// }
|
||
// }
|
||
// GlobalHelper.ShowWarningMsg(warnLst, new EventArgs());
|
||
//}
|
||
}
|
||
else if (i >= 3 && i <= 9)
|
||
{//D13~D19报警
|
||
if (tmpReadAlarmBytArr_D[i] == 0)
|
||
{
|
||
CM_WarningLogMd warnLogTmp = GlobalHelper.CmWarningLogMd_List.Find(y => y.WarningStatus == 0 && y.WarningHandleResult == 0 && y.PlcItemCode == $"{priStr + (index + i)}");
|
||
if (warnLogTmp != null)
|
||
{
|
||
warnLogTmp.UpdateTime = DateTime.Now;
|
||
warnLogTmp.DataVersion += 1;
|
||
warnLogTmp.WarningStatus = 1;
|
||
warnLogTmp.WarningHandleResult = 1;
|
||
warnLogTmp.WarningHandleTime = DateTime.Now;
|
||
}
|
||
#region 已删除
|
||
//string sql2 = $@"
|
||
// UPDATE CM_WARNING_LOG
|
||
// SET UPDATE_TIME = datetime('now', 'localtime'),
|
||
// DATA_VERSION = DATA_VERSION + 1,
|
||
// WARING_STATUS = 1,
|
||
// WARNING_HANDLE_RESULT = 1,
|
||
// WARNING_HANDLE_TIME = datetime('now', 'localtime')
|
||
// WHERE
|
||
// EXISTS ( SELECT 1 FROM CM_WARNING_LOG WHERE WARING_STATUS = 0 AND WARNING_HANDLE_RESULT = 0 AND WARNING_CODE = '{i}' AND PLC_ITEM_CODE is NULL )
|
||
// AND WARING_STATUS = 0
|
||
// AND WARNING_HANDLE_RESULT = 0
|
||
// AND WARNING_CODE = 'i'
|
||
// AND PLC_ITEM_CODE is NULL;
|
||
// ";
|
||
//LeadDbHelperSQLite.ExecuteSql(sql2);
|
||
#endregion
|
||
}
|
||
else
|
||
{//根据报警编码,获取报警信息,根据读取顺序编号,组成PlcItemCode
|
||
Bas_WarningMd tmpMd = GlobalHelper.BasWarningMd_List.Find(x => string.IsNullOrEmpty(x.PlcItemCode) && x.WarningCode == tmpReadAlarmBytArr_D[i].ToString() && x.WarningType == "2");
|
||
if (tmpMd != null)
|
||
{
|
||
CM_WarningLogMd warnLogTmp = GlobalHelper.CmWarningLogMd_List.Find(y => y.WarningStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == $"{priStr + (index + i)}");
|
||
if (warnLogTmp != null)
|
||
{
|
||
warnLogTmp.UpdateTime = DateTime.Now;
|
||
warnLogTmp.DataVersion += 1;
|
||
}
|
||
else
|
||
{
|
||
GlobalHelper.CmWarningLogMd_List.Add(new CM_WarningLogMd
|
||
{
|
||
WarningCode = tmpMd.WarningCode,
|
||
WarningName = tmpMd.WarningName,
|
||
WarningType = tmpMd.WarningType,
|
||
WarningMessage = tmpMd.WarningMessage,
|
||
WarningTime = DateTime.Now,
|
||
PlcItemCode = $"{priStr + (index + i)}",
|
||
WarningStatus = 0,
|
||
WarningHandleResult = 0,
|
||
CreateBy = 1,
|
||
CreateTime = DateTime.Now,
|
||
UpdateBy = 1,
|
||
UpdateTime = DateTime.Now,
|
||
DataVersion = 0,
|
||
});
|
||
}
|
||
}
|
||
|
||
#region 已删除
|
||
|
||
// string sql = $@"SELECT * FROM BAS_WARNING WHERE PLC_ITEM_CODE is NULL and WARNING_CODE = '{i}' and WARNING_TYPE = '2'";
|
||
// DataTable dt = LeadDbHelperSQLite.Query(sql).Tables[0];
|
||
// if (dt != null && dt.Rows.Count > 0)
|
||
// {
|
||
// string sql1 = $@"
|
||
// INSERT INTO CM_WARNING_LOG (
|
||
// WARNING_CODE,
|
||
// WARNING_NAME,
|
||
// WARNING_TYPE,
|
||
// WARNING_MESSAGE,
|
||
// WARNING_TIME,
|
||
// PLC_ITEM_CODE,
|
||
// WARING_STATUS,
|
||
// WARNING_HANDLE_RESULT,
|
||
// CREATE_BY,
|
||
// CREATE_TIME,
|
||
// UPDATE_BY,
|
||
// UPDATE_TIME,
|
||
// DATA_VERSION
|
||
// )
|
||
// SELECT
|
||
// '{dt.Rows[0]["WARNING_CODE"]}',
|
||
// '{dt.Rows[0]["WARNING_NAME"]}',
|
||
// '{dt.Rows[0]["WARNING_TYPE"]}',
|
||
// '{dt.Rows[0]["WARNING_MESSAGE"]}',
|
||
// datetime('now', 'localtime'),
|
||
// '{dt.Rows[0]["PLC_ITEM_CODE"]}',
|
||
// 0,
|
||
// 0,
|
||
// 1,
|
||
// datetime('now', 'localtime'),
|
||
// 1,
|
||
// datetime('now', 'localtime'),
|
||
// 0
|
||
// WHERE NOT EXISTS (SELECT 1 FROM CM_WARNING_LOG WHERE WARING_STATUS = 0 and WARNING_HANDLE_RESULT = 0 and WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' and PLC_ITEM_CODE is NULL)
|
||
// ";
|
||
// LeadDbHelperSQLite.ExecuteSql(sql1);
|
||
// string sql2 = $@"
|
||
//UPDATE CM_WARNING_LOG set UPDATE_TIME = datetime('now', 'localtime'),DATA_VERSION = DATA_VERSION + 1
|
||
// WHERE EXISTS(SELECT 1 FROM CM_WARNING_LOG WHERE WARING_STATUS = 0 and WARNING_HANDLE_RESULT = 0 and WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' and PLC_ITEM_CODE is NULL) and WARING_STATUS = 0 and WARNING_HANDLE_RESULT = 0 and WARNING_CODE = '{dt.Rows[0]["WARNING_CODE"]}' and is NULL;
|
||
// ";
|
||
// LeadDbHelperSQLite.ExecuteSql(sql2);
|
||
// }
|
||
#endregion
|
||
|
||
}
|
||
}
|
||
else if (i > 9)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
List<CM_WarningLogMd> tmpLst = GlobalHelper.CmWarningLogMd_List.FindAll(x => x.WarningHandleResult == 1 && x.WarningStatus == 1);
|
||
if (tmpLst.Count >= 10)
|
||
{
|
||
foreach (CM_WarningLogMd item in tmpLst)
|
||
{
|
||
string sql1 = $@"
|
||
INSERT INTO CM_WARNING_LOG (
|
||
WARNING_CODE,
|
||
WARNING_NAME,
|
||
WARNING_TYPE,
|
||
WARNING_MESSAGE,
|
||
WARNING_TIME,
|
||
PLC_ITEM_CODE,
|
||
WARING_STATUS,
|
||
WARNING_HANDLE_RESULT,
|
||
WARNING_HANDLE_TIME,
|
||
CREATE_BY,
|
||
CREATE_TIME,
|
||
UPDATE_BY,
|
||
UPDATE_TIME,
|
||
DATA_VERSION
|
||
)
|
||
VALUES (
|
||
'{item.WarningCode}',
|
||
'{item.WarningName}',
|
||
'{item.WarningType}',
|
||
'{item.WarningMessage}',
|
||
'{item.WarningTime.ToString("yyyy-MM-dd HH:mm:ss")}',
|
||
'{item.PlcItemCode}',
|
||
'{item.WarningStatus}',
|
||
'{item.WarningHandleResult}',
|
||
'{item.WarningHandleTime.ToString("yyyy-MM-dd HH:mm:ss")}',
|
||
'{item.CreateBy}',
|
||
'{item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")}',
|
||
'{item.UpdateBy}',
|
||
'{item.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss")}',
|
||
'{item.DataVersion}'
|
||
);
|
||
";
|
||
LeadDbHelperSQLite.ExecuteSql(sql1);
|
||
}
|
||
GlobalHelper.CmWarningLogMd_List.RemoveAll(x => x.WarningHandleResult == 1 && x.WarningStatus == 1);
|
||
}
|
||
|
||
if (GlobalHelper.CmWarningLogMd_List.FindAll(x => x.WarningHandleResult == 0 && x.WarningStatus == 0).Count > 0)
|
||
{
|
||
//frmMainNew.Show_ptb_Alarm(true);
|
||
GlobalHelper.ShowMainFrmAlarmMsg(true, EventArgs.Empty);
|
||
}
|
||
else
|
||
{
|
||
//frmMainNew.Show_ptb_Alarm(false);
|
||
GlobalHelper.ShowMainFrmAlarmMsg(false, EventArgs.Empty);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"处理设备报警信息时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
}
|
||
|
||
public void RefreshPlcItem_X()
|
||
{
|
||
try
|
||
{
|
||
bool[] readBytArr_X = master.ReadInputs(1, 1024, 50);
|
||
if (readBytArr_X.Length > 0)
|
||
//if(true)
|
||
{
|
||
if (GlobalHelper.PlcItemMd_X_List != null && GlobalHelper.PlcItemMd_X_List.Count > 0)
|
||
{
|
||
foreach (BasPlcItemMd item in GlobalHelper.PlcItemMd_X_List)
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
item.PlcItemValue = readBytArr_X[plcItemDeviationAddress].ToString();
|
||
|
||
//Random r = new Random();
|
||
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
|
||
|
||
GlobalHelper.ShowX_Y_Status(item, new EventArgs());
|
||
GlobalHelper.ShowSensorStatus(item, new EventArgs());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取X 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
}
|
||
|
||
public void RefreshPlcItem_Y()
|
||
{
|
||
try
|
||
{
|
||
bool[] readBytArr_Y = master.ReadCoils(1, 1280, 50);
|
||
if (readBytArr_Y.Length > 0)
|
||
//if(true)
|
||
{
|
||
if (GlobalHelper.PlcItemMd_Y_List != null && GlobalHelper.PlcItemMd_Y_List.Count > 0)
|
||
{
|
||
foreach (BasPlcItemMd item in GlobalHelper.PlcItemMd_Y_List)
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
item.PlcItemValue = readBytArr_Y[plcItemDeviationAddress].ToString();
|
||
|
||
//Random r = new Random();
|
||
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
|
||
|
||
GlobalHelper.ShowX_Y_Status(item, new EventArgs());
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取Y 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
}
|
||
|
||
public object ReadPlcItem(string plcItemCode, PlcItemTypeDesc itemType)
|
||
{
|
||
try
|
||
{
|
||
object retObj;
|
||
switch (itemType)
|
||
{
|
||
case PlcItemTypeDesc.M:
|
||
BasPlcItemMd item_M = GlobalHelper.PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_M == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (string.IsNullOrEmpty(item_M.PlcItemValue))
|
||
{
|
||
retObj = false;
|
||
}
|
||
else
|
||
{
|
||
retObj = bool.Parse(item_M.PlcItemValue);
|
||
}
|
||
break;
|
||
case PlcItemTypeDesc.D:
|
||
BasPlcItemMd item_D = GlobalHelper.PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_D == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (string.IsNullOrEmpty(item_D.PlcItemValue))
|
||
{
|
||
retObj = 0;
|
||
}
|
||
else
|
||
{
|
||
retObj = short.Parse(item_D.PlcItemValue);
|
||
}
|
||
break;
|
||
case PlcItemTypeDesc.X:
|
||
BasPlcItemMd item_X = GlobalHelper.PlcItemMd_X_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_X == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (string.IsNullOrEmpty(item_X.PlcItemValue))
|
||
{
|
||
retObj = false;
|
||
}
|
||
else
|
||
{
|
||
retObj = bool.Parse(item_X.PlcItemValue);
|
||
}
|
||
break;
|
||
case PlcItemTypeDesc.Y:
|
||
BasPlcItemMd item_Y = GlobalHelper.PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_Y == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (string.IsNullOrEmpty(item_Y.PlcItemValue))
|
||
{
|
||
retObj = false;
|
||
}
|
||
else
|
||
{
|
||
retObj = bool.Parse(item_Y.PlcItemValue);
|
||
}
|
||
break;
|
||
default:
|
||
retObj = null;
|
||
break;
|
||
}
|
||
return retObj;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取测试:【{plcItemCode}】", LogTypeEum.Err);
|
||
LogHelper.WriteLog($"读取【{itemType.ToString()}】类型PLC变量【{plcItemCode}】时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
public bool WritePlcItem(string plcItemCode, string writeValue, PlcItemTypeDesc itemType)
|
||
{
|
||
try
|
||
{
|
||
switch (itemType)
|
||
{
|
||
case PlcItemTypeDesc.M:
|
||
BasPlcItemMd item_M = GlobalHelper.PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_M == null)
|
||
{
|
||
return false;
|
||
}
|
||
master.WriteSingleCoil(1, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue));
|
||
return true;
|
||
case PlcItemTypeDesc.Y:
|
||
BasPlcItemMd item_Y = GlobalHelper.PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_Y == null)
|
||
{
|
||
return false;
|
||
}
|
||
master.WriteSingleCoil(1, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue));
|
||
return true;
|
||
case PlcItemTypeDesc.D:
|
||
BasPlcItemMd item_D = GlobalHelper.PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_D == null)
|
||
{
|
||
return false;
|
||
}
|
||
master.WriteSingleRegister(1, Convert.ToUInt16(item_D.PlcItemModbusAddress), ushort.Parse(writeValue));
|
||
return true;
|
||
case PlcItemTypeDesc.X:
|
||
return false;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"写入【{itemType.ToString()}】类型PLC变量【{plcItemCode}】的值【{writeValue}】时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
return false;
|
||
}
|
||
}
|
||
private int wcsToPlcHeartDataNew = 0;
|
||
private DateTime HeartbeatTime = new DateTime();
|
||
private int ReConnNum = 1;
|
||
public void HeartbeatDetection()
|
||
{
|
||
try
|
||
{
|
||
int wcsToPlcHeartData = Convert.ToInt32(ReadPlcItem("PlcHeartbeatWriteData", PlcItemTypeDesc.D) == null ? 0 : ReadPlcItem("PlcHeartbeatWriteData", PlcItemTypeDesc.D));
|
||
int plcToWcsHeartData = Convert.ToInt32(ReadPlcItem("PlcHeartbeatReadData", PlcItemTypeDesc.D) == null ? 0 : ReadPlcItem("PlcHeartbeatReadData", PlcItemTypeDesc.D));
|
||
if (wcsToPlcHeartData == 0 && plcToWcsHeartData == 0)
|
||
{
|
||
WritePlcItem("PlcHeartbeatWriteData", "1", PlcItemTypeDesc.D);
|
||
wcsToPlcHeartDataNew = 1;
|
||
}
|
||
else if (wcsToPlcHeartDataNew == plcToWcsHeartData && plcToWcsHeartData != 0 && wcsToPlcHeartDataNew != 0)
|
||
{
|
||
if (plcToWcsHeartData >= 256)
|
||
{
|
||
wcsToPlcHeartDataNew = 1;
|
||
wcsToPlcHeartData = wcsToPlcHeartDataNew;
|
||
}
|
||
else
|
||
{
|
||
wcsToPlcHeartData = wcsToPlcHeartDataNew + 1;
|
||
}
|
||
if (WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartData.ToString(), PlcItemTypeDesc.D))
|
||
{
|
||
isConn = true;
|
||
HeartbeatTime = DateTime.Now;
|
||
wcsToPlcHeartDataNew = wcsToPlcHeartData;
|
||
frmMainNew.Show_Plc_Alarm("Serial port open success, PLC connect success!");
|
||
}
|
||
}
|
||
else if (wcsToPlcHeartDataNew == 0 && plcToWcsHeartData != 0)
|
||
{
|
||
if (plcToWcsHeartData == wcsToPlcHeartData)
|
||
{
|
||
if (plcToWcsHeartData >= 256)
|
||
{
|
||
wcsToPlcHeartDataNew = 1;
|
||
wcsToPlcHeartData = wcsToPlcHeartDataNew;
|
||
}
|
||
else
|
||
{
|
||
wcsToPlcHeartData = wcsToPlcHeartDataNew + 1;
|
||
}
|
||
if (WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartData.ToString(), PlcItemTypeDesc.D))
|
||
{
|
||
isConn = true;
|
||
HeartbeatTime = DateTime.Now;
|
||
wcsToPlcHeartDataNew = wcsToPlcHeartData;
|
||
frmMainNew.Show_Plc_Alarm("Serial port open success, PLC connect success!");
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartDataNew.ToString(), PlcItemTypeDesc.D);
|
||
}
|
||
|
||
|
||
|
||
#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
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public enum PlcItemTypeDesc
|
||
{
|
||
/// <summary>
|
||
/// M 类型布尔变量
|
||
/// </summary>
|
||
M,
|
||
/// <summary>
|
||
/// D 类型整数变量
|
||
/// </summary>
|
||
D,
|
||
/// <summary>
|
||
/// X 类型布尔变量
|
||
/// </summary>
|
||
X,
|
||
/// <summary>
|
||
/// Y 类型布尔变量
|
||
/// </summary>
|
||
Y
|
||
}
|
||
}
|