Files
CleanMachine/CleanMachineHMI/Utils/PlcOperationHelper.cs

1242 lines
53 KiB
C#
Raw Normal View History

2022-01-19 14:59:16 +08:00
//使用本地调式
2022-05-05 09:38:28 +08:00
#if DEBUG
2022-05-09 08:59:35 +08:00
#define USE_LOCAL_TCP_DEBUG
2022-05-05 09:38:28 +08:00
#endif
2022-01-19 14:59:16 +08:00
//启用心跳检查
2021-12-27 09:51:57 +08:00
2022-05-05 09:38:28 +08:00
#if RELEASE
2022-05-05 15:15:53 +08:00
#define UseHeartbeat
2022-05-05 09:38:28 +08:00
#endif
2021-12-31 19:53:53 +08:00
using CleanMachine.Model;
2022-05-05 20:42:02 +08:00
using CleanMachine.Model.BAS;
using CleanMachine.Model.CM;
2021-12-27 09:51:57 +08:00
using LeadDataBaseUtility;
2022-01-13 10:43:45 +08:00
using NModbus;
2021-06-05 14:17:30 +08:00
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;
2022-05-06 15:23:30 +08:00
using System.Threading;
2021-06-05 14:17:30 +08:00
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
2022-05-09 08:59:35 +08:00
private PlcOperationHelper()
{
_synchronization = SynchronizationContext.Current;
}
2022-05-09 13:22:39 +08:00
/// <summary>
/// 读取完成的委托
/// </summary>
public delegate void ReadFinish();
public event ReadFinish OnReadFinish;
public void DoOnReadFinish(object data)
{
OnReadFinish?.Invoke();
}
2022-05-10 15:21:13 +08:00
public delegate void ShowX_Y_Status(IEnumerable<PlcItem> plcItem);
2022-05-09 13:22:39 +08:00
2022-05-10 15:21:13 +08:00
public event ShowX_Y_Status OnShowX_Y_Status;
private void PostShowX_Y_Status(object data)
{
var _data = data as IEnumerable<PlcItem>;
OnShowX_Y_Status?.Invoke(_data);
}
public void DoShowX_Y_Status(IEnumerable<PlcItem> plcItem)
{
if (_synchronization != null)
{
_synchronization.Post(this.PostShowX_Y_Status, plcItem);
2022-05-11 15:20:00 +08:00
// OnShowX_Y_Status?.Invoke(plcItem);
2022-05-10 15:21:13 +08:00
}
}
2022-05-09 13:22:39 +08:00
2022-05-09 08:59:35 +08:00
private SynchronizationContext _synchronization;
2021-12-27 21:39:45 +08:00
2022-05-06 15:23:30 +08:00
private byte slaveAddress = byte.Parse(ConfigurationManager.AppSettings["SlaveAddress"]);
2021-12-27 21:39:45 +08:00
2021-06-05 14:17:30 +08:00
private bool _isConn = false;
public bool isConn
{
get
{
return this._isConn;
}
set { this._isConn = value; }
}
private static IModbusMaster master;
2022-01-13 10:43:45 +08:00
//NModbus.IO.IStreamResource port;
2021-12-27 09:51:57 +08:00
#if USE_LOCAL_TCP_DEBUG
2021-12-26 21:30:02 +08:00
private static System.Net.Sockets.TcpClient port;
2021-12-27 09:51:57 +08:00
#else
private static SerialPort port;
#endif
2022-05-06 15:23:30 +08:00
2022-05-09 13:22:39 +08:00
public List<PlcItem> PlcItemMdList { get; set; } = new List<PlcItem>();
2022-05-06 15:23:30 +08:00
public List<PlcItem> PlcItemMd_D_List { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMd_M_List { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMd_X_List { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMd_Y_List { get; set; } = new List<PlcItem>();
2022-05-09 13:22:39 +08:00
public void StartRead()
{
Thread th = new Thread(RefreshPlcItem)
{
IsBackground = true
};
th.Start();
2022-05-06 15:23:30 +08:00
2022-05-09 13:22:39 +08:00
}
2022-05-09 08:59:35 +08:00
// public System.Threading.AutoResetEvent thEvent = new System.Threading.AutoResetEvent(false);
2022-05-06 15:23:30 +08:00
private void RefreshPlcItem()
{
2022-05-11 15:20:00 +08:00
Stopwatch stopWatch = new Stopwatch();
2022-05-06 15:23:30 +08:00
while (Global.SysThIsRunFlag)
{
2022-05-11 15:20:00 +08:00
stopWatch.Restart();
2022-05-10 15:21:13 +08:00
var haschange = false;
2022-05-09 13:22:39 +08:00
RefreshPlcItem_M(ref haschange);
RefreshPlcItem_D(ref haschange);
RefreshPlcItem_X(ref haschange);
RefreshPlcItem_Y(ref haschange);
2022-05-11 15:20:00 +08:00
stopWatch.Stop();
2022-05-13 09:08:42 +08:00
TimeSpan ts = stopWatch.Elapsed;
2022-05-11 15:20:00 +08:00
LogHelper.WriteLog($"读取PLC时间:{ts.Milliseconds}", LogTypeEum.Run);
2022-05-09 13:22:39 +08:00
if (haschange)
{
if (_synchronization != null)
{
_synchronization.Post(this.DoOnReadFinish, null);
}
}
2022-05-11 15:20:00 +08:00
2022-05-09 08:59:35 +08:00
// thEvent.Set();
2022-05-06 15:23:30 +08:00
// ShowWarningMsg();
2022-05-11 15:20:00 +08:00
int sleeptime = 100 - ts.Milliseconds;
2022-05-13 09:08:42 +08:00
if (sleeptime < 0)
2022-05-11 15:20:00 +08:00
{
sleeptime = 0;
}
Thread.Sleep(sleeptime);
2022-05-06 15:23:30 +08:00
}
}
2021-12-27 09:51:57 +08:00
/// <summary>
/// 初始化PLC
/// </summary>
/// <returns></returns>
2022-05-06 15:23:30 +08:00
public bool PlcInit(IEnumerable<PlcItem> items)
2021-06-05 14:17:30 +08:00
{
2022-01-19 09:31:47 +08:00
2022-05-06 15:23:30 +08:00
foreach (var plcitem in items)
{
switch (plcitem.PlcItemDataType)
{
2022-05-10 15:21:13 +08:00
case PlcItemTypeDesc.M:
2022-05-06 15:23:30 +08:00
PlcItemMd_M_List.Add(plcitem);
break;
2022-05-10 15:21:13 +08:00
case PlcItemTypeDesc.D:
2022-05-06 15:23:30 +08:00
PlcItemMd_D_List.Add(plcitem);
break;
2022-05-10 15:21:13 +08:00
case PlcItemTypeDesc.X:
2022-05-06 15:23:30 +08:00
PlcItemMd_X_List.Add(plcitem);
break;
2022-05-10 15:21:13 +08:00
case PlcItemTypeDesc.Y:
2022-05-06 15:23:30 +08:00
PlcItemMd_Y_List.Add(plcitem);
break;
}
PlcItemMdList.Add(plcitem);
}
2022-05-05 15:15:53 +08:00
2021-06-05 14:17:30 +08:00
try
{
2022-05-05 15:15:53 +08:00
string portname = ConfigurationManager.AppSettings["PortName"];
int baudrate = int.Parse(ConfigurationManager.AppSettings["BaudRate"]);
int databits = int.Parse(ConfigurationManager.AppSettings["DataBits"]);
StopBits stopbis = (StopBits)int.Parse(ConfigurationManager.AppSettings["StopBits"]);
Parity paritymsg = (Parity)int.Parse(ConfigurationManager.AppSettings["ParityMsg"]);
int modbusPattern = int.Parse(ConfigurationManager.AppSettings["ModbusPattern"]);
2022-05-06 15:23:30 +08:00
2022-05-05 15:15:53 +08:00
//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"]);
2021-12-27 09:51:57 +08:00
#if USE_LOCAL_TCP_DEBUG
2022-05-06 12:28:16 +08:00
port = new System.Net.Sockets.TcpClient();
2022-01-13 10:43:45 +08:00
2022-05-06 12:28:16 +08:00
var adaptet = new NModbus.IO.TcpClientAdapter(port);
2021-12-27 09:51:57 +08:00
#else
2022-05-05 15:15:53 +08:00
port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
var adaptet = new NModbus.Serial.SerialPortAdapter(port);
adaptet.ReadTimeout = 200;
2021-12-27 09:51:57 +08:00
#endif
2022-05-05 15:15:53 +08:00
var factory = new ModbusFactory();
if (modbusPattern == 0)
{
master = factory.CreateRtuMaster(adaptet);
}
else
{
master = factory.CreateAsciiMaster(adaptet);
}
2022-01-13 10:43:45 +08:00
2021-12-27 09:51:57 +08:00
#if USE_LOCAL_TCP_DEBUG
2022-05-06 12:28:16 +08:00
port.Connect("10.0.0.95", 5002);
this._isConn = true;
return true;
2021-12-27 09:51:57 +08:00
#else
2022-01-13 10:43:45 +08:00
2022-05-05 15:15:53 +08:00
port.Open();
return port.IsOpen;
2021-12-27 09:51:57 +08:00
#endif
2022-05-05 15:15:53 +08:00
2022-05-05 09:38:28 +08:00
}
2021-06-05 14:17:30 +08:00
catch (Exception ex)
{
LogHelper.WriteLog($"初始化PLC连接时发生异常:【{ex.Message}】", LogTypeEum.Err);
isConn = false;
return false;
}
2022-05-05 15:15:53 +08:00
2021-06-05 14:17:30 +08:00
}
public void ClosePlcConn()
{
2022-01-19 14:59:16 +08:00
#if USE_LOCAL_TCP_DEBUG
2021-12-26 21:30:02 +08:00
if (port.Connected)
2022-01-19 14:59:16 +08:00
#else
if (port.IsOpen)
#endif
2021-06-05 14:17:30 +08:00
{
port.Close();
master = null;
isConn = false;
}
}
2022-05-09 13:22:39 +08:00
public void RefreshPlcItem_M(ref bool haschange)
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
bool[] readBytArr_M = master.ReadCoils(slaveAddress, 2248, 50);
bool[] readBytArr_M1 = master.ReadCoils(slaveAddress, 2048, 50);
if (readBytArr_M.Length > 0 && readBytArr_M1.Length > 0)
{
2022-05-06 15:23:30 +08:00
if (PlcItemMd_M_List != null && PlcItemMd_M_List.Count > 0)
2021-06-05 14:17:30 +08:00
{
2022-05-06 15:23:30 +08:00
foreach (PlcItem item in PlcItemMd_M_List)
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
string value = "";
2022-01-18 20:20:10 +08:00
if (Convert.ToInt32(item.PlcItemPriAddress) >= 200)
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
2022-01-18 20:20:10 +08:00
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
2021-06-05 14:17:30 +08:00
{
2022-05-05 09:12:10 +08:00
//string PlcItemId = item.PlcItemId.ToString();
2022-05-09 13:22:39 +08:00
//string plcdatatype = item.PlcItemDataType.ToString();
2022-01-18 20:20:10 +08:00
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
2022-05-09 13:22:39 +08:00
value = readBytArr_M[plcItemDeviationAddress].ToString();
2021-06-05 14:17:30 +08:00
}
2022-01-18 20:20:10 +08:00
}
else
{
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
2021-06-05 14:17:30 +08:00
{
2022-05-05 09:12:10 +08:00
//string PlcItemId = item.PlcItemId.ToString();
//string plcdatatype = item.PlcItemDataType.ToString();
2022-01-18 20:20:10 +08:00
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
2022-05-09 13:22:39 +08:00
value = readBytArr_M1[plcItemDeviationAddress].ToString();
}
}
if (value != "")
{
if (item.PlcItemValue != value)
{
haschange = true;
2021-06-05 14:17:30 +08:00
}
2022-05-09 13:22:39 +08:00
item.PlcItemValue = value;
Global.ShowSensorStatus(item, new EventArgs());
2021-06-05 14:17:30 +08:00
}
2022-05-09 13:22:39 +08:00
2021-06-05 14:17:30 +08:00
}
}
}
2022-05-05 09:38:28 +08:00
#if RELEASE
2022-01-18 20:20:10 +08:00
}
2021-06-05 14:17:30 +08:00
catch (Exception ex)
{
LogHelper.WriteLog($"读取M 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
2022-05-09 13:22:39 +08:00
public void RefreshPlcItem_D(ref bool haschange)
2021-06-05 14:17:30 +08:00
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
2022-05-10 15:21:13 +08:00
2022-01-18 20:20:10 +08:00
ushort[] tmpReadAlarmBytArr_D = master.ReadHoldingRegisters(slaveAddress, 4106, 50);
ushort[] readBytArr_D = master.ReadHoldingRegisters(slaveAddress, 4505, 50);
if (readBytArr_D.Length > 0 && tmpReadAlarmBytArr_D.Length > 0)
{
2022-05-06 15:23:30 +08:00
if (PlcItemMd_D_List != null && PlcItemMd_D_List.Count > 0)
2021-06-05 14:17:30 +08:00
{
2022-05-06 15:23:30 +08:00
foreach (PlcItem item in PlcItemMd_D_List)
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
var value = "";
2022-01-18 20:20:10 +08:00
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
string PlcItemId = item.PlcItemId.ToString();
2022-05-05 15:15:53 +08:00
// string plcdatatype = item.PlcItemDataType.ToString();
2022-01-18 20:20:10 +08:00
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
if (Convert.ToInt32(item.PlcItemPriAddress) < 409)
{
2022-05-09 13:22:39 +08:00
value = tmpReadAlarmBytArr_D[plcItemDeviationAddress].ToString();
2022-01-18 20:20:10 +08:00
}
else
{
2022-05-09 08:59:35 +08:00
2022-01-18 20:20:10 +08:00
if (item.PlcItemDataLength == 2)
2021-06-05 14:17:30 +08:00
{
2022-05-09 08:59:35 +08:00
// ushort[] tmp_1 = new ushort[] { readBytArr_D[plcItemDeviationAddress], readBytArr_D[plcItemDeviationAddress + 1] };
2022-01-18 20:20:10 +08:00
#region 方法一 :Short数组转换为int数据 20201014 孙亚龙
2022-05-13 14:55:10 +08:00
//低位 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] };
2022-05-13 09:08:42 +08:00
2022-05-13 14:55:10 +08:00
//readBytArr_D[plcItemDeviationAddress + 1] << 16 +
value = BitConverter.ToInt32(tmpByt_3, 0).ToString();
2022-01-18 20:20:10 +08:00
#endregion
#region 方法二 :Short数组转换为int数据 20201014 孙亚龙
//相较而言 使用位移的算法效率更高一些。
2022-05-13 14:55:10 +08:00
// value = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
2022-05-09 13:22:39 +08:00
2022-01-18 20:20:10 +08:00
#endregion
2021-06-05 14:17:30 +08:00
}
else
{
2022-05-09 13:22:39 +08:00
value = readBytArr_D[plcItemDeviationAddress].ToString();
2021-06-05 14:17:30 +08:00
}
}
}
2022-05-09 13:22:39 +08:00
if (value != "")
{
if (item.PlcItemValue != value)
{
haschange = true;
2022-05-10 15:21:13 +08:00
item.PlcItemValue = value;
2022-05-09 13:22:39 +08:00
}
2022-05-10 15:21:13 +08:00
2022-05-09 13:22:39 +08:00
}
2021-06-05 14:17:30 +08:00
}
}
2022-05-09 08:59:35 +08:00
string priStr = "D";
int index = 10;
Global.CmWarningLogMd_List.Clear();
for (var i = 0; i < 9; i++)
{
var plcitem = $"{priStr}{index + i}";
if (tmpReadAlarmBytArr_D[i] != 0)
{
var wanglist = Global.CheckWarng(tmpReadAlarmBytArr_D[i], plcitem);
Global.CmWarningLogMd_List.AddRange(wanglist);
}
}
if (Global.CmWarningLogMd_List.Count() > 0)
{
Global.ShowMainFrmAlarmMsg(true, EventArgs.Empty);
}
else
{
Global.ShowMainFrmAlarmMsg(false, EventArgs.Empty);
}
2022-05-09 13:22:39 +08:00
2022-05-09 08:59:35 +08:00
// HandleWaringLog(tmpReadAlarmBytArr_D);
2022-01-18 20:20:10 +08:00
}
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLog($"读取D 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
2022-05-09 08:59:35 +08:00
2021-06-05 14:17:30 +08:00
//报警信息记录
private void HandleWaringLog(ushort[] tmpReadAlarmBytArr_D)
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
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++)
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
if (tmpCharArr[j] == '1')
{//当前报警
2022-05-06 12:28:16 +08:00
Warning tmpMd = Global.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
2021-06-05 14:17:30 +08:00
if (tmpMd != null)
{
2022-05-06 12:28:16 +08:00
WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == tmpMd.PlcItemCode);
2021-06-05 14:17:30 +08:00
if (warnLogTmp != null)
2022-01-18 20:20:10 +08:00
{//找到报警项,并增加版本
2021-06-05 14:17:30 +08:00
warnLogTmp.UpdateTime = DateTime.Now;
warnLogTmp.DataVersion += 1;
}
else
2022-01-18 20:20:10 +08:00
{//新报警增加记录
2022-05-06 12:28:16 +08:00
Global.CmWarningLogMd_List.Add(new WarningLog
2021-06-05 14:17:30 +08:00
{
WarningCode = tmpMd.WarningCode,
WarningName = tmpMd.WarningName,
WarningType = tmpMd.WarningType,
WarningMessage = tmpMd.WarningMessage,
WarningTime = DateTime.Now,
2022-01-18 20:20:10 +08:00
PlcItemCode = tmpMd.PlcItemCode,
2021-12-31 19:53:53 +08:00
WaringStatus = 0,
2021-06-05 14:17:30 +08:00
WarningHandleResult = 0,
CreateBy = 1,
CreateTime = DateTime.Now,
UpdateBy = 1,
UpdateTime = DateTime.Now,
DataVersion = 0,
});
}
}
#region 已删除
2022-01-18 20:20:10 +08:00
// 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 = $@"
2021-06-05 14:17:30 +08:00
// 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
2022-01-18 20:20:10 +08:00
// 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"]}')
2021-06-05 14:17:30 +08:00
// ";
2022-01-18 20:20:10 +08:00
// LeadDbHelperSQLite.ExecuteSql(sql1);
// string sql2 = $@"
2021-06-05 14:17:30 +08:00
//UPDATE CM_WARNING_LOG set UPDATE_TIME = datetime('now', 'localtime'),DATA_VERSION = DATA_VERSION + 1
2022-01-18 20:20:10 +08:00
// 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"]}';
2021-06-05 14:17:30 +08:00
// ";
2022-01-18 20:20:10 +08:00
// LeadDbHelperSQLite.ExecuteSql(sql2);
// }
2021-06-05 14:17:30 +08:00
2022-01-18 20:20:10 +08:00
#endregion
}
else
{//当前报警项没有报警
2022-05-06 12:28:16 +08:00
Warning tmpMd = Global.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
2022-01-18 20:20:10 +08:00
if (tmpMd != null)
{//之前有报警,则结束当前报警项
2022-05-06 12:28:16 +08:00
WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == tmpMd.PlcItemCode);
2022-01-18 20:20:10 +08:00
if (warnLogTmp != null)
{
warnLogTmp.UpdateTime = DateTime.Now;
warnLogTmp.DataVersion += 1;
warnLogTmp.WaringStatus = 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
2021-06-05 14:17:30 +08:00
}
}
2022-01-18 20:20:10 +08:00
}
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)
2021-06-05 14:17:30 +08:00
{
2022-05-06 12:28:16 +08:00
WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.PlcItemCode == $"{priStr + (index + i)}");
2022-01-18 20:20:10 +08:00
if (warnLogTmp != null)
{
warnLogTmp.UpdateTime = DateTime.Now;
warnLogTmp.DataVersion += 1;
warnLogTmp.WaringStatus = 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
2022-05-06 12:28:16 +08:00
Warning tmpMd = Global.BasWarningMd_List.Find(x => string.IsNullOrEmpty(x.PlcItemCode) && x.WarningCode == tmpReadAlarmBytArr_D[i].ToString() && x.WarningType == "2");
2022-01-18 20:20:10 +08:00
if (tmpMd != null)
{
2022-05-06 12:28:16 +08:00
WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == $"{priStr + (index + i)}");
2022-01-18 20:20:10 +08:00
if (warnLogTmp != null)
{
warnLogTmp.UpdateTime = DateTime.Now;
warnLogTmp.DataVersion += 1;
}
else
{
2022-05-06 12:28:16 +08:00
Global.CmWarningLogMd_List.Add(new WarningLog
2022-01-18 20:20:10 +08:00
{
WarningCode = tmpMd.WarningCode,
WarningName = tmpMd.WarningName,
WarningType = tmpMd.WarningType,
WarningMessage = tmpMd.WarningMessage,
WarningTime = DateTime.Now,
PlcItemCode = $"{priStr + (index + i)}",
WaringStatus = 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
2021-06-05 14:17:30 +08:00
}
}
2022-01-18 20:20:10 +08:00
else if (i > 9)
{
break;
}
}
2021-06-05 14:17:30 +08:00
2022-05-06 12:28:16 +08:00
List<WarningLog> tmpLst = Global.CmWarningLogMd_List.FindAll(x => x.WarningHandleResult == 1 && x.WaringStatus == 1);
2022-01-18 20:20:10 +08:00
if (tmpLst.Count >= 10)
{
foreach (WarningLog item in tmpLst)
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
string sql1 = $@"
2021-06-05 14:17:30 +08:00
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}',
2021-12-31 19:53:53 +08:00
'{item.WaringStatus}',
2021-06-05 14:17:30 +08:00
'{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}'
);
";
2022-01-18 20:20:10 +08:00
LeadDbHelperSQLite.ExecuteSql(sql1);
2021-06-05 14:17:30 +08:00
}
2022-05-06 12:28:16 +08:00
Global.CmWarningLogMd_List.RemoveAll(x => x.WarningHandleResult == 1 && x.WaringStatus == 1);
2022-01-18 20:20:10 +08:00
}
2021-06-05 14:17:30 +08:00
2022-05-06 12:28:16 +08:00
if (Global.CmWarningLogMd_List.FindAll(x => x.WarningHandleResult == 0 && x.WaringStatus == 0).Count > 0)
2022-01-18 20:20:10 +08:00
{
//frmMainNew.Show_ptb_Alarm(true);
2022-05-06 12:28:16 +08:00
Global.ShowMainFrmAlarmMsg(true, EventArgs.Empty);
2022-01-18 20:20:10 +08:00
}
else
{
//frmMainNew.Show_ptb_Alarm(false);
2022-05-06 12:28:16 +08:00
Global.ShowMainFrmAlarmMsg(false, EventArgs.Empty);
2022-01-18 20:20:10 +08:00
}
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLog($"处理设备报警信息时发生异常:【{ex.Message}】", LogTypeEum.Err);
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
2022-05-09 13:22:39 +08:00
public void RefreshPlcItem_X(ref bool haschange)
2021-06-05 14:17:30 +08:00
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
2022-05-10 15:21:13 +08:00
List<PlcItem> changeItems = new List<PlcItem>();
2022-01-18 20:20:10 +08:00
bool[] readBytArr_X = master.ReadInputs(slaveAddress, 1024, 50);
if (readBytArr_X.Length > 0)
//if(true)
{
2022-05-06 15:23:30 +08:00
if (PlcItemMd_X_List != null && PlcItemMd_X_List.Count > 0)
2021-06-05 14:17:30 +08:00
{
2022-05-06 15:23:30 +08:00
foreach (PlcItem item in PlcItemMd_X_List)
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
string value = "";
2022-01-18 20:20:10 +08:00
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
string PlcItemId = item.PlcItemId.ToString();
string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
2022-05-09 13:22:39 +08:00
value = readBytArr_X[plcItemDeviationAddress].ToString();
if (value != null)
{
if (item.PlcItemValue != value)
{
haschange = true;
item.PlcItemValue = value;
2022-05-10 15:21:13 +08:00
// this.DoShowX_Y_Status(item);
changeItems.Add(item);
// Global.ShowX_Y_Status(item, new EventArgs());
2022-05-09 13:22:39 +08:00
Global.ShowSensorStatus(item, new EventArgs());
}
}
2021-06-05 14:17:30 +08:00
2022-01-18 20:20:10 +08:00
//Random r = new Random();
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
2021-06-05 14:17:30 +08:00
2022-05-09 13:22:39 +08:00
2021-06-05 14:17:30 +08:00
}
}
2022-05-10 15:21:13 +08:00
this.DoShowX_Y_Status(changeItems);
2021-06-05 14:17:30 +08:00
}
}
2022-05-05 09:38:28 +08:00
#if RELEASE
2022-01-18 20:20:10 +08:00
}
2021-06-05 14:17:30 +08:00
catch (Exception ex)
{
LogHelper.WriteLog($"读取X 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
2022-05-09 13:22:39 +08:00
public void RefreshPlcItem_Y(ref bool haschange)
2021-06-05 14:17:30 +08:00
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
2022-05-10 15:21:13 +08:00
List<PlcItem> changeItems = new List<PlcItem>();
2022-01-18 20:20:10 +08:00
bool[] readBytArr_Y = master.ReadCoils(slaveAddress, 1280, 50);
if (readBytArr_Y.Length > 0)
//if(true)
{
2022-05-06 15:23:30 +08:00
if (PlcItemMd_Y_List != null && PlcItemMd_Y_List.Count > 0)
2021-06-05 14:17:30 +08:00
{
2022-05-06 15:23:30 +08:00
foreach (PlcItem item in PlcItemMd_Y_List)
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
string value = "";
2022-01-18 20:20:10 +08:00
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
string PlcItemId = item.PlcItemId.ToString();
string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
2022-05-09 13:22:39 +08:00
value = readBytArr_Y[plcItemDeviationAddress].ToString();
if (value != null)
{
if (item.PlcItemValue != value)
{
2022-05-10 15:21:13 +08:00
haschange = true;
2022-05-09 13:22:39 +08:00
item.PlcItemValue = value;
2022-05-10 15:21:13 +08:00
changeItems.Add(item);
// this.DoShowX_Y_Status(item);
// Global.ShowX_Y_Status(item, new EventArgs());
2022-05-09 13:22:39 +08:00
}
}
2022-01-18 20:20:10 +08:00
//Random r = new Random();
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
2021-06-05 14:17:30 +08:00
}
}
2022-05-10 15:21:13 +08:00
this.DoShowX_Y_Status(changeItems);
2021-06-05 14:17:30 +08:00
}
}
2022-05-05 09:38:28 +08:00
#if RELEASE
2022-01-18 20:20:10 +08:00
}
2021-06-05 14:17:30 +08:00
catch (Exception ex)
{
LogHelper.WriteLog($"读取Y 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
2022-01-19 09:31:47 +08:00
2022-05-06 15:23:30 +08:00
2022-05-06 12:28:16 +08:00
public object ReadPlcItem(string plcItemCode)
{
2022-05-06 15:23:30 +08:00
PlcItem item = PlcItemMdList.Find(x => x.PlcItemCode == plcItemCode);
2022-05-06 12:28:16 +08:00
if (item == null)
{
return null;
}
2022-05-10 15:21:13 +08:00
if (item.PlcItemDataType == PlcItemTypeDesc.D)
2022-05-06 12:28:16 +08:00
{
if (string.IsNullOrEmpty(item.PlcItemValue))
{
return 0;
}
else
{
2022-05-13 09:08:42 +08:00
if (item.PlcItemDataLength == 2)
{
return int.Parse(item.PlcItemValue);
}
else
{
return short.Parse(item.PlcItemValue);
}
2022-05-06 12:28:16 +08:00
}
}
else
{
if (string.IsNullOrEmpty(item.PlcItemValue))
{
return false;
}
else
{
return bool.Parse(item.PlcItemValue);
}
}
}
2021-06-05 14:17:30 +08:00
public object ReadPlcItem(string plcItemCode, PlcItemTypeDesc itemType)
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
object retObj;
switch (itemType)
{
case PlcItemTypeDesc.M:
2022-01-19 09:31:47 +08:00
2022-05-06 15:23:30 +08:00
PlcItem item_M = PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
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:
2022-05-06 15:23:30 +08:00
PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
if (item_D == null)
{
return null;
}
if (string.IsNullOrEmpty(item_D.PlcItemValue))
{
retObj = 0;
}
else
{
2022-05-13 09:08:42 +08:00
if (item_D.PlcItemDataLength == 2)
{
retObj = int.Parse(item_D.PlcItemValue);
}
else
{
retObj = short.Parse(item_D.PlcItemValue);
}
2022-01-18 20:20:10 +08:00
}
break;
case PlcItemTypeDesc.X:
2022-05-06 15:23:30 +08:00
PlcItem item_X = PlcItemMd_X_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
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:
2022-05-06 15:23:30 +08:00
PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
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;
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLog($"读取测试:【{plcItemCode}】", LogTypeEum.Err);
LogHelper.WriteLog($"读取【{itemType.ToString()}】类型PLC变量【{plcItemCode}】时发生异常:【{ex.Message}】", LogTypeEum.Err);
return null;
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
public bool WritePlcItem(string plcItemCode, string writeValue, PlcItemTypeDesc itemType)
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
switch (itemType)
{
case PlcItemTypeDesc.M:
2022-05-06 15:23:30 +08:00
PlcItem item_M = PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
if (item_M == null)
{
2021-06-05 14:17:30 +08:00
return false;
2022-01-18 20:20:10 +08:00
}
master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue));
return true;
case PlcItemTypeDesc.Y:
2022-05-06 15:23:30 +08:00
PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
if (item_Y == null)
{
2021-06-05 14:17:30 +08:00
return false;
2022-01-18 20:20:10 +08:00
}
master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue));
return true;
case PlcItemTypeDesc.D:
2022-05-06 15:23:30 +08:00
PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
2022-01-18 20:20:10 +08:00
if (item_D == null)
{
return false;
}
2022-05-13 14:55:10 +08:00
if (item_D.PlcItemDataLength == 2)
{
var _value = int.Parse(writeValue);
var bytes = BitConverter.GetBytes(_value);
var v1 = BitConverter.ToUInt16(bytes, 0);
var v2 = BitConverter.ToUInt16(bytes, 2);
master.WriteMultipleRegisters(slaveAddress, Convert.ToUInt16(item_D.PlcItemModbusAddress), new ushort[] { v1, v2 });
}
else
{
master.WriteSingleRegister(slaveAddress, Convert.ToUInt16(item_D.PlcItemModbusAddress), ushort.Parse(writeValue));
}
2022-01-18 20:20:10 +08:00
return true;
case PlcItemTypeDesc.X:
return false;
default:
return false;
}
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
}
catch (Exception ex)
{
2022-01-18 20:20:10 +08:00
LogHelper.WriteLog($"写入【{itemType}】类型PLC变量【{plcItemCode}】的值【{writeValue}】时发生异常:【{ex.Message}】", LogTypeEum.Err);
2021-06-05 14:17:30 +08:00
return false;
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
private int wcsToPlcHeartDataNew = 0;
private DateTime HeartbeatTime = new DateTime();
2022-05-05 09:38:28 +08:00
private int ReConnNum = 1;
2021-12-27 21:39:45 +08:00
/// <summary>
/// 心跳处理
/// </summary>
2021-06-05 14:17:30 +08:00
public void HeartbeatDetection()
{
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
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)
2021-06-05 14:17:30 +08:00
{
wcsToPlcHeartDataNew = 1;
2022-01-18 20:20:10 +08:00
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!");
2021-06-05 14:17:30 +08:00
}
2022-01-18 20:20:10 +08:00
}
else if (wcsToPlcHeartDataNew == 0 && plcToWcsHeartData != 0)
{
if (plcToWcsHeartData == wcsToPlcHeartData)
2021-06-05 14:17:30 +08:00
{
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!");
}
}
2022-01-18 20:20:10 +08:00
}
else
{
WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartDataNew.ToString(), PlcItemTypeDesc.D);
}
2021-12-27 09:51:57 +08:00
2022-01-19 14:59:16 +08:00
#if UseHeartbeat
2021-12-27 09:51:57 +08:00
2022-01-13 10:43:45 +08:00
2022-01-19 14:59:16 +08:00
if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时
{
isConn = false;
//报警处理。
frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
if (ReConnNum % 5 == 0)
2022-01-13 10:43:45 +08:00
{
2022-01-19 14:59:16 +08:00
ClosePlcConn();
PlcInit();
2022-01-13 10:43:45 +08:00
}
2022-01-19 14:59:16 +08:00
ReConnNum++;
}
2021-12-27 09:51:57 +08:00
#endif
2022-01-18 20:20:10 +08:00
2022-05-05 09:38:28 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
}
2022-01-18 20:20:10 +08:00
catch (Exception ex)
2021-06-05 14:17:30 +08:00
{
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
}
}
2022-05-11 15:20:00 +08:00
2021-06-05 14:17:30 +08:00
}