修改消息处理

This commit is contained in:
gongxh
2022-05-09 13:22:39 +08:00
parent b39357f27e
commit 8f9e5a8ee2
5 changed files with 1060 additions and 687 deletions

View File

@@ -65,6 +65,24 @@ namespace CleanMachineHMI
_synchronization = SynchronizationContext.Current;
}
/// <summary>
/// 读取完成的委托
/// </summary>
public delegate void ReadFinish();
public event ReadFinish OnReadFinish;
public void DoOnReadFinish(object data)
{
OnReadFinish?.Invoke();
}
private SynchronizationContext _synchronization;
@@ -98,7 +116,7 @@ namespace CleanMachineHMI
public List<PlcItem> PlcItemMdList { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMdList { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMd_D_List { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMd_M_List { get; set; } = new List<PlcItem>();
@@ -108,15 +126,15 @@ namespace CleanMachineHMI
public List<PlcItem> PlcItemMd_Y_List { get; set; } = new List<PlcItem>();
//public void StartRead()
//{
// Thread th = new Thread(RefreshPlcItem)
// {
// IsBackground = true
// };
// th.Start();
public void StartRead()
{
Thread th = new Thread(RefreshPlcItem)
{
IsBackground = true
};
th.Start();
//}
}
@@ -126,10 +144,18 @@ namespace CleanMachineHMI
{
while (Global.SysThIsRunFlag)
{
RefreshPlcItem_M();
RefreshPlcItem_D();
RefreshPlcItem_X();
RefreshPlcItem_Y();
var haschange=false;
RefreshPlcItem_M(ref haschange);
RefreshPlcItem_D(ref haschange);
RefreshPlcItem_X(ref haschange);
RefreshPlcItem_Y(ref haschange);
if (haschange)
{
if (_synchronization != null)
{
_synchronization.Post(this.DoOnReadFinish, null);
}
}
// thEvent.Set();
// ShowWarningMsg();
Thread.Sleep(100);
@@ -246,8 +272,9 @@ namespace CleanMachineHMI
isConn = false;
}
}
public void RefreshPlcItem_M()
public void RefreshPlcItem_M(ref bool haschange)
{
#if RELEASE
try
{
@@ -260,15 +287,17 @@ namespace CleanMachineHMI
{
foreach (PlcItem item in PlcItemMd_M_List)
{
string value = "";
if (Convert.ToInt32(item.PlcItemPriAddress) >= 200)
{
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
//string PlcItemId = item.PlcItemId.ToString();
// string plcdatatype = item.PlcItemDataType.ToString();
//string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
item.PlcItemValue = readBytArr_M[plcItemDeviationAddress].ToString();
Global.ShowSensorStatus(item, new EventArgs());
value = readBytArr_M[plcItemDeviationAddress].ToString();
}
}
else
@@ -278,10 +307,20 @@ namespace CleanMachineHMI
//string PlcItemId = item.PlcItemId.ToString();
//string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
item.PlcItemValue = readBytArr_M1[plcItemDeviationAddress].ToString();
Global.ShowSensorStatus(item, new EventArgs());
value = readBytArr_M1[plcItemDeviationAddress].ToString();
}
}
if (value != "")
{
if (item.PlcItemValue != value)
{
haschange = true;
}
item.PlcItemValue = value;
Global.ShowSensorStatus(item, new EventArgs());
}
}
}
}
@@ -293,7 +332,7 @@ namespace CleanMachineHMI
}
#endif
}
public void RefreshPlcItem_D()
public void RefreshPlcItem_D(ref bool haschange)
{
#if RELEASE
try
@@ -307,6 +346,7 @@ namespace CleanMachineHMI
{
foreach (PlcItem item in PlcItemMd_D_List)
{
var value = "";
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
@@ -315,7 +355,8 @@ namespace CleanMachineHMI
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
if (Convert.ToInt32(item.PlcItemPriAddress) < 409)
{
item.PlcItemValue = tmpReadAlarmBytArr_D[plcItemDeviationAddress].ToString();
value = tmpReadAlarmBytArr_D[plcItemDeviationAddress].ToString();
}
else
{
@@ -337,16 +378,30 @@ namespace CleanMachineHMI
#region 方法二 :Short数组转换为int数据 20201014 孙亚龙
//相较而言 使用位移的算法效率更高一些。
item.PlcItemValue = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
value = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
#endregion
}
else
{
item.PlcItemValue = readBytArr_D[plcItemDeviationAddress].ToString();
value = readBytArr_D[plcItemDeviationAddress].ToString();
}
}
}
if (value != "")
{
if (item.PlcItemValue != value)
{
haschange = true;
}
item.PlcItemValue = value;
}
}
}
string priStr = "D";
@@ -372,7 +427,7 @@ namespace CleanMachineHMI
{
Global.ShowMainFrmAlarmMsg(false, EventArgs.Empty);
}
// HandleWaringLog(tmpReadAlarmBytArr_D);
}
@@ -728,7 +783,7 @@ namespace CleanMachineHMI
#endif
}
public void RefreshPlcItem_X()
public void RefreshPlcItem_X(ref bool haschange)
{
#if RELEASE
try
@@ -742,18 +797,33 @@ namespace CleanMachineHMI
{
foreach (PlcItem item in PlcItemMd_X_List)
{
string value = "";
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
string PlcItemId = item.PlcItemId.ToString();
string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
item.PlcItemValue = readBytArr_X[plcItemDeviationAddress].ToString();
value = readBytArr_X[plcItemDeviationAddress].ToString();
if (value != null)
{
if (item.PlcItemValue != value)
{
haschange = true;
item.PlcItemValue = value;
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";
Global.ShowX_Y_Status(item, new EventArgs());
Global.ShowSensorStatus(item, new EventArgs());
}
}
}
@@ -767,7 +837,7 @@ namespace CleanMachineHMI
#endif
}
public void RefreshPlcItem_Y()
public void RefreshPlcItem_Y(ref bool haschange)
{
#if RELEASE
try
@@ -781,17 +851,31 @@ namespace CleanMachineHMI
{
foreach (PlcItem item in PlcItemMd_Y_List)
{
string value = "";
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
string PlcItemId = item.PlcItemId.ToString();
string plcdatatype = item.PlcItemDataType.ToString();
int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress);
item.PlcItemValue = readBytArr_Y[plcItemDeviationAddress].ToString();
value = readBytArr_Y[plcItemDeviationAddress].ToString();
if (value != null)
{
if (item.PlcItemValue != value)
{
haschange=true;
item.PlcItemValue = value;
Global.ShowX_Y_Status(item, new EventArgs());
}
}
//Random r = new Random();
//item.PlcItemValue = r.Next(0, 2) == 0 ? "False" : "True";
Global.ShowX_Y_Status(item, new EventArgs());
}
}