2022-01-19 14:59:16 +08:00
//使用本地调式
2022-05-05 09:38:28 +08:00
#if DEBUG
2022-05-27 15:47:18 +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-19 15:56:08 +08:00
//#if RELEASE
2022-05-27 15:47:18 +08:00
#define UseHeartbeat
2022-05-19 15:56:08 +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 ;
2022-05-17 09:25:44 +08:00
public bool IsConn
2021-06-05 14:17:30 +08:00
{
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-18 10:15:49 +08:00
public Dictionary < string , PlcItem > PlcItemMdList { get ; set ; } = new Dictionary < string , PlcItem > ( ) ;
public Dictionary < string , PlcItem > PlcItemMd_D_List { get ; set ; } = new Dictionary < string , PlcItem > ( ) ;
2022-05-06 15:23:30 +08:00
2022-05-18 10:15:49 +08:00
public Dictionary < string , PlcItem > PlcItemMd_M_List { get ; set ; } = new Dictionary < string , PlcItem > ( ) ;
2022-05-06 15:23:30 +08:00
2022-05-18 10:15:49 +08:00
public Dictionary < string , PlcItem > PlcItemMd_X_List { get ; set ; } = new Dictionary < string , PlcItem > ( ) ;
2022-05-06 15:23:30 +08:00
2022-05-18 10:15:49 +08:00
public Dictionary < string , PlcItem > PlcItemMd_Y_List { get ; set ; } = new Dictionary < string , PlcItem > ( ) ;
2022-05-06 15:23:30 +08:00
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
2022-05-17 16:26:35 +08:00
public bool readIO { get ; set ; } = false ;
2022-05-16 09:03:42 +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
2022-05-19 12:55:26 +08:00
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 ) ;
2022-05-16 09:03:42 +08:00
if ( readIO )
{
RefreshPlcItem_X ( ref haschange ) ;
RefreshPlcItem_Y ( ref haschange ) ;
2022-05-11 15:20:00 +08:00
2022-05-16 09:03:42 +08:00
}
2022-05-11 15:20:00 +08:00
stopWatch . Stop ( ) ;
2022-05-13 09:08:42 +08:00
TimeSpan ts = stopWatch . Elapsed ;
2022-05-19 12:55:26 +08:00
// Console.WriteLine($"读取PLC时间:{ts.Milliseconds}", LogTypeEum.Run);
2022-05-17 16:26:35 +08:00
2022-05-09 13:22:39 +08:00
if ( haschange )
{
if ( _synchronization ! = null )
{
2022-05-17 16:26:35 +08:00
_synchronization . Send ( this . DoOnReadFinish , null ) ;
2022-05-09 13:22:39 +08:00
}
}
2022-05-11 15:20:00 +08:00
2022-05-16 09:03:42 +08:00
HeartbeatDetection ( ) ;
2022-05-09 08:59:35 +08:00
// thEvent.Set();
2022-05-06 15:23:30 +08:00
// ShowWarningMsg();
2022-05-17 16:26:35 +08:00
int sleeptime = 500 - 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
}
}
2022-05-16 09:03:42 +08:00
public bool PlcInit ( )
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-27 15:47:18 +08:00
port . Connect ( "10.20.88.95" , 5002 ) ;
2022-05-06 12:28:16 +08:00
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 ) ;
2022-05-17 09:25:44 +08:00
IsConn = false ;
2021-06-05 14:17:30 +08:00
return false ;
}
2022-05-16 09:03:42 +08:00
}
/// <summary>
/// 初始化PLC
/// </summary>
/// <returns></returns>
public bool PlcInit ( IEnumerable < PlcItem > items )
{
foreach ( var plcitem in items )
{
switch ( plcitem . PlcItemDataType )
{
case PlcItemTypeDesc . M :
2022-05-18 10:15:49 +08:00
PlcItemMd_M_List . Add ( plcitem . PlcItemCode , plcitem ) ;
2022-05-16 09:03:42 +08:00
break ;
case PlcItemTypeDesc . D :
2022-05-18 10:15:49 +08:00
PlcItemMd_D_List . Add ( plcitem . PlcItemCode , plcitem ) ;
2022-05-16 09:03:42 +08:00
break ;
case PlcItemTypeDesc . X :
2022-05-18 10:15:49 +08:00
PlcItemMd_X_List . Add ( plcitem . PlcItemCode , plcitem ) ;
2022-05-16 09:03:42 +08:00
break ;
case PlcItemTypeDesc . Y :
2022-05-18 10:15:49 +08:00
PlcItemMd_Y_List . Add ( plcitem . PlcItemCode , plcitem ) ;
2022-05-16 09:03:42 +08:00
break ;
}
2022-05-18 10:15:49 +08:00
PlcItemMdList . Add ( plcitem . PlcItemCode , plcitem ) ;
2022-05-16 09:03:42 +08:00
}
return PlcInit ( ) ;
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 ;
2022-05-17 09:25:44 +08:00
IsConn = false ;
2021-06-05 14:17:30 +08:00
}
}
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
2022-05-19 15:56:08 +08:00
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 )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
foreach ( PlcItem item in PlcItemMd_M_List . Values )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
string value = "" ;
if ( Convert . ToInt32 ( item . PlcItemPriAddress ) > = 200 )
2021-06-05 14:17:30 +08:00
{
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
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 ( ) ;
2022-05-19 12:55:26 +08:00
2021-06-05 14:17:30 +08:00
}
2022-05-19 15:56:08 +08:00
}
else
{
if ( ! string . IsNullOrEmpty ( item . PlcItemDeviationAddress ) )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
//string PlcItemId = item.PlcItemId.ToString();
//string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert . ToInt32 ( item . PlcItemDeviationAddress ) ;
value = readBytArr_M1 [ plcItemDeviationAddress ] . ToString ( ) ;
2022-05-09 13:22:39 +08:00
}
2022-05-19 15:56:08 +08:00
}
if ( value ! = "" )
{
if ( item . PlcItemValue ! = value )
2022-05-09 13:22:39 +08:00
{
2022-05-19 15:56:08 +08:00
haschange = true ;
Console . WriteLine ( item . PlcItemCode + "数据变化" ) ;
2021-06-05 14:17:30 +08:00
}
2022-05-19 15:56:08 +08:00
item . PlcItemValue = value ;
Global . ShowSensorStatus ( item , new EventArgs ( ) ) ;
2022-05-19 12:55:26 +08:00
}
2022-05-19 15:56:08 +08:00
2021-06-05 14:17:30 +08:00
}
}
2022-05-19 15:56:08 +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-05-19 15:56:08 +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 )
{
if ( PlcItemMd_D_List ! = null & & PlcItemMd_D_List . Count > 0 )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
foreach ( PlcItem item in PlcItemMd_D_List . Values )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
var value = "" ;
if ( ! string . IsNullOrEmpty ( item . PlcItemDeviationAddress ) )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
string PlcItemId = item . PlcItemId . ToString ( ) ;
// string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert . ToInt32 ( item . PlcItemDeviationAddress ) ;
if ( Convert . ToInt32 ( item . PlcItemPriAddress ) < 409 )
2022-01-18 20:20:10 +08:00
{
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
value = tmpReadAlarmBytArr_D [ plcItemDeviationAddress ] . ToString ( ) ;
}
else
{
2022-05-09 08:59:35 +08:00
2022-05-19 15:56:08 +08:00
if ( item . PlcItemDataLength = = 2 )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
// ushort[] tmp_1 = new ushort[] { readBytArr_D[plcItemDeviationAddress], readBytArr_D[plcItemDeviationAddress + 1] };
2022-01-18 20:20:10 +08:00
2022-05-19 15:56:08 +08:00
#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 ] } ;
2022-05-13 09:08:42 +08:00
2022-01-18 20:20:10 +08:00
2022-05-19 15:56:08 +08:00
//readBytArr_D[plcItemDeviationAddress + 1] << 16 +
value = BitConverter . ToInt32 ( tmpByt_3 , 0 ) . ToString ( ) ;
#endregion
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
#region 方 法 二 : Short数组转换为int数据 20201014 孙 亚 龙
//相较而言 使用位移的算法效率更高一些。
// 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
2022-05-19 15:56:08 +08:00
#endregion
2022-05-19 12:55:26 +08:00
2022-05-19 15:56:08 +08:00
}
else
{
value = readBytArr_D [ plcItemDeviationAddress ] . ToString ( ) ;
2021-06-05 14:17:30 +08:00
}
}
2022-05-19 15:56:08 +08:00
}
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
if ( value ! = "" )
{
if ( item . PlcItemValue ! = value )
2022-05-09 13:22:39 +08:00
{
2022-05-19 15:56:08 +08:00
if ( item . PlcItemCode ! = "PlcHeartbeatWriteData" & & item . PlcItemCode ! = "PlcHeartbeatReadData" )
2022-05-16 13:54:26 +08:00
{
2022-05-19 15:56:08 +08:00
haschange = true ;
Console . WriteLine ( item . PlcItemCode + "数据变化" ) ;
2022-05-16 13:54:26 +08:00
}
2022-05-19 15:56:08 +08:00
item . PlcItemValue = value ;
2022-05-10 15:21:13 +08:00
2022-05-09 13:22:39 +08:00
}
2022-05-10 15:21:13 +08:00
2022-05-09 13:22:39 +08:00
}
2022-05-19 15:56:08 +08:00
2022-05-19 12:55:26 +08:00
}
2022-05-19 15:56:08 +08:00
}
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 ] )
2022-05-19 12:55:26 +08:00
{
2022-05-19 15:56:08 +08:00
foreach ( var W in CmWarningLogMd_List )
{
if ( W . Value . PlcItemCode = = plcitem )
{
W . Value . WaringStatus = 1 ;
}
}
if ( tmpReadAlarmBytArr_D [ i ] ! = 0 )
2022-05-19 12:55:26 +08:00
{
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
var wanglist = Global . CheckWarng ( tmpReadAlarmBytArr_D [ i ] , plcitem ) ;
foreach ( var wang in wanglist )
2022-05-19 12:55:26 +08:00
{
2022-05-19 15:56:08 +08:00
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);
}
2022-05-19 12:55:26 +08:00
}
2022-05-19 15:56:08 +08:00
2022-05-09 13:22:39 +08:00
2022-05-19 12:55:26 +08:00
}
2022-05-19 15:56:08 +08:00
lastAlarm [ i ] = tmpReadAlarmBytArr_D [ i ] ;
alarmChange = true ;
2021-06-05 14:17:30 +08:00
}
2022-05-19 15:56:08 +08:00
}
2022-05-09 08:59:35 +08:00
2022-05-19 15:56:08 +08:00
var keys = CmWarningLogMd_List . Keys . ToList ( ) ;
foreach ( var key in keys )
{
if ( CmWarningLogMd_List [ key ] . WaringStatus = = 1 )
2022-05-19 12:55:26 +08:00
{
2022-05-19 15:56:08 +08:00
// new CleanMachine.BLL.CM.WarningLogBLL().Update(Global.CmWarningLogMd_List[key]);
CmWarningLogMd_List . Remove ( key ) ;
2022-05-09 08:59:35 +08:00
}
2022-05-19 15:56:08 +08:00
}
2022-05-09 08:59:35 +08:00
2022-05-19 15:56:08 +08:00
if ( alarmChange )
{
ShowMainFrmAlarmMsg ( ) ;
}
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
//if (Global.CmWarningLogMd_List.Count() > 0)
//{
// ShowMainFrmAlarmMsg(true, EventArgs.Empty);
//}
//else
//{
// ShowMainFrmAlarmMsg(false, EventArgs.Empty);
//}
2022-05-09 08:59:35 +08:00
2022-05-19 15:56:08 +08:00
// HandleWaringLog(tmpReadAlarmBytArr_D);
}
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-19 12:55:26 +08:00
ushort [ ] lastAlarm = new ushort [ 9 ] { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , } ;
2022-05-19 15:56:08 +08:00
public Dictionary < string , WarningLog > CmWarningLogMd_List { get ; set ; } = new Dictionary < string , WarningLog > ( ) ;
2022-05-19 12:55:26 +08:00
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 ) ;
}
2022-05-09 08:59:35 +08:00
2022-05-19 12:55:26 +08:00
}
2022-05-09 08:59:35 +08:00
2021-06-05 14:17:30 +08:00
//报警信息记录
2022-05-19 15:56:08 +08:00
// 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
// }
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-19 15:56:08 +08:00
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 )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
foreach ( PlcItem item in PlcItemMd_X_List . Values )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
string value = "" ;
if ( ! string . IsNullOrEmpty ( item . PlcItemDeviationAddress ) )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
string PlcItemId = item . PlcItemId . ToString ( ) ;
string plcdatatype = item . PlcItemDataType . ToString ( ) ;
int plcItemDeviationAddress = Convert . ToInt32 ( item . PlcItemDeviationAddress ) ;
value = readBytArr_X [ plcItemDeviationAddress ] . ToString ( ) ;
if ( value ! = null )
2022-05-09 13:22:39 +08:00
{
2022-05-19 15:56:08 +08:00
if ( item . PlcItemValue ! = value )
2022-05-09 13:22:39 +08:00
{
2022-05-19 15:56:08 +08:00
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 ( ) ) ;
}
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
}
2022-05-09 13:22:39 +08:00
2021-06-05 14:17:30 +08:00
2022-05-19 15:56:08 +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-19 12:55:26 +08:00
}
2022-05-19 15:56:08 +08:00
this . DoShowX_Y_Status ( changeItems ) ;
2021-06-05 14:17:30 +08:00
}
2022-05-19 15:56:08 +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
2022-05-19 15:56:08 +08:00
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 )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
foreach ( PlcItem item in PlcItemMd_Y_List . Values )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
string value = "" ;
if ( ! string . IsNullOrEmpty ( item . PlcItemDeviationAddress ) )
2021-06-05 14:17:30 +08:00
{
2022-05-19 15:56:08 +08:00
string PlcItemId = item . PlcItemId . ToString ( ) ;
string plcdatatype = item . PlcItemDataType . ToString ( ) ;
int plcItemDeviationAddress = Convert . ToInt32 ( item . PlcItemDeviationAddress ) ;
value = readBytArr_Y [ plcItemDeviationAddress ] . ToString ( ) ;
2022-05-09 13:22:39 +08:00
2022-05-19 15:56:08 +08:00
if ( value ! = null )
{
if ( item . PlcItemValue ! = value )
2022-05-09 13:22:39 +08:00
{
2022-05-19 15:56:08 +08:00
haschange = true ;
item . PlcItemValue = value ;
changeItems . Add ( item ) ;
// this.DoShowX_Y_Status(item);
// Global.ShowX_Y_Status(item, new EventArgs());
2022-05-09 13:22:39 +08:00
}
}
2022-05-19 15:56:08 +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-19 12:55:26 +08:00
}
2022-05-19 15:56:08 +08:00
this . DoShowX_Y_Status ( changeItems ) ;
2021-06-05 14:17:30 +08:00
}
2022-05-19 15:56:08 +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-18 10:15:49 +08:00
// PlcItem item = PlcItemMdList.Find(x => x.PlcItemCode == plcItemCode);
PlcItemMdList . TryGetValue ( plcItemCode , out var item ) ;
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
2022-05-19 15:56:08 +08:00
object retObj ;
switch ( itemType )
{
case PlcItemTypeDesc . M :
PlcItemMd_M_List . TryGetValue ( plcItemCode , out var item_M ) ;
2022-01-19 09:31:47 +08:00
2022-05-19 15:56:08 +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 :
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 )
2022-05-13 09:08:42 +08:00
{
2022-05-19 15:56:08 +08:00
retObj = int . Parse ( item_D . PlcItemValue ) ;
2022-05-13 09:08:42 +08:00
}
else
{
2022-05-19 15:56:08 +08:00
retObj = short . Parse ( item_D . PlcItemValue ) ;
2022-05-19 13:02:36 +08:00
}
2022-05-13 09:08:42 +08:00
2022-05-19 15:56:08 +08:00
}
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 ;
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
2022-05-19 15:56:08 +08:00
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 ) ;
2022-05-19 13:02:36 +08:00
2022-05-19 15:56:08 +08:00
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 ;
}
2022-05-13 14:55:10 +08:00
2022-05-19 15:56:08 +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 } ) ;
2022-05-13 14:55:10 +08:00
2022-05-19 15:56:08 +08:00
}
else
{
master . WriteSingleRegister ( slaveAddress , Convert . ToUInt16 ( item_D . PlcItemModbusAddress ) , ushort . Parse ( writeValue ) ) ;
}
2022-05-13 14:55:10 +08:00
2022-05-19 15:56:08 +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
2022-05-19 15:56:08 +08:00
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-05-19 15:56:08 +08:00
wcsToPlcHeartData = wcsToPlcHeartDataNew ;
2022-01-18 20:20:10 +08:00
}
2022-05-19 15:56:08 +08:00
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 )
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 ) )
{
2022-05-17 09:25:44 +08:00
IsConn = true ;
2021-06-05 14:17:30 +08:00
HeartbeatTime = DateTime . Now ;
wcsToPlcHeartDataNew = wcsToPlcHeartData ;
frmMainNew . Show_Plc_Alarm ( "Serial port open success, PLC connect success!" ) ;
}
}
2022-05-19 15:56:08 +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-05-27 15:47:18 +08:00
if ( DateTime . Now > HeartbeatTime . AddSeconds ( 10 ) ) //心跳超时
2022-01-13 10:43:45 +08:00
{
2022-05-19 12:55:26 +08:00
IsConn = false ;
//报警处理。
frmMainNew . Show_Plc_Alarm ( "PLC heartbeat data is abnormal, PLC loses connection!" ) ;
if ( ReConnNum % 5 = = 0 )
{
ClosePlcConn ( ) ;
PlcInit ( ) ;
}
ReConnNum + + ;
2022-01-13 10:43:45 +08:00
}
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
}