From 3287eccd7074c6a73159e6c5f11b399fb4255c44 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Sat, 30 May 2020 12:46:21 +0200 Subject: [PATCH] Added RT Module read method --- CMS_CORE_Library/Models/DataStructures.cs | 10 +- CMS_CORE_Library/NcThermo.cs | 19 +- CMS_CORE_Library/S7Net/Nc_S7Net.cs | 227 +++++++++++++++++++--- 3 files changed, 218 insertions(+), 38 deletions(-) diff --git a/CMS_CORE_Library/Models/DataStructures.cs b/CMS_CORE_Library/Models/DataStructures.cs index 775b38a..13c3239 100644 --- a/CMS_CORE_Library/Models/DataStructures.cs +++ b/CMS_CORE_Library/Models/DataStructures.cs @@ -626,11 +626,11 @@ namespace CMS_CORE_Library.Models public class ModuleBlock { - public ushort Id { get; set; } = 0; - public uint ActualDelay { get; set; } = 0; - public uint ActualDuration { get; set; } = 0; - public uint EstimatedDelay { get; set; } = 0; - public uint EstimatedDuration { get; set; } = 0; + public short Id { get; set; } = 0; + public int ActualDelay { get; set; } = 0; + public int ActualDuration { get; set; } = 0; + public int EstimatedDelay { get; set; } = 0; + public int EstimatedDuration { get; set; } = 0; public List PrecedingId { get; set; } = new List(); public bool Visible { get; set; } = false; public bool Running { get; set; } = false; diff --git a/CMS_CORE_Library/NcThermo.cs b/CMS_CORE_Library/NcThermo.cs index 2b55e58..ffdb877 100644 --- a/CMS_CORE_Library/NcThermo.cs +++ b/CMS_CORE_Library/NcThermo.cs @@ -620,16 +620,6 @@ namespace CMS_CORE_Library #region THERMO high level data - /// - /// - /// Read power gauge data - /// - /// Compatibility: S7Net - /// - /// Returns an error when an internal or a library error occours - /// - /// Reference to the value of the gauge data - public abstract CmsError PLC_RGaugeData(ref GaugeModel gaugeData); /// /// Get current recipe parameter list /// @@ -657,6 +647,15 @@ namespace CMS_CORE_Library /// public abstract CmsError PLC_RWarmerChannelList(ref Dictionary currWarmerChannelList); /// + /// Read power gauge data + /// + /// Compatibility: S7Net + /// + /// Returns an error when an internal or a library error occours + /// + /// Reference to the value of the gauge data + public abstract CmsError PLC_RGaugeData(ref GaugeModel gaugeData); + /// /// Get current Production Info data /// /// diff --git a/CMS_CORE_Library/S7Net/Nc_S7Net.cs b/CMS_CORE_Library/S7Net/Nc_S7Net.cs index 3c23507..5380c9f 100644 --- a/CMS_CORE_Library/S7Net/Nc_S7Net.cs +++ b/CMS_CORE_Library/S7Net/Nc_S7Net.cs @@ -1805,29 +1805,6 @@ namespace CMS_CORE_Library.S7Net #region THERMO high level data - /// - /// Get gauge data - /// - /// - /// - public override CmsError PLC_RGaugeData(ref GaugeModel gaugeData) - { - List Values = new List(); - CmsError cmsError = MEM_RWIntegerList(R, 1, MACHINE_GAUGE_DATA.MemType, MACHINE_GAUGE_DATA.Address, MACHINE_GAUGE_DATA.SubAddress, MACHINE_GAUGE_DATA.Size / 4, ref Values); - - if (cmsError.IsError()) - return cmsError; - - gaugeData = new GaugeModel() - { - TimeAdv = Convert.ToUInt32(Values[0]), - Power = Values[1], - Vacuum = Values[2], - Air = Values[3], - }; - - return NO_ERROR; - } /// /// read current recipe parameter list /// @@ -1884,6 +1861,16 @@ namespace CMS_CORE_Library.S7Net /// public override CmsError PLC_RModulesBlockList(bool onlyRT, ref Dictionary currModulesBlockList) { + // se richiesto NON SOLO RT faccio refresh anche dati "lenti" + if (!onlyRT) + { + refreshMemModBlockParameter(); + } + // refresh dati veloci / RT + refreshMemModBlockParameterRT(); + + // copio lista act in output... + currModulesBlockList = ThermoModuleList; return NO_ERROR; } /// @@ -1896,6 +1883,29 @@ namespace CMS_CORE_Library.S7Net return NO_ERROR; } /// + /// Get gauge data + /// + /// + /// + public override CmsError PLC_RGaugeData(ref GaugeModel gaugeData) + { + List Values = new List(); + CmsError cmsError = MEM_RWIntegerList(R, 1, MACHINE_GAUGE_DATA.MemType, MACHINE_GAUGE_DATA.Address, MACHINE_GAUGE_DATA.SubAddress, MACHINE_GAUGE_DATA.Size / 4, ref Values); + + if (cmsError.IsError()) + return cmsError; + + gaugeData = new GaugeModel() + { + TimeAdv = Convert.ToUInt32(Values[0]), + Power = Values[1], + Vacuum = Values[2], + Air = Values[3], + }; + + return NO_ERROR; + } + /// /// Get current Production Info data /// /// @@ -2072,6 +2082,177 @@ namespace CMS_CORE_Library.S7Net } } + protected void refreshMemModBlockParameterRT() + { + List currMem = new List(); + int packSize = 12; + + // leggo da PLC a array di byte di appoggio... + CmsError cmsError = MEM_RWByteList(R, 0, MODULE_RT_DATA.MemType, MODULE_RT_DATA.Address, MODULE_RT_DATA.SubAddress, 0, MODULE_RT_DATA.Size, ref currMem); + + // controllo SE ho dati... + if (currMem.Count > 0) + { + // converto a blocchi di 8 byte... + byte[] memArray = currMem.ToArray(); + for (int i = 0; i < MODULE_RT_DATA.Size / packSize; i++) + { + PlcModBlockRT currModBlock = new PlcModBlockRT(memArray.Skip(i * packSize).Take(packSize).ToArray()); + // solo se ID > 0... + if (currModBlock.Id > 0) + { + // update oggetto thermoParamList... + if (ThermoModuleList.ContainsKey(currModBlock.Id)) + { + ThermoModuleList[currModBlock.Id].Visible = currModBlock.Visible; + ThermoModuleList[currModBlock.Id].Running = currModBlock.Running; + ThermoModuleList[currModBlock.Id].HasError = currModBlock.HasError; + ThermoModuleList[currModBlock.Id].ActualDuration = currModBlock.ActualDuration; + ThermoModuleList[currModBlock.Id].ActualDelay = currModBlock.ActualDelay; + } + else + { + ThermoModuleList.Add(currModBlock.Id, new ModuleBlock() { + Id = currModBlock.Id, + ActualDuration = currModBlock.ActualDuration, + ActualDelay = currModBlock.ActualDelay, + Visible = currModBlock.Visible, + Running = currModBlock.Running, + HasError = currModBlock.HasError + }); + } + } + } + } + } + protected void refreshMemModBlockParameter() + { +#if false + List currMem = new List(); + int packSize = 20; + + // leggo da PLC a array di byte di appoggio... + CmsError cmsError = MEM_RWByteList(R, 0, PARAMETER_DATA.MemType, PARAMETER_DATA.Address, PARAMETER_DATA.SubAddress, 0, PARAMETER_DATA.Size, ref currMem); + + + // controllo SE ho dati... + if (currMem.Count > 0) + { + // converto a blocchi di 8 byte... + byte[] memArray = currMem.ToArray(); + for (int i = 0; i < PARAMETER_DATA.Size / packSize; i++) + { + PlcParam currParam = new PlcParam(memArray.Skip(i * packSize).Take(packSize).ToArray()); + // solo se ID > 0... + if (currParam.Id > 0) + { + // update oggetto thermoParamList... + if (ThermoParamList.ContainsKey(currParam.Id)) + { + ThermoParamList[currParam.Id].SetpointHMI = currParam.SetpointHMI; + ThermoParamList[currParam.Id].SetpointPLC = currParam.SetpointPLC; + ThermoParamList[currParam.Id].ValMax = currParam.ValMax; + ThermoParamList[currParam.Id].ValMin = currParam.ValMin; + ThermoParamList[currParam.Id].UnitMeasure = currParam.UnitMeasure; + } + else + { + ThermoParamList.Add(currParam.Id, new RecipeParam() { Id = currParam.Id, SetpointHMI = currParam.SetpointHMI, SetpointPLC = currParam.SetpointPLC, ValMax = currParam.ValMax, ValMin = currParam.ValMin, UnitMeasure = currParam.UnitMeasure }); + } + } + } + } +#endif + } + /// + /// Oggetto Modulo Blocco RT da PLC + /// + protected class PlcModBlockRT + { + public short Id { get; set; } = 0; + public int ActualDuration { get; set; } = 0; + public int ActualDelay { get; set; } = 0; + private ushort Stato { get; set; } = 0; + public bool Visible { get; set; } + public bool Running { get; set; } + public bool HasError { get; set; } + /// + /// Deserializzazione da byte ad oggetto ParamRT + /// + /// + public PlcModBlockRT(byte[] rawData) + { + // converto i dati da byte grezzi ai 3 componenti... + Id = S7.Net.Types.Int.FromByteArray(rawData.Skip(0).Take(2).ToArray()); + ActualDuration = S7.Net.Types.DInt.FromByteArray(rawData.Skip(2).Take(4).ToArray()); + ActualDelay = S7.Net.Types.DInt.FromByteArray(rawData.Skip(6).Take(4).ToArray()); + Stato = S7.Net.Types.Word.FromByteArray(rawData.Skip(10).Take(2).ToArray()); + // calcolo bit... + Visible = (Stato & 1) == 1; + Running = (Stato & 2) == 2; + HasError = (Stato & 4) == 4; + } + /// + /// Converte un singolo item in un array di byte per scrittura su PLC S7 + /// + /// + public byte[] convertToByte() + { + // convero stato dai bit... + Stato = 0; + Stato += (ushort)(Visible ? 1 : 0); + Stato += (ushort)(Running ? 2 : 0); + Stato += (ushort)(HasError ? 4 : 0); + byte[] answ = new byte[8]; + Buffer.BlockCopy(S7.Net.Types.Int.ToByteArray(Id), 0, answ, 0, 2); + Buffer.BlockCopy(S7.Net.Types.DInt.ToByteArray(ActualDuration), 0, answ, 2, 4); + Buffer.BlockCopy(S7.Net.Types.DInt.ToByteArray(ActualDelay), 0, answ, 6, 4); + Buffer.BlockCopy(S7.Net.Types.Word.ToByteArray(Stato), 0, answ, 10, 2); + return answ; + } + } + /// + /// Oggetto Modulo Blocco da PLC + /// + protected class PlcModBlock + { + public short Id { get; set; } = 0; + public int SetpointHMI { get; set; } = 0; + public int SetpointPLC { get; set; } = 0; + public int ValMax { get; set; } = 0; + public int ValMin { get; set; } = 0; + public ushort UnitMeasure { get; set; } = 0; + /// + /// Deserializzazione da byte ad oggetto Param + /// + /// + public PlcModBlock(byte[] rawData) + { + // converto i dati da byte grezzi ai 3 componenti... + Id = S7.Net.Types.Int.FromByteArray(rawData.Skip(0).Take(2).ToArray()); + SetpointHMI = S7.Net.Types.DInt.FromByteArray(rawData.Skip(2).Take(4).ToArray()); + SetpointPLC = S7.Net.Types.DInt.FromByteArray(rawData.Skip(6).Take(4).ToArray()); + ValMax = S7.Net.Types.DInt.FromByteArray(rawData.Skip(10).Take(4).ToArray()); + ValMin = S7.Net.Types.DInt.FromByteArray(rawData.Skip(14).Take(4).ToArray()); + UnitMeasure = S7.Net.Types.Word.FromByteArray(rawData.Skip(18).Take(2).ToArray()); + } + /// + /// Converte un singolo item in un array di byte per scrittura su PLC S7 + /// + /// + public byte[] convertToByte() + { + byte[] answ = new byte[20]; + Buffer.BlockCopy(S7.Net.Types.Int.ToByteArray(Id), 0, answ, 0, 2); + Buffer.BlockCopy(S7.Net.Types.DInt.ToByteArray(SetpointHMI), 0, answ, 2, 4); + Buffer.BlockCopy(S7.Net.Types.DInt.ToByteArray(SetpointPLC), 0, answ, 6, 4); + Buffer.BlockCopy(S7.Net.Types.DInt.ToByteArray(ValMax), 0, answ, 10, 4); + Buffer.BlockCopy(S7.Net.Types.DInt.ToByteArray(ValMin), 0, answ, 14, 4); + Buffer.BlockCopy(S7.Net.Types.Word.ToByteArray(UnitMeasure), 0, answ, 18, 2); + return answ; + } + } + #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////