修改读取逻辑

This commit is contained in:
gongxh
2022-05-06 15:23:30 +08:00
parent 0e459f231a
commit 64238fee13
8 changed files with 199 additions and 121 deletions

View File

@@ -117,14 +117,14 @@ namespace CleanMachineHMI
/// <summary>
///
/// </summary>
public static List<PlcItem> PlcItemMdList { get; set; } = new List<PlcItem>();
public static List<PlcItem> PlcItemMd_D_List { get; set; } = new List<PlcItem>();
//public static List<PlcItem> PlcItemMdList { get; set; } = new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_D_List { get; set; } = new List<PlcItem>();
public static List<PlcItem> PlcItemMd_M_List { get; set; }=new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_M_List { get; set; }=new List<PlcItem>();
public static List<PlcItem> PlcItemMd_X_List { get; set; }=new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_X_List { get; set; }=new List<PlcItem>();
public static List<PlcItem> PlcItemMd_Y_List { get; set; }=new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_Y_List { get; set; }=new List<PlcItem>();
public static List<Warning> BasWarningMd_List { get; set; }

View File

@@ -24,6 +24,7 @@ using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CleanMachineHMI
@@ -60,7 +61,7 @@ namespace CleanMachineHMI
private byte slaveAddress = 1;
private byte slaveAddress = byte.Parse(ConfigurationManager.AppSettings["SlaveAddress"]);
private bool _isConn = false;
@@ -85,13 +86,77 @@ namespace CleanMachineHMI
#else
private static SerialPort port;
#endif
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>();
public List<PlcItem> PlcItemMd_X_List { get; set; } = new List<PlcItem>();
public List<PlcItem> PlcItemMd_Y_List { get; set; } = new List<PlcItem>();
public void StartRead()
{
Thread th = new Thread(RefreshPlcItem)
{
IsBackground = true
};
th.Start();
}
private void RefreshPlcItem()
{
while (Global.SysThIsRunFlag)
{
RefreshPlcItem_M();
Instance.RefreshPlcItem_D();
Instance.RefreshPlcItem_X();
Instance.RefreshPlcItem_Y();
// ShowWarningMsg();
Thread.Sleep(100);
}
}
/// <summary>
/// 初始化PLC
/// </summary>
/// <returns></returns>
public bool PlcInit()
public bool PlcInit(IEnumerable<PlcItem> items)
{
foreach (var plcitem in items)
{
switch (plcitem.PlcItemDataType)
{
case 0:
PlcItemMd_M_List.Add(plcitem);
break;
case 1:
PlcItemMd_D_List.Add(plcitem);
break;
case 2:
PlcItemMd_X_List.Add(plcitem);
break;
case 3:
PlcItemMd_Y_List.Add(plcitem);
break;
}
PlcItemMdList.Add(plcitem);
}
try
{
@@ -103,6 +168,7 @@ namespace CleanMachineHMI
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"]);
@@ -176,9 +242,9 @@ namespace CleanMachineHMI
bool[] readBytArr_M1 = master.ReadCoils(slaveAddress, 2048, 50);
if (readBytArr_M.Length > 0 && readBytArr_M1.Length > 0)
{
if (Global.PlcItemMd_M_List != null && Global.PlcItemMd_M_List.Count > 0)
if (PlcItemMd_M_List != null && PlcItemMd_M_List.Count > 0)
{
foreach (PlcItem item in Global.PlcItemMd_M_List)
foreach (PlcItem item in PlcItemMd_M_List)
{
if (Convert.ToInt32(item.PlcItemPriAddress) >= 200)
{
@@ -223,9 +289,9 @@ namespace CleanMachineHMI
ushort[] readBytArr_D = master.ReadHoldingRegisters(slaveAddress, 4505, 50);
if (readBytArr_D.Length > 0 && tmpReadAlarmBytArr_D.Length > 0)
{
if (Global.PlcItemMd_D_List != null && Global.PlcItemMd_D_List.Count > 0)
if (PlcItemMd_D_List != null && PlcItemMd_D_List.Count > 0)
{
foreach (PlcItem item in Global.PlcItemMd_D_List)
foreach (PlcItem item in PlcItemMd_D_List)
{
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
@@ -239,6 +305,7 @@ namespace CleanMachineHMI
}
else
{
if (item.PlcItemDataLength == 2)
{
ushort[] tmp_1 = new ushort[] { readBytArr_D[plcItemDeviationAddress], readBytArr_D[plcItemDeviationAddress + 1] };
@@ -627,9 +694,9 @@ namespace CleanMachineHMI
if (readBytArr_X.Length > 0)
//if(true)
{
if (Global.PlcItemMd_X_List != null && Global.PlcItemMd_X_List.Count > 0)
if (PlcItemMd_X_List != null && PlcItemMd_X_List.Count > 0)
{
foreach (PlcItem item in Global.PlcItemMd_X_List)
foreach (PlcItem item in PlcItemMd_X_List)
{
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
@@ -666,9 +733,9 @@ namespace CleanMachineHMI
if (readBytArr_Y.Length > 0)
//if(true)
{
if (Global.PlcItemMd_Y_List != null && Global.PlcItemMd_Y_List.Count > 0)
if (PlcItemMd_Y_List != null && PlcItemMd_Y_List.Count > 0)
{
foreach (PlcItem item in Global.PlcItemMd_Y_List)
foreach (PlcItem item in PlcItemMd_Y_List)
{
if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress))
{
@@ -696,9 +763,11 @@ namespace CleanMachineHMI
}
public object ReadPlcItem(string plcItemCode)
{
PlcItem item = Global.PlcItemMdList.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item = PlcItemMdList.Find(x => x.PlcItemCode == plcItemCode);
if (item == null)
{
return null;
@@ -744,7 +813,7 @@ namespace CleanMachineHMI
{
case PlcItemTypeDesc.M:
PlcItem item_M = Global.PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_M = PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_M == null)
{
return null;
@@ -759,7 +828,7 @@ namespace CleanMachineHMI
}
break;
case PlcItemTypeDesc.D:
PlcItem item_D = Global.PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_D == null)
{
return null;
@@ -774,7 +843,7 @@ namespace CleanMachineHMI
}
break;
case PlcItemTypeDesc.X:
PlcItem item_X = Global.PlcItemMd_X_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_X = PlcItemMd_X_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_X == null)
{
return null;
@@ -789,7 +858,7 @@ namespace CleanMachineHMI
}
break;
case PlcItemTypeDesc.Y:
PlcItem item_Y = Global.PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_Y == null)
{
return null;
@@ -828,7 +897,7 @@ namespace CleanMachineHMI
switch (itemType)
{
case PlcItemTypeDesc.M:
PlcItem item_M = Global.PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_M = PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_M == null)
{
return false;
@@ -836,7 +905,7 @@ namespace CleanMachineHMI
master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue));
return true;
case PlcItemTypeDesc.Y:
PlcItem item_Y = Global.PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_Y == null)
{
return false;
@@ -844,7 +913,7 @@ namespace CleanMachineHMI
master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue));
return true;
case PlcItemTypeDesc.D:
PlcItem item_D = Global.PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
if (item_D == null)
{
return false;