1354 lines
60 KiB
C#
1354 lines
60 KiB
C#
//使用本地调式
|
||
|
||
#if DEBUG
|
||
#define USE_LOCAL_TCP_DEBUG
|
||
#endif
|
||
//启用心跳检查
|
||
|
||
#if !USE_LOCAL_TCP_DEBUG
|
||
#define UseHeartbeat
|
||
#endif
|
||
using CleanMachine.Model;
|
||
using CleanMachine.Model.BAS;
|
||
using CleanMachine.Model.CM;
|
||
using LeadDataBaseUtility;
|
||
|
||
using NModbus;
|
||
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;
|
||
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 PlcOperationHelper()
|
||
{
|
||
_synchronization = SynchronizationContext.Current;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 读取完成的委托
|
||
/// </summary>
|
||
public delegate void ReadFinish();
|
||
|
||
public event ReadFinish OnReadFinish;
|
||
|
||
public void DoOnReadFinish(object data)
|
||
{
|
||
|
||
OnReadFinish?.Invoke();
|
||
|
||
}
|
||
|
||
public delegate void ShowX_Y_Status(IEnumerable<PlcItem> plcItem);
|
||
|
||
|
||
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);
|
||
// OnShowX_Y_Status?.Invoke(plcItem);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
private SynchronizationContext _synchronization;
|
||
|
||
|
||
private byte slaveAddress = byte.Parse(ConfigurationManager.AppSettings["SlaveAddress"]);
|
||
|
||
|
||
private bool _isConn = false;
|
||
|
||
|
||
public bool IsConn
|
||
{
|
||
get
|
||
{
|
||
return this._isConn;
|
||
}
|
||
set { this._isConn = value; }
|
||
}
|
||
private static IModbusMaster master;
|
||
//NModbus.IO.IStreamResource port;
|
||
#if USE_LOCAL_TCP_DEBUG
|
||
private static System.Net.Sockets.TcpClient port;
|
||
#else
|
||
private static SerialPort port;
|
||
#endif
|
||
|
||
|
||
|
||
public Dictionary<string, PlcItem> PlcItemMdList { get; set; } = new Dictionary<string, PlcItem>();
|
||
public Dictionary<string, PlcItem> PlcItemMd_D_List { get; set; } = new Dictionary<string, PlcItem>();
|
||
|
||
public Dictionary<string, PlcItem> PlcItemMd_M_List { get; set; } = new Dictionary<string, PlcItem>();
|
||
|
||
public Dictionary<string, PlcItem> PlcItemMd_X_List { get; set; } = new Dictionary<string, PlcItem>();
|
||
|
||
public Dictionary<string, PlcItem> PlcItemMd_Y_List { get; set; } = new Dictionary<string, PlcItem>();
|
||
|
||
|
||
public void StartRead()
|
||
{
|
||
Thread th = new Thread(RefreshPlcItem)
|
||
{
|
||
IsBackground = true
|
||
};
|
||
th.Start();
|
||
|
||
}
|
||
|
||
|
||
public bool readIO { get; set; } = false;
|
||
|
||
|
||
|
||
// public System.Threading.AutoResetEvent thEvent = new System.Threading.AutoResetEvent(false);
|
||
|
||
|
||
|
||
|
||
private void RefreshPlcItem()
|
||
{
|
||
Stopwatch stopWatch = new Stopwatch();
|
||
while (Global.SysThIsRunFlag)
|
||
{
|
||
|
||
|
||
stopWatch.Restart();
|
||
var haschange = false;
|
||
RefreshPlcItem_M(ref haschange);
|
||
RefreshPlcItem_D(ref haschange);
|
||
if (readIO)
|
||
{
|
||
RefreshPlcItem_X(ref haschange);
|
||
RefreshPlcItem_Y(ref haschange);
|
||
|
||
}
|
||
stopWatch.Stop();
|
||
|
||
TimeSpan ts = stopWatch.Elapsed;
|
||
// Console.WriteLine($"读取PLC时间:{ts.Milliseconds}", LogTypeEum.Run);
|
||
|
||
if (haschange)
|
||
{
|
||
if (_synchronization != null)
|
||
{
|
||
_synchronization.Send(this.DoOnReadFinish, null);
|
||
}
|
||
}
|
||
|
||
HeartbeatDetection();
|
||
// thEvent.Set();
|
||
// ShowWarningMsg();
|
||
int sleeptime = 500 - ts.Milliseconds;
|
||
if (sleeptime < 0)
|
||
{
|
||
sleeptime = 0;
|
||
}
|
||
Thread.Sleep(sleeptime);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public bool PlcInit()
|
||
{
|
||
try
|
||
{
|
||
|
||
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"]);
|
||
|
||
|
||
//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();
|
||
|
||
var adaptet = new NModbus.IO.TcpClientAdapter(port);
|
||
#else
|
||
port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
|
||
var adaptet = new NModbus.Serial.SerialPortAdapter(port);
|
||
adaptet.ReadTimeout = 200;
|
||
#endif
|
||
|
||
var factory = new ModbusFactory();
|
||
if (modbusPattern == 0)
|
||
{
|
||
|
||
|
||
master = factory.CreateRtuMaster(adaptet);
|
||
}
|
||
else
|
||
{
|
||
master = factory.CreateAsciiMaster(adaptet);
|
||
}
|
||
|
||
#if USE_LOCAL_TCP_DEBUG
|
||
port.Connect("10.20.88.95", 5002);
|
||
this._isConn = true;
|
||
return true;
|
||
#else
|
||
|
||
port.Open();
|
||
return port.IsOpen;
|
||
|
||
#endif
|
||
|
||
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"初始化PLC连接时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
IsConn = false;
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化PLC
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool PlcInit(IEnumerable<PlcItem> items)
|
||
{
|
||
|
||
foreach (var plcitem in items)
|
||
{
|
||
switch (plcitem.PlcItemDataType)
|
||
{
|
||
case PlcItemTypeDesc.M:
|
||
PlcItemMd_M_List.Add(plcitem.PlcItemCode, plcitem);
|
||
break;
|
||
|
||
case PlcItemTypeDesc.D:
|
||
PlcItemMd_D_List.Add(plcitem.PlcItemCode, plcitem);
|
||
break;
|
||
case PlcItemTypeDesc.X:
|
||
PlcItemMd_X_List.Add(plcitem.PlcItemCode, plcitem);
|
||
break;
|
||
case PlcItemTypeDesc.Y:
|
||
PlcItemMd_Y_List.Add(plcitem.PlcItemCode, plcitem);
|
||
break;
|
||
}
|
||
PlcItemMdList.Add(plcitem.PlcItemCode, plcitem);
|
||
}
|
||
|
||
|
||
return PlcInit();
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
public void ClosePlcConn()
|
||
{
|
||
#if USE_LOCAL_TCP_DEBUG
|
||
if (port.Connected)
|
||
#else
|
||
if (port.IsOpen)
|
||
#endif
|
||
{
|
||
port.Close();
|
||
master = null;
|
||
IsConn = false;
|
||
}
|
||
}
|
||
public void RefreshPlcItem_M(ref bool haschange)
|
||
{
|
||
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#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)
|
||
{
|
||
if (PlcItemMd_M_List != null && PlcItemMd_M_List.Count > 0)
|
||
{
|
||
foreach (PlcItem item in PlcItemMd_M_List.Values)
|
||
{
|
||
string value = "";
|
||
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);
|
||
value = readBytArr_M[plcItemDeviationAddress].ToString();
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
//string PlcItemId = item.PlcItemId.ToString();
|
||
//string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
value = readBytArr_M1[plcItemDeviationAddress].ToString();
|
||
|
||
}
|
||
}
|
||
if (value != "")
|
||
{
|
||
if (item.PlcItemValue != value)
|
||
{
|
||
haschange = true;
|
||
Console.WriteLine(item.PlcItemCode + "数据变化");
|
||
}
|
||
item.PlcItemValue = value;
|
||
Global.ShowSensorStatus(item, new EventArgs());
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取M 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
#endif
|
||
}
|
||
public void RefreshPlcItem_D(ref bool haschange)
|
||
{
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#endif
|
||
|
||
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)
|
||
{
|
||
if (PlcItemMd_D_List != null && PlcItemMd_D_List.Count > 0)
|
||
{
|
||
foreach (PlcItem item in PlcItemMd_D_List.Values)
|
||
{
|
||
var value = "";
|
||
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)
|
||
{
|
||
|
||
value = tmpReadAlarmBytArr_D[plcItemDeviationAddress].ToString();
|
||
}
|
||
else
|
||
{
|
||
|
||
if (item.PlcItemDataLength == 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 +
|
||
value = BitConverter.ToInt32(tmpByt_3, 0).ToString();
|
||
#endregion
|
||
|
||
#region 方法二 :Short数组转换为int数据 20201014 孙亚龙
|
||
//相较而言 使用位移的算法效率更高一些。
|
||
// value = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
|
||
|
||
|
||
#endregion
|
||
|
||
}
|
||
else
|
||
{
|
||
value = readBytArr_D[plcItemDeviationAddress].ToString();
|
||
}
|
||
}
|
||
}
|
||
|
||
if (value != "")
|
||
{
|
||
if (item.PlcItemValue != value)
|
||
{
|
||
if (item.PlcItemCode != "PlcHeartbeatWriteData" && item.PlcItemCode != "PlcHeartbeatReadData")
|
||
{
|
||
haschange = true;
|
||
Console.WriteLine(item.PlcItemCode + "数据变化");
|
||
}
|
||
item.PlcItemValue = value;
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
string priStr = "D";
|
||
int index = 10;
|
||
bool alarmChange = false;
|
||
|
||
|
||
//Global.CmWarningLogMd_List.Clear();
|
||
for (var i = 0; i < 9; i++)
|
||
{
|
||
var plcitem = $"{priStr}{index + i}";
|
||
if (lastAlarm[i] != tmpReadAlarmBytArr_D[i])
|
||
{
|
||
|
||
foreach (var W in CmWarningLogMd_List)
|
||
{
|
||
if (W.Value.PlcItemCode == plcitem)
|
||
{
|
||
W.Value.WaringStatus = 1;
|
||
}
|
||
}
|
||
if (tmpReadAlarmBytArr_D[i] != 0)
|
||
{
|
||
|
||
|
||
|
||
var wanglist = Global.CheckWarng(tmpReadAlarmBytArr_D[i], plcitem);
|
||
foreach (var wang in wanglist)
|
||
{
|
||
if (CmWarningLogMd_List.TryGetValue(wang.PlcItemCode + wang.WarningCode, out var warning))
|
||
{
|
||
wang.WaringStatus = 0;
|
||
}
|
||
else
|
||
{
|
||
//添加
|
||
CmWarningLogMd_List.Add(wang.PlcItemCode + wang.WarningCode, wang);
|
||
// new CleanMachine.BLL.CM.WarningLogBLL().Add(wang);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|
||
lastAlarm[i] = tmpReadAlarmBytArr_D[i];
|
||
alarmChange = true;
|
||
}
|
||
}
|
||
|
||
var keys = CmWarningLogMd_List.Keys.ToList();
|
||
foreach (var key in keys)
|
||
{
|
||
if (CmWarningLogMd_List[key].WaringStatus == 1)
|
||
{
|
||
// new CleanMachine.BLL.CM.WarningLogBLL().Update(Global.CmWarningLogMd_List[key]);
|
||
CmWarningLogMd_List.Remove(key);
|
||
}
|
||
}
|
||
|
||
if (alarmChange)
|
||
{
|
||
ShowMainFrmAlarmMsg();
|
||
}
|
||
|
||
//if (Global.CmWarningLogMd_List.Count() > 0)
|
||
//{
|
||
// ShowMainFrmAlarmMsg(true, EventArgs.Empty);
|
||
//}
|
||
//else
|
||
//{
|
||
// ShowMainFrmAlarmMsg(false, EventArgs.Empty);
|
||
//}
|
||
|
||
|
||
// HandleWaringLog(tmpReadAlarmBytArr_D);
|
||
}
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取D 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
ushort[] lastAlarm = new ushort[9] { 0, 0, 0, 0, 0, 0, 0, 0, 0, };
|
||
|
||
|
||
public Dictionary<string, WarningLog> CmWarningLogMd_List { get; set; } = new Dictionary<string, WarningLog>();
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public delegate void AlarmChange();
|
||
|
||
public event AlarmChange OnShowMainFrmAlarmMsg;
|
||
|
||
public void DoShowAlarm(object param)
|
||
{
|
||
if (!eventAlarmLock)
|
||
{
|
||
OnShowMainFrmAlarmMsg?.Invoke();
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
public bool eventAlarmLock = false;
|
||
public void ShowMainFrmAlarmMsg()
|
||
{
|
||
if (_synchronization != null)
|
||
{
|
||
_synchronization.Post(DoShowAlarm, null);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
//报警信息记录
|
||
// private void HandleWaringLog(ushort[] tmpReadAlarmBytArr_D)
|
||
// {
|
||
//#if RELEASE
|
||
// try
|
||
// {
|
||
//#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++)
|
||
// {
|
||
// if (tmpCharArr[j] == '1')
|
||
// {//当前报警
|
||
// Warning tmpMd = Global.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
|
||
// if (tmpMd != null)
|
||
// {
|
||
// WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == tmpMd.PlcItemCode);
|
||
// if (warnLogTmp != null)
|
||
// {//找到报警项,并增加版本
|
||
// warnLogTmp.UpdateTime = DateTime.Now;
|
||
// warnLogTmp.DataVersion += 1;
|
||
// }
|
||
// else
|
||
// {//新报警增加记录
|
||
// Global.CmWarningLogMd_List.Add(new WarningLog
|
||
// {
|
||
// WarningCode = tmpMd.WarningCode,
|
||
// WarningName = tmpMd.WarningName,
|
||
// WarningType = tmpMd.WarningType,
|
||
// WarningMessage = tmpMd.WarningMessage,
|
||
// WarningTime = DateTime.Now,
|
||
// PlcItemCode = tmpMd.PlcItemCode,
|
||
// 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 = '{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
|
||
// {//当前报警项没有报警
|
||
// Warning tmpMd = Global.BasWarningMd_List.Find(x => x.PlcItemCode == $"{priStr + (index + i)}" && x.WarningCode == $"{priWarnCode + j}" && x.WarningType == "1");
|
||
// if (tmpMd != null)
|
||
// {//之前有报警,则结束当前报警项
|
||
// WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == tmpMd.PlcItemCode);
|
||
// 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
|
||
// }
|
||
// }
|
||
// }
|
||
// 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)
|
||
// {
|
||
// WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.PlcItemCode == $"{priStr + (index + i)}");
|
||
// 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
|
||
// Warning tmpMd = Global.BasWarningMd_List.Find(x => string.IsNullOrEmpty(x.PlcItemCode) && x.WarningCode == tmpReadAlarmBytArr_D[i].ToString() && x.WarningType == "2");
|
||
// if (tmpMd != null)
|
||
// {
|
||
// WarningLog warnLogTmp = Global.CmWarningLogMd_List.Find(y => y.WaringStatus == 0 && y.WarningHandleResult == 0 && y.WarningCode == tmpMd.WarningCode && y.PlcItemCode == $"{priStr + (index + i)}");
|
||
// if (warnLogTmp != null)
|
||
// {
|
||
// warnLogTmp.UpdateTime = DateTime.Now;
|
||
// warnLogTmp.DataVersion += 1;
|
||
// }
|
||
// else
|
||
// {
|
||
// Global.CmWarningLogMd_List.Add(new WarningLog
|
||
// {
|
||
// 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
|
||
|
||
// }
|
||
// }
|
||
// else if (i > 9)
|
||
// {
|
||
// break;
|
||
// }
|
||
// }
|
||
|
||
// List<WarningLog> tmpLst = Global.CmWarningLogMd_List.Values.FindAll(x => x.WarningHandleResult == 1 && x.WaringStatus == 1);
|
||
// if (tmpLst.Count >= 10)
|
||
// {
|
||
// foreach (WarningLog 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.WaringStatus}',
|
||
// '{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.Execute(sql1);
|
||
// }
|
||
// Global.CmWarningLogMd_List.RemoveAll(x => x.WarningHandleResult == 1 && x.WaringStatus == 1);
|
||
// }
|
||
|
||
// if (Global.CmWarningLogMd_List.FindAll(x => x.WarningHandleResult == 0 && x.WaringStatus == 0).Count > 0)
|
||
// {
|
||
// //frmMainNew.Show_ptb_Alarm(true);
|
||
// ShowMainFrmAlarmMsg();
|
||
// }
|
||
// else
|
||
// {
|
||
// //frmMainNew.Show_ptb_Alarm(false);
|
||
// ShowMainFrmAlarmMsg();
|
||
// }
|
||
//#if RELEASE
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// LogHelper.WriteLog($"处理设备报警信息时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
// }
|
||
//#endif
|
||
// }
|
||
|
||
public void RefreshPlcItem_X(ref bool haschange)
|
||
{
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#endif
|
||
List<PlcItem> changeItems = new List<PlcItem>();
|
||
bool[] readBytArr_X = master.ReadInputs(slaveAddress, 1024, 50);
|
||
if (readBytArr_X.Length > 0)
|
||
//if(true)
|
||
{
|
||
if (PlcItemMd_X_List != null && PlcItemMd_X_List.Count > 0)
|
||
{
|
||
foreach (PlcItem item in PlcItemMd_X_List.Values)
|
||
{
|
||
string value = "";
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
value = readBytArr_X[plcItemDeviationAddress].ToString();
|
||
if (value != null)
|
||
{
|
||
if (item.PlcItemValue != value)
|
||
{
|
||
haschange = true;
|
||
item.PlcItemValue = value;
|
||
// this.DoShowX_Y_Status(item);
|
||
changeItems.Add(item);
|
||
// Global.ShowX_Y_Status(item, new EventArgs());
|
||
Global.ShowSensorStatus(item, new EventArgs());
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
//Random r = new Random();
|
||
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
|
||
|
||
|
||
}
|
||
}
|
||
|
||
this.DoShowX_Y_Status(changeItems);
|
||
}
|
||
}
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取X 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
public void RefreshPlcItem_Y(ref bool haschange)
|
||
{
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#endif
|
||
|
||
|
||
List<PlcItem> changeItems = new List<PlcItem>();
|
||
bool[] readBytArr_Y = master.ReadCoils(slaveAddress, 1280, 50);
|
||
if (readBytArr_Y.Length > 0)
|
||
//if(true)
|
||
{
|
||
if (PlcItemMd_Y_List != null && PlcItemMd_Y_List.Count > 0)
|
||
{
|
||
foreach (PlcItem item in PlcItemMd_Y_List.Values)
|
||
{
|
||
string value = "";
|
||
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
|
||
{
|
||
string PlcItemId = item.PlcItemId.ToString();
|
||
string plcdatatype = item.PlcItemDataType.ToString();
|
||
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
|
||
value = readBytArr_Y[plcItemDeviationAddress].ToString();
|
||
|
||
if (value != null)
|
||
{
|
||
if (item.PlcItemValue != value)
|
||
{
|
||
haschange = true;
|
||
item.PlcItemValue = value;
|
||
changeItems.Add(item);
|
||
// this.DoShowX_Y_Status(item);
|
||
// Global.ShowX_Y_Status(item, new EventArgs());
|
||
}
|
||
}
|
||
//Random r = new Random();
|
||
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
|
||
}
|
||
}
|
||
|
||
this.DoShowX_Y_Status(changeItems);
|
||
}
|
||
}
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取Y 类型PLC变量时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
|
||
|
||
|
||
public object ReadPlcItem(string plcItemCode)
|
||
{
|
||
// PlcItem item = PlcItemMdList.Find(x => x.PlcItemCode == plcItemCode);
|
||
|
||
PlcItemMdList.TryGetValue(plcItemCode, out var item);
|
||
|
||
|
||
if (item == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (item.PlcItemDataType == PlcItemTypeDesc.D)
|
||
{
|
||
|
||
if (string.IsNullOrEmpty(item.PlcItemValue))
|
||
{
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
if (item.PlcItemDataLength == 2)
|
||
{
|
||
return int.Parse(item.PlcItemValue);
|
||
}
|
||
else
|
||
{
|
||
|
||
|
||
return short.Parse(item.PlcItemValue);
|
||
}
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
if (string.IsNullOrEmpty(item.PlcItemValue))
|
||
{
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
return bool.Parse(item.PlcItemValue);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
public object ReadPlcItem(string plcItemCode, PlcItemTypeDesc itemType)
|
||
{
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#endif
|
||
object retObj;
|
||
switch (itemType)
|
||
{
|
||
case PlcItemTypeDesc.M:
|
||
PlcItemMd_M_List.TryGetValue(plcItemCode, out var item_M);
|
||
|
||
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:
|
||
PlcItemMd_D_List.TryGetValue(plcItemCode, out var item_D);
|
||
// PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
|
||
if (item_D == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (string.IsNullOrEmpty(item_D.PlcItemValue))
|
||
{
|
||
retObj = 0;
|
||
}
|
||
else
|
||
{
|
||
if (item_D.PlcItemDataLength == 2)
|
||
{
|
||
retObj = int.Parse(item_D.PlcItemValue);
|
||
}
|
||
else
|
||
{
|
||
retObj = short.Parse(item_D.PlcItemValue);
|
||
}
|
||
|
||
}
|
||
break;
|
||
case PlcItemTypeDesc.X:
|
||
PlcItemMd_X_List.TryGetValue(plcItemCode, out var item_X);
|
||
//PlcItem item_X = 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:
|
||
PlcItemMd_Y_List.TryGetValue(plcItemCode, out var item_Y);
|
||
// PlcItem item_Y = 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;
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"读取测试:【{plcItemCode}】", LogTypeEum.Err);
|
||
LogHelper.WriteLog($"读取【{itemType.ToString()}】类型PLC变量【{plcItemCode}】时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
return null;
|
||
}
|
||
#endif
|
||
}
|
||
|
||
public bool WritePlcItem(string plcItemCode, string writeValue, PlcItemTypeDesc itemType)
|
||
{
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#endif
|
||
// if (!this.IsConn) return false;
|
||
switch (itemType)
|
||
{
|
||
case PlcItemTypeDesc.M:
|
||
PlcItemMd_M_List.TryGetValue(plcItemCode, out var item_M);
|
||
if (item_M == null)
|
||
{
|
||
return false;
|
||
}
|
||
master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue));
|
||
return true;
|
||
case PlcItemTypeDesc.Y:
|
||
PlcItemMd_Y_List.TryGetValue(plcItemCode, out var item_Y);
|
||
|
||
if (item_Y == null)
|
||
{
|
||
return false;
|
||
}
|
||
master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue));
|
||
return true;
|
||
case PlcItemTypeDesc.D:
|
||
PlcItemMd_D_List.TryGetValue(plcItemCode, out var item_D);
|
||
if (item_D == null)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
|
||
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));
|
||
}
|
||
|
||
|
||
|
||
return true;
|
||
case PlcItemTypeDesc.X:
|
||
return false;
|
||
default:
|
||
return false;
|
||
}
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog($"写入【{itemType}】类型PLC变量【{plcItemCode}】的值【{writeValue}】时发生异常:【{ex.Message}】", LogTypeEum.Err);
|
||
return false;
|
||
}
|
||
#endif
|
||
}
|
||
private int wcsToPlcHeartDataNew = 0;
|
||
private DateTime HeartbeatTime = new DateTime();
|
||
private int ReConnNum = 1;
|
||
/// <summary>
|
||
/// 心跳处理
|
||
/// </summary>
|
||
public void HeartbeatDetection()
|
||
{
|
||
#if RELEASE
|
||
try
|
||
{
|
||
#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)
|
||
{
|
||
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 UseHeartbeat
|
||
|
||
|
||
if (DateTime.Now > HeartbeatTime.AddSeconds(10))//心跳超时
|
||
{
|
||
IsConn = false;
|
||
//报警处理。
|
||
frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
|
||
if (ReConnNum % 5 == 0)
|
||
{
|
||
ClosePlcConn();
|
||
PlcInit();
|
||
}
|
||
ReConnNum++;
|
||
}
|
||
#endif
|
||
|
||
#if RELEASE
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
#endif
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
}
|