2022-01-19 14:59:16 +08:00
//使用本地调式
2022-05-05 09:38:28 +08:00
#if DEBUG
2022-05-06 12:28:16 +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 ;
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
2021-12-27 21:39:45 +08:00
private byte slaveAddress = 1 ;
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
/// <summary>
/// 初始化PLC
/// </summary>
/// <returns></returns>
2021-06-05 14:17:30 +08:00
public bool PlcInit ( )
{
2022-01-19 09:31:47 +08:00
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" ] ) ;
//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 ;
}
}
public void RefreshPlcItem_M ( )
{
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 12:28:16 +08:00
if ( Global . PlcItemMd_M_List ! = null & & Global . PlcItemMd_M_List . Count > 0 )
2021-06-05 14:17:30 +08:00
{
2022-05-06 12:28:16 +08:00
foreach ( PlcItem item in Global . PlcItemMd_M_List )
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
if ( Convert . ToInt32 ( item . PlcItemPriAddress ) > = 200 )
2021-06-05 14:17:30 +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-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 ) ;
item . PlcItemValue = readBytArr_M [ plcItemDeviationAddress ] . ToString ( ) ;
2022-05-06 12:28:16 +08:00
Global . ShowSensorStatus ( item , new EventArgs ( ) ) ;
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 ) ;
item . PlcItemValue = readBytArr_M1 [ plcItemDeviationAddress ] . ToString ( ) ;
2022-05-06 12:28:16 +08:00
Global . ShowSensorStatus ( item , new EventArgs ( ) ) ;
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
}
public void RefreshPlcItem_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
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 12:28:16 +08:00
if ( Global . PlcItemMd_D_List ! = null & & Global . PlcItemMd_D_List . Count > 0 )
2021-06-05 14:17:30 +08:00
{
2022-05-06 12:28:16 +08:00
foreach ( PlcItem item in Global . PlcItemMd_D_List )
2021-06-05 14:17:30 +08:00
{
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 )
{
item . PlcItemValue = tmpReadAlarmBytArr_D [ plcItemDeviationAddress ] . ToString ( ) ;
}
else
{
if ( item . PlcItemDataLength = = 2 )
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
ushort [ ] tmp_1 = new ushort [ ] { readBytArr_D [ plcItemDeviationAddress ] , readBytArr_D [ plcItemDeviationAddress + 1 ] } ;
#region 方 法 一 : Short数组转换为int数据 20201014 孙 亚 龙
////低位 short 的字节数组。
//byte[] tmpByt_1 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress]);
////高位 short 的字节数组。
//byte[] tmpByt_2 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress + 1]);
////组合为int32的字节数组。
//byte[] tmpByt_3 = new byte[] { tmpByt_1[0], tmpByt_1[1], tmpByt_2[0], tmpByt_2[1] };
////readBytArr_D[plcItemDeviationAddress + 1] << 16 +
//item.PlcItemValue = BitConverter.ToUInt32(tmpByt_3, 0).ToString();
#endregion
#region 方 法 二 : Short数组转换为int数据 20201014 孙 亚 龙
//相较而言 使用位移的算法效率更高一些。
item . PlcItemValue = ( ( readBytArr_D [ plcItemDeviationAddress + 1 ] < < 16 ) + readBytArr_D [ plcItemDeviationAddress ] ) . ToString ( ) ;
#endregion
2021-06-05 14:17:30 +08:00
}
else
{
2022-01-18 20:20:10 +08:00
item . PlcItemValue = readBytArr_D [ plcItemDeviationAddress ] . ToString ( ) ;
2021-06-05 14:17:30 +08:00
}
}
}
}
}
2022-01-18 20:20:10 +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
}
//报警信息记录
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
}
public void RefreshPlcItem_X ( )
{
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_X = master . ReadInputs ( slaveAddress , 1024 , 50 ) ;
if ( readBytArr_X . Length > 0 )
//if(true)
{
2022-05-06 12:28:16 +08:00
if ( Global . PlcItemMd_X_List ! = null & & Global . PlcItemMd_X_List . Count > 0 )
2021-06-05 14:17:30 +08:00
{
2022-05-06 12:28:16 +08:00
foreach ( PlcItem item in Global . PlcItemMd_X_List )
2021-06-05 14:17:30 +08:00
{
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 ) ;
item . PlcItemValue = readBytArr_X [ plcItemDeviationAddress ] . ToString ( ) ;
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-06 12:28:16 +08:00
Global . ShowX_Y_Status ( item , new EventArgs ( ) ) ;
Global . ShowSensorStatus ( item , new EventArgs ( ) ) ;
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
}
public void RefreshPlcItem_Y ( )
{
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_Y = master . ReadCoils ( slaveAddress , 1280 , 50 ) ;
if ( readBytArr_Y . Length > 0 )
//if(true)
{
2022-05-06 12:28:16 +08:00
if ( Global . PlcItemMd_Y_List ! = null & & Global . PlcItemMd_Y_List . Count > 0 )
2021-06-05 14:17:30 +08:00
{
2022-05-06 12:28:16 +08:00
foreach ( PlcItem item in Global . PlcItemMd_Y_List )
2021-06-05 14:17:30 +08:00
{
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 ) ;
item . PlcItemValue = readBytArr_Y [ plcItemDeviationAddress ] . ToString ( ) ;
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-06 12:28:16 +08:00
Global . ShowX_Y_Status ( item , new EventArgs ( ) ) ;
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 12:28:16 +08:00
public object ReadPlcItem ( string plcItemCode )
{
PlcItem item = Global . PlcItemMdList . Find ( x = > x . PlcItemCode = = plcItemCode ) ;
if ( item = = null )
{
return null ;
}
if ( item . PlcItemDataType = = 1 )
{
if ( string . IsNullOrEmpty ( item . PlcItemValue ) )
{
return 0 ;
}
else
{
return short . Parse ( item . PlcItemValue ) ;
}
}
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 12:28:16 +08:00
PlcItem item_M = Global . 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 12:28:16 +08:00
PlcItem item_D = Global . 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
{
retObj = short . Parse ( item_D . PlcItemValue ) ;
}
break ;
case PlcItemTypeDesc . X :
2022-05-06 12:28:16 +08:00
PlcItem item_X = Global . 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 12:28:16 +08:00
PlcItem item_Y = Global . 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 12:28:16 +08:00
PlcItem item_M = Global . 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 12:28:16 +08:00
PlcItem item_Y = Global . 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 12:28:16 +08:00
PlcItem item_D = Global . PlcItemMd_D_List . Find ( x = > x . PlcItemCode = = plcItemCode ) ;
2022-01-18 20:20:10 +08:00
if ( item_D = = null )
{
return false ;
}
master . WriteSingleRegister ( slaveAddress , Convert . ToUInt16 ( item_D . PlcItemModbusAddress ) , ushort . Parse ( writeValue ) ) ;
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
}
}
public enum PlcItemTypeDesc
{
/// <summary>
/// M 类型布尔变量
/// </summary>
M ,
/// <summary>
/// D 类型整数变量
/// </summary>
D ,
/// <summary>
/// X 类型布尔变量
/// </summary>
X ,
/// <summary>
/// Y 类型布尔变量
/// </summary>
Y
}
}