From 8548eb36fc5854cfd34e076c8103fffc1f233a6f Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Thu, 7 May 2020 18:41:35 +0200 Subject: [PATCH] Partial fix NC S7Net for read/write byte arrays --- CMS_CORE_Library/CMS_CORE_Library.csproj | 4 + CMS_CORE_Library/S7Net/Nc_S7Net.cs | 1714 ++------------------- CMS_CORE_Library/packages.config | 4 + SiemensS7/Siemens-S7-Test/TestMainForm.cs | 1 + 4 files changed, 114 insertions(+), 1609 deletions(-) create mode 100644 CMS_CORE_Library/packages.config diff --git a/CMS_CORE_Library/CMS_CORE_Library.csproj b/CMS_CORE_Library/CMS_CORE_Library.csproj index d92774a..389be1e 100644 --- a/CMS_CORE_Library/CMS_CORE_Library.csproj +++ b/CMS_CORE_Library/CMS_CORE_Library.csproj @@ -103,6 +103,9 @@ False libs\CndexLinkDotNet.dll + + ..\packages\S7netplus.0.4.0\lib\net452\S7.Net.dll + False libs\Siemens.Sinumerik.Operate.Services.dll @@ -1115,6 +1118,7 @@ Always + diff --git a/CMS_CORE_Library/S7Net/Nc_S7Net.cs b/CMS_CORE_Library/S7Net/Nc_S7Net.cs index fd87d24..92e26f0 100644 --- a/CMS_CORE_Library/S7Net/Nc_S7Net.cs +++ b/CMS_CORE_Library/S7Net/Nc_S7Net.cs @@ -1,5 +1,5 @@ using CMS_CORE_Library.Models; -using Siemens.Sinumerik.Operate.Services; +using S7.Net; using System; using System.Collections; using System.Collections.Generic; @@ -11,36 +11,29 @@ using System.Net.NetworkInformation; using System.Text; using System.Text.RegularExpressions; using System.Threading; -using System.Windows.Forms; using System.Xml.Linq; using static CMS_CORE_Library.Models.DataStructures; using static CMS_CORE_Library.Nc; using static CMS_CORE_Library.Siemens.MEMORY_ADDRESS; using static CMS_CORE_Library.ToolConfigurations; using static CMS_CORE_Library.Utils.Nc_Utils; -using static Siemens.Sinumerik.Operate.Services.ToolMngmntSvc; + namespace CMS_CORE_Library.S7Net { public class Nc_S7Net : Nc { // Global Constants -#if false - private const string OptionHMINotRunning = "P66"; - - private const string HMIDllName = "Wrapper.dll"; -#endif private const string NcNotFound = "Missing response"; private const string NcNotFound2 = "networking error"; -#if false private const string NcNotFound3 = "Nc not Connected"; - private const string SinumerikEnvVar = "HMI_INSTALL_DIR"; -#endif +#if false private const int IDMinPLCMessage = 700000; private const int IDMaxPLCMessage = 799999; private const int IDMinChannel = 10000; private const int IDMaxChannel = 19999; +#endif #if false // Siemens Path Constants @@ -61,11 +54,7 @@ namespace CMS_CORE_Library.S7Net private const string ACT_PROG_LINES_PATH = "/Channel/ProgramInfo/actPartProgram"; private const string ACT_PROG_NAME_PATH = "/Channel/ProgramPointer/progName"; - private const string MAGAZINE_ACTION_PATH = "DB258.DBB4000"; - // TOOLS - private const string NC_TOOL_FILE_PATH = "//NC/ACT.DIR/TO.DIR/TO_INI.INI"; - private const string LOCALE_TOOL_FILE_PATH = "C:\\CMS\\Active\\TMP\\TO_INI.INI"; #endif // Global Variables @@ -82,36 +71,15 @@ namespace CMS_CORE_Library.S7Net private static string UnitOfMeasure; // Alarms data - private AlarmSvc SiemensAlmSvc = null; - private Alarm[] SiemensAlarms; + private static Dictionary PlcMessages; #if false - // Tool table listeners - private static ToolMngmntSvc SiemensToolSvc = null; - - private static DataSvc MagazinePositionSvc = null; - private static DataSvc MagazineActionsSvc = null; - - private Guid ToolMngmtListGuid = Guid.Empty; - - // Magazine config parameters - private static int MAX_TOOL_NUMER; - - private static int MAX_EDGES_PER_TOOL; - private static int MAX_TOOLS_PER_FAMILY; - private static int MAX_MULTITOOLS_NUMBER; - private static int MAX_TOOLS_PER_MULTITOOL; - private static int MAX_MULTITOOLS_LOCATIONS; - - private static bool LIFE_OPTION_ACTIVE; - private static bool MULTITOOL_OPTION_ACTIVE; private const string PLC_MESSAGES_FILE_PATH = "C:\\Program Files (x86)\\Siemens\\MotionControl\\oem\\sinumerik\\hmi\\lng\\"; private static readonly string AXES_FILE_PATH = AppDomain.CurrentDomain.BaseDirectory + "\\Config\\axesConfig.xml"; // axis data private static List AxesData = new List(); - public static uint multitoolsNumber = 0; public static uint MAX_AXES_FOR_PROCESS = 20; @@ -121,8 +89,11 @@ namespace CMS_CORE_Library.S7Net private static DataSvc AxesSvc; private static DataSvc SelectedProcessSvc; +#endif + private static ushort SelectedProcess = 0; +#if false private static Dictionary MachineAxesPosition = new Dictionary(); private static Dictionary ToGoAxesPosition = new Dictionary(); private static Dictionary InterpAxesPosition = new Dictionary(); @@ -137,6 +108,15 @@ namespace CMS_CORE_Library.S7Net private bool EnableAxes; + private CpuType tipoCpu = CpuType.S71500; + private short rack = 0; + private short slot = 1; + + /// + /// Oggetto PLC da ri-utilizzare... + /// + protected Plc currPLC; + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region Contructor & global methods @@ -198,12 +178,12 @@ namespace CMS_CORE_Library.S7Net return cmsError; // Check connection - Item item = new Item("DB258.DBX0.0"); - DataSvc svc = new DataSvc(); - - svc.Read(item); - Connected = true; + currPLC = new Plc(tipoCpu, this.Ip, this.rack, this.slot); + currPLC.Open(); + if (currPLC.IsConnected) + Connected = true; +#if false // InitializeSiemens objects if (this.EnableAxes) { @@ -224,18 +204,23 @@ namespace CMS_CORE_Library.S7Net new Item() { Path = "DB258.DBX2598.2" }, new Item() { Path = "DB258.DBX2602.2" }, }); - } + } +#endif - //Setup the alarms (Only managed in this way (Siemens Thread) + // Setup the alarms (with DB bitmap) --> PlcMessages if (this.EnableAlarms) { + // PlcMessages +#if false SiemensAlarms = new Alarm[] { }; SiemensAlmSvc = new AlarmSvc(ConvertToSTEPLanguage(SiemensLanguage).ThreeLetterISOLanguageName); SiemensAlmSvc.Subscribe(AlarmsChanged); - Infrastructure.SubscribeLanguageChanged(NcLanguageChanged); + Infrastructure.SubscribeLanguageChanged(NcLanguageChanged); +#endif } +#if false if (this.EnableTools) { // Setup tooltable listeners @@ -259,7 +244,8 @@ namespace CMS_CORE_Library.S7Net MagazineActionsSvc = new DataSvc(); SetupMagazineActionListener(); } - } + } +#endif } catch (Exception ex) { @@ -269,6 +255,7 @@ namespace CMS_CORE_Library.S7Net return NO_ERROR; } +#if false private void ManageSelectedProcess(Guid guid, Item[] item, DataSvcStatus[] status) { SelectedProcess = 0; @@ -344,40 +331,20 @@ namespace CMS_CORE_Library.S7Net } } } - } + } +#endif // Disconnect Method public override CmsError NC_Disconnect() { // Set Connected to FALSE and close the sessions Connected = false; - if (SiemensToolSvc != null) - { - SiemensToolSvc.UnSubscribe(ToolMngmtSvcHandler); - // SiemensToolSvc.Dispose(); - } - if (MagazinePositionSvc != null) - { - MagazinePositionSvc.UnSubscribe(MagazinePositionEnabled_OnChange); - //MagazinePositionSvc = null; - // MagazinePositionSvc.Dispose(); - } - if (MagazineActionsSvc != null) - { - MagazineActionsSvc.UnSubscribe(MagazineAction_OnChange); - //MagazineActionsSvc = null; - // MagazineActionsSvc.Dispose(); - } - - if (this.EnableAxes) - { - if (AxesSvc != null) - AxesSvc.UnSubscribe(ManageAxes); - } + currPLC.Close(); return NO_ERROR; } +#if false private void SetupToolTableData() { DataSvc dataSvc = new DataSvc(); @@ -463,14 +430,18 @@ namespace CMS_CORE_Library.S7Net // Setup magazine actions listener MagazineActionsSvc.Subscribe(MagazineAction_OnChange, magActionsItem); - } + } +#endif private void RemoveLifeParamsFromConfig() { +#if false SiemensToolFieldsConfig.RemoveAll(x => x.Category == "life"); - SiemensEdgesFieldsConfiguration.EdgeConfiguration.RemoveAll(x => x.Category == "life"); + SiemensEdgesFieldsConfiguration.EdgeConfiguration.RemoveAll(x => x.Category == "life"); +#endif } +#if false public override CmsError NC_GetTranslatedPlcMessages(string language, ref Dictionary messages) { string filePath; @@ -551,7 +522,8 @@ namespace CMS_CORE_Library.S7Net } return NO_ERROR; - } + } +#endif #endregion Contructor & global methods @@ -610,14 +582,7 @@ namespace CMS_CORE_Library.S7Net // Set the NC Language public override CmsError NC_WLanguage(CultureInfo Language) { - //CmsError cmsError = CheckConnection(); - //if (cmsError.IsError()) - // return cmsError; - - //// Write write the language - //Infrastructure.CurrentLanguage = ConvertToNCLanguage(Language); - - return NO_ERROR; + return FUNCTION_NOT_ALLOWED_ERROR; } // Get the NC Serial number @@ -803,6 +768,7 @@ namespace CMS_CORE_Library.S7Net /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region PLC High-level data + public override CmsError PLC_RWManageWatchdog() { bool plcWatchdog = false; @@ -3056,45 +3022,45 @@ namespace CMS_CORE_Library.S7Net #region Low level methods - public override CmsError MEM_RWByteList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemByteStart, int Number, ref List Value) + public override CmsError MEM_RWByteList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemByteStart, int Number, ref List Values) { //Check if the NC is Connected CmsError cmsError = CheckConnection(); if (cmsError.IsError()) return cmsError; - //Try to get/set information - try + if (MemType != MEMORY_TYPE.Siemens_DB) { - //Setup variables - DataSvc dataSvc = HighPriorityDataSvc(); - - cmsError = ConvertMemToPath(MemType, MemTable, MemIndex, 0, (bWrite) ? Value.Count : Number, 'B', out string itemString); - if (cmsError.IsError()) - return cmsError; - Item item = new Item(itemString); - - //Read-Write Data - if (bWrite) - { - item.Value = Value.ToArray(); - dataSvc.Write(item); - } - else - { - dataSvc.Read(item); - if (item.Value.GetType().IsArray) - Value = ((object[])item.Value).Cast().ToList(); - else - { - Value.Clear(); - Value.Add((byte)(uint)item.Value); - } - } + return INCORRECT_PARAMETERS_ERROR; } - catch (Exception ex) + + cmsError = null; + + if (bWrite) { - return ManageException(ex); + //Prevent some exceptions + if (Values.Count == 0) + return NO_ERROR; + + currPLC.WriteBytes(DataType.DataBlock, MemTable, MemIndex, Values.ToArray()); + } + else + { + // Prevent some exceptions + if (Number == 0) + return NO_ERROR; + + Values.Clear(); + + var memByteRead = currPLC.ReadBytes(DataType.DataBlock, MemTable, MemIndex, Number); + Values = memByteRead.ToList(); +#if false + // ciclo eda ggiungo i bytes... + foreach (var item in memByteRead) + { + Values.Add(item); + } +#endif } return NO_ERROR; @@ -5095,36 +5061,11 @@ namespace CMS_CORE_Library.S7Net #region Subordinate Private Functions - private DataSvc HighPriorityDataSvc() - { - return new DataSvc() - { - PriorityFlag = SlPriority.highPriority, - Timeout = TimeoutConn - }; - } - - private DataSvc DefaultPriorityDataSvc() - { - return new DataSvc() - { - PriorityFlag = SlPriority.defaultPriority, - Timeout = TimeoutConn - }; - } - - private DataSvc LowPriorityDataSvc() - { - return new DataSvc() - { - PriorityFlag = SlPriority.lowPriority, - Timeout = TimeoutConn - }; - } // Manage the Alarms - Called automatically on changes private void AlarmsChanged(Guid guid, Alarm[] alarms) { +#if false try { if (alarms != null) @@ -5133,15 +5074,18 @@ namespace CMS_CORE_Library.S7Net catch (Exception ex) { Console.WriteLine(ex.Message); - } + } +#endif } private void NcLanguageChanged(string language) { +#if false SiemensAlmSvc.UnSubscribe(AlarmsChanged); // SiemensAlmSvc.Dispose(); SiemensAlmSvc = new AlarmSvc(language); - SiemensAlmSvc.Subscribe(AlarmsChanged); + SiemensAlmSvc.Subscribe(AlarmsChanged); +#endif } private int SetBitValue(int intVal, bool val, int bitIndex) @@ -5235,7 +5179,7 @@ namespace CMS_CORE_Library.S7Net { // faccio ping... - if(testPing() != IPStatus.Success) + if (testPing() != IPStatus.Success) return PLC_IP_NOT_FOUND_ERROR; return NO_ERROR; } @@ -5261,25 +5205,12 @@ namespace CMS_CORE_Library.S7Net //Manage the Exception Launch private CmsError ManageException(Exception ex) { - //Catch the Siemens exceptions - if (ex is OperateMissingOptionException && ex.Message.Contains(OptionHMINotRunning) || ex.Message.Contains("CORBA")) - { - Connected = false; - return SIEMENS_HMI_NOT_RUNNING_ERROR; - } - else if (ex is FileNotFoundException && ex.Message.Contains(HMIDllName)) - { - return SIEMENS_ENVIRONMENT_NOT_FOUND_ERROR; - } - else if (ex.Message.Contains(NcNotFound) || ex.Message.Contains(NcNotFound2) || ex.Message.Contains(NcNotFound3) || ex.Message.Contains("Read() failed:")) + // Catch the S7Net exceptions + if (ex.Message.Contains(NcNotFound) || ex.Message.Contains(NcNotFound2) || ex.Message.Contains(NcNotFound3) || ex.Message.Contains("Read() failed:")) { Connected = false; return NOT_CONNECTED_ERROR; } - else if (ex.Message.Contains("m_pSlToolMngmntSvc")) - { - return SIEMENS_TOOL_TABLE_ERROR; - } else { return CmsError.InternalError(ex.Message, ex); @@ -5300,12 +5231,13 @@ namespace CMS_CORE_Library.S7Net if (cmsError.IsError()) return cmsError; - //Read oly one time every X seconds + // Read oly one time every X seconds if (DateTime.Now - Last_Static_Read > new TimeSpan(NC_MIN_SEC_READ_STATIC_DATA * TimeSpan.TicksPerSecond)) { //Try to get information try { +#if false DataSvc ReadStaticData = DefaultPriorityDataSvc(); Item ItemNcType = new Item(NC_TYPE_PATH); Item ItemNcVersion = new Item(NC_VERSION_PATH); @@ -5342,7 +5274,8 @@ namespace CMS_CORE_Library.S7Net UnitOfMeasure = (uint)ItemUnitOfMeasure.Value == 0 ? MILLIMETERS : INCHES; //Save NOW - Last_Static_Read = DateTime.Now; + Last_Static_Read = DateTime.Now; +#endif } catch (Exception ex) { @@ -5512,1472 +5445,36 @@ namespace CMS_CORE_Library.S7Net /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - #region Siemens Tool private functions - private string[] ReadToolTableFile() - { - // Create a configuration file - FileStream f = new FileStream(LOCALE_TOOL_FILE_PATH, FileMode.Create); - - // Read siemens configuration file - CmsError cmsError = FILES_RProgramToFile(NC_TOOL_FILE_PATH, f); - f.Close(); - - return File.ReadAllLines(LOCALE_TOOL_FILE_PATH); - } - - private void MagazinePositionEnabled_OnChange(Guid guid, Item[] items, DataSvcStatus[] status) - { - int i = 0; - // Parse each new item - foreach (Item item in items) - { - if (status[i].Ok) - { - // Get Ids - string[] ids = item.SymbolicName.Split('_'); - int magazineId = Convert.ToInt32(ids[0]); - int positionId = Convert.ToInt32(ids[1]); - - foreach (PositionModel pos in MagazinePositionsData) - { - // Find position - if (pos.PositionId == positionId && pos.MagazineId == magazineId) - { - // Set Value - pos.Disabled = GetBitValue(Convert.ToInt32(item.Value), 0); - } - } - } - - i++; - } - } - - private void MagazineAction_OnChange(Guid guid, Item item, DataSvcStatus status) - { - CmsError cmsError = NO_ERROR; - if (Connected == false) - NC_Connect(); - - List wordList = new List(); - cmsError = MEM_RWWordList(R, 0, MAGAZINE_ACTION.MemType, MAGAZINE_ACTION.Address, MAGAZINE_ACTION.SubAddress + 2, 6, ref wordList); - if (cmsError.IsError()) - { - MagazineAction = new MagazineActionModel() - { - Action = MAGAZINE_ACTIONS.NOT_READY, - Tool = null, - SecondTool = null, - OriginMagazine = 0, - OriginPosition = 0, - DestinationMagazine = 0, - DestinationPosition = 0 - }; - } - else - { - MagazineActionToolModel tool = null; - MagazineActionToolModel secondTool = null; - - // Get the first tool if exists - ushort toolId = wordList[0]; - if (toolId != 0) - tool = ToolTableData - .Where(x => x.Id == toolId) - .Select(x => new MagazineActionToolModel() - { - Id = x.Id, - IsMultitool = false, - Name = x.FamilyName, - ToolType = x.ToolType - }).FirstOrDefault(); - - // Get the second tool if exists - toolId = wordList[1]; - if (toolId != 0) - secondTool = ToolTableData - .Where(x => x.Id == toolId) - .Select(x => new MagazineActionToolModel() - { - Id = x.Id, - IsMultitool = false, - Name = x.FamilyName, - ToolType = x.ToolType - }).FirstOrDefault(); - - // Populate model with new data - MagazineAction = new MagazineActionModel() - { - Action = (MAGAZINE_ACTIONS)Convert.ToInt32(item.Value), - Tool = tool, - SecondTool = secondTool, - OriginMagazine = wordList[4], - OriginPosition = wordList[5], - DestinationMagazine = wordList[2], - DestinationPosition = wordList[3] - }; - } - } - - private void PopulateToolsTableFromFileStrings(string[] toolsStrings) - { - SiemensEdgesConfiguration toolEdgesConfig = new SiemensEdgesConfiguration(); - - int lastFirstId = 0; - int lastSecondId = 0; - int toolIndex = 0; - int edgeIndex = 0; - // int multitoolIndex = 0; - - // Foreach string - for (int i = 0; i < toolsStrings.Count(); i++) - { - string actualString = toolsStrings[i]; // Get actual string data - - GetIdsFromSiemensToolString(actualString, out int firstId, out int secondId); // find ids from current string - - if (firstId != 0) - { - // If tool is changed, change current index tool - if (lastFirstId != firstId) - { - toolIndex = ToolTableData.FindIndex(x => x.Id == firstId); - // multitoolIndex = MultitoolsData.FindIndex(x => x.Id == firstId); - } - - // Check if the key string has only 1 id - if (secondId == 0) - { - switch (GetStringUntilOrEmpty(actualString)) // Cut string until [ character - { - case "$TC_TP1": - { - ToolTableData.Add(new SiemensToolModel() - { - Id = firstId, - ChildId = Convert.ToInt32(actualString.Split('=').LastOrDefault()) - }); - // Update Index - toolIndex = ToolTableData.Count - 1; - } - break; - - case "$TC_TP2": - { - string familyName = actualString.Split('=').LastOrDefault().Replace("\"", ""); - ToolTableData[toolIndex].FamilyName = familyName; - // Add new family with new tool - AddOrUpdateFamilyChild(familyName); - } - break; - - case "$TC_TP3": - ToolTableData[toolIndex].LeftSize = Convert.ToByte(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP4": - ToolTableData[toolIndex].RightSize = Convert.ToByte(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP7": - ToolTableData[toolIndex].MagazinePositionType = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP8": - { - // Get tool status and read each bit - int toolStatus = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - - ToolTableData[toolIndex].IsActive = GetBitValue(toolStatus, 0); - ToolTableData[toolIndex].IsEnabled = GetBitValue(toolStatus, 1); - ToolTableData[toolIndex].IsInhibited = GetBitValue(toolStatus, 2); - ToolTableData[toolIndex].IsMeasured = GetBitValue(toolStatus, 3); - ToolTableData[toolIndex].PreAlarm = GetBitValue(toolStatus, 4); - ToolTableData[toolIndex].InChangeTool = GetBitValue(toolStatus, 5); - ToolTableData[toolIndex].InFixedPlace = GetBitValue(toolStatus, 6); - ToolTableData[toolIndex].IsInUse = GetBitValue(toolStatus, 7); - } - break; - - case "$TC_TP9": - ToolTableData[toolIndex].LifeType = (SIEMENS_LIFE_TYPE)Convert.ToInt32(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP_MAX_VELO": - ToolTableData[toolIndex].MaxSpeed = Convert.ToDouble(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - //case "$TC_TP_MAX_ACC": - // ToolTableData[toolIndex].MaxAcceleration = Convert.ToDouble(actualString.Split('=').LastOrDefault(), numberFormat); - // break; - // Custom CMS fields - case "$TC_TPC1": - ToolTableData[toolIndex].Load = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC2": - ToolTableData[toolIndex].Gamma = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC3": - ToolTableData[toolIndex].MaxLoadPctAutoload = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC4": - ToolTableData[toolIndex].MinLoadPctAutoload = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC5": - ToolTableData[toolIndex].ToolTypeCode = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - //*SHANK FIX*case "$TC_MTPN": - // { - // // Insert new multitool - // MultitoolsData.Add(new ShankModel() - // { - // Id = firstId, - // MaxChilds = Convert.ToInt32(actualString.Split('=').LastOrDefault()) - // }); - // // Update Index - // multitoolIndex = MultitoolsData.Count - 1; - // } - // break; - - //case "$TC_MTP2": - // MultitoolsData[multitoolIndex].Name = actualString.Split('=').LastOrDefault().Replace("\"", ""); - // break; - - //case "$TC_MTP3": - // MultitoolsData[multitoolIndex].LeftSize = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - // break; - - //case "$TC_MTP4": - // MultitoolsData[multitoolIndex].RightSize = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - // break; - - //case "$TC_MTP7": - // MultitoolsData[multitoolIndex].MagazinePositionType = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - // break; - - //case "$TC_MTP8": - // { - // // Get tool status - // byte toolStatus = Convert.ToByte(actualString.Split('=').LastOrDefault()); - - // MultitoolsData[multitoolIndex].IsEnabled = GetBitValue(toolStatus, 1); - // MultitoolsData[multitoolIndex].IsInhibited = GetBitValue(toolStatus, 2); - // MultitoolsData[multitoolIndex].InChangeTool = GetBitValue(toolStatus, 3); - // MultitoolsData[multitoolIndex].InFixedPlace = GetBitValue(toolStatus, 4); - // MultitoolsData[multitoolIndex].InUse = GetBitValue(toolStatus, 7); - // } - // break; - - case "$TC_MAP1": - { - // Magazine type - SiemensMagazineConfigModel mag = MagazineConfig.Where(x => x.Id == firstId).FirstOrDefault(); - if (mag == null) - { - bool active = true; - SIEMENS_MAGAZINE_TYPE type = (SIEMENS_MAGAZINE_TYPE)Convert.ToInt32(actualString.Split('=').LastOrDefault()); - if (type == SIEMENS_MAGAZINE_TYPE.MAGAZINE_LOADING_STATION || type == SIEMENS_MAGAZINE_TYPE.MAGAZINE_TOOL_BUFFER) - active = false; - - MagazineConfig.Add(new SiemensMagazineConfigModel() - { - Id = (short)firstId, - Type = (SIEMENS_MAGAZINE_TYPE)Convert.ToInt32(actualString.Split('=').LastOrDefault()), - LoadingIsActive = active - }); - } - else - mag.Type = (SIEMENS_MAGAZINE_TYPE)Convert.ToInt32(actualString.Split('=').LastOrDefault()); - } - break; - - case "$TC_MAP2": - { - // Magazine Name - SiemensMagazineConfigModel mag = MagazineConfig.Where(x => x.Id == firstId).FirstOrDefault(); - if (mag != null) - mag.Name = actualString.Split('=').LastOrDefault().Replace("\"", ""); - } - break; - - case "$TC_MAP7": - { - // Add max number of magazine positions - SiemensMagazineConfigModel mag = MagazineConfig.Where(x => x.Id == firstId).FirstOrDefault(); - if (mag != null) - mag.MaxPositions = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - } - break; - } - } - else // If the key string has 2 ids ex: $TC_DP1[1,3] - { - // If secondId is changed, change current edge id - if (lastSecondId != secondId && toolIndex >= 0) - edgeIndex = ToolTableData[toolIndex].EdgesData.FindIndex(x => x.Id == secondId); - - switch (GetStringUntilOrEmpty(actualString)) - { - case "$TC_DP1": - { - if (secondId == 1) // Read tool type from first edge type - ToolTableData[toolIndex].ToolType = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - // Add new edge - ToolTableData[toolIndex].EdgesData.Add(new EdgeModel() { Id = secondId }); - edgeIndex = ToolTableData[toolIndex].EdgesData.Count - 1; // Update Index - } - break; - - case "$TC_MOP1": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.TIME) - { - tool - .EdgesData[edgeIndex] - .PreAlmLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP2": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.TIME) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .ResidualLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP11": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.TIME) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .NominalLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP3": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.COUNT) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .PreAlmLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP4": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.COUNT) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .ResidualLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP13": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.COUNT) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .NominalLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP6": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.WEAR) - { - tool - .EdgesData[edgeIndex] - .ResidualLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP15": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.WEAR) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .NominalLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP5": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - if (tool.LifeType == SIEMENS_LIFE_TYPE.WEAR) - { - ToolTableData[toolIndex] - .EdgesData[edgeIndex] - .PreAlmLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - //case "$TC_DP2": - case "$TC_DP3": - case "$TC_DP4": - case "$TC_DP5": - case "$TC_DP6": - case "$TC_DP7": - case "$TC_DP8": - case "$TC_DP9": - case "$TC_DP10": - case "$TC_DP11": - case "$TC_DP12": - case "$TC_DP13": - case "$TC_DP14": - case "$TC_DP15": - case "$TC_DP16": - case "$TC_DP17": - case "$TC_DP21": - case "$TC_DP22": - case "$TC_DP23": - case "$TC_DP24": - case "$TC_DPV": - case "$TC_DPV3": - case "$TC_DPV4": - case "$TC_DPV5": - case "$TC_DPNT": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - //// Clear tool - //tool - // .EdgesData[edgeIndex] - // .EdgeAdditionalParams - // .Clear(); - // Get tool type additional parameters - if (toolEdgesConfig.EdgesConfig.ContainsKey(tool.ToolType)) - { - // Find if actual param exist for the tool type - List toolTypeConfig = toolEdgesConfig.EdgesConfig[tool.ToolType]; - EdgeConfigModel paramConfig = toolTypeConfig - .Where(x => x.Path == (GetStringUntilOrEmpty(actualString) + "[")) - .FirstOrDefault(); - - // If Exist - if (paramConfig != null) - { - // Add additional param - tool - .EdgesData[edgeIndex] - .EdgeAdditionalParams - .Add(paramConfig.Name, Convert.ToDouble(actualString.Split('=').LastOrDefault(), numberFormat)); - } - } - } - break; - - case "$TC_DP25": - { - SiemensToolModel tool = ToolTableData[toolIndex]; - // Sets tool value only with the first edge value - if (tool.EdgesData[edgeIndex].Id == 1) - { - // Get tool general information - var toolInfo = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - - // Convert rotation data - ROTATION rotation = ROTATION.NONE; - if (GetBitValue(toolInfo, 8)) - rotation = ROTATION.CLOCKWHISE; - else if (GetBitValue(toolInfo, 9)) - rotation = ROTATION.COUNTERCLOCKWHISE; - // Set new data - - tool.Cooling1 = GetBitValue(toolInfo, 10); - tool.Cooling2 = GetBitValue(toolInfo, 11); - tool.Rotation = rotation; - } - } - break; - - case "$TC_MPP1": - { - MagazinePositionsData.Add(new PositionModel() - { - PositionId = secondId, - MagazineId = firstId, - PhysicalType = (POSITION_TYPE)Convert.ToInt32(actualString.Split('=').LastOrDefault()) - }); - } - break; - - case "$TC_MPP2": - { - int index = MagazinePositionsData.FindIndex(x => x.PositionId == secondId && x.MagazineId == firstId); - if (index >= 0) - MagazinePositionsData[index].Type = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - //*SHANK FIX*MagazinePositionsData.Add(new PositionModel() - //{ - // PositionId = secondId, - // MagazineId = firstId, - // Type = Convert.ToInt32(actualString.Split('=').LastOrDefault()) - //}); - } - break; - - case "$TC_MPP4": - { - int index = MagazinePositionsData.FindIndex(x => x.PositionId == secondId && x.MagazineId == firstId); - if (index >= 0) - { - MagazinePositionsData[index].Disabled = GetBitValue(Convert.ToInt32(actualString.Split('=').LastOrDefault()), 0); - MagazinePositionsData[index].Occupied = GetBitValue(Convert.ToInt32(actualString.Split('=').LastOrDefault()), 2); - } - } - break; - - case "$TC_MPP6": - { - int toolId = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - if (toolId != 0) - { - MoveToolInMagazineList(toolId, firstId, secondId); - } - } - break; - - //*SHANK FIX*case "$TC_MTPP6": - // { - // // Find tool by id - // int toolId = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - // if (toolId != 0) - // { - // // Add new multitool child - // MultitoolsData[multitoolIndex].ChildsTools.Add(new ShankChildModel() - // { - // Id = toolId, - // MultitoolId = secondId - // }); - // } - // } - // break; - - case "$TC_MDP1": - { - // Add max number of magazine positions - SiemensMagazineConfigModel mag = MagazineConfig.Where(x => x.Id == firstId).FirstOrDefault(); - if (mag != null) - mag.LoadPosition = secondId; - } - break; - } - } - } - } - } - - private SiemensToolModel CreateToolFromId(int toolIdToGet, string[] toolsStrings) - { - SiemensEdgesConfiguration toolEdgesConfig = new SiemensEdgesConfiguration(); - SiemensToolModel outputTool = new SiemensToolModel(); - int lastSecondId = 0; - int edgeIndex = 0; - - // Foreach string - for (int i = 0; i < toolsStrings.Count(); i++) - { - string actualString = toolsStrings[i]; // Get actual string data - - GetIdsFromSiemensToolString(actualString, out int firstId, out int secondId); // find ids from current string - - if (firstId != 0 && firstId == toolIdToGet) - { - if (secondId == 0) - { - switch (GetStringUntilOrEmpty(actualString)) // Cut string until [ character - { - case "$TC_TP1": - { - outputTool = new SiemensToolModel() - { - Id = firstId, - ChildId = Convert.ToInt32(actualString.Split('=').LastOrDefault()) - }; - } - break; - - case "$TC_TP2": - { - string familyName = actualString.Split('=').LastOrDefault().Replace("\"", ""); - outputTool.FamilyName = familyName; - // Add new family with new tool - AddOrUpdateFamilyChild(familyName); - } - break; - - case "$TC_TP3": - outputTool.LeftSize = Convert.ToByte(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP4": - outputTool.RightSize = Convert.ToByte(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP7": - outputTool.MagazinePositionType = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP8": - { - // Get tool status and read each bit - int toolStatus = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - - outputTool.IsActive = GetBitValue(toolStatus, 0); - outputTool.IsEnabled = GetBitValue(toolStatus, 1); - outputTool.IsInhibited = GetBitValue(toolStatus, 2); - outputTool.IsMeasured = GetBitValue(toolStatus, 3); - outputTool.PreAlarm = GetBitValue(toolStatus, 4); - outputTool.InChangeTool = GetBitValue(toolStatus, 5); - outputTool.InFixedPlace = GetBitValue(toolStatus, 6); - outputTool.IsInUse = GetBitValue(toolStatus, 7); - } - break; - - case "$TC_TP9": - outputTool.LifeType = (SIEMENS_LIFE_TYPE)Convert.ToInt32(actualString.Split('=').LastOrDefault()); - break; - - case "$TC_TP_MAX_VELO": - outputTool.MaxSpeed = Convert.ToDouble(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - //case "$TC_TP_MAX_ACC": - // outputTool.MaxAcceleration = Convert.ToDouble(actualString.Split('=').LastOrDefault(), numberFormat); - // break; - // Custom CMS fields - case "$TC_TPC1": - outputTool.Load = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC2": - outputTool.Gamma = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC3": - outputTool.MaxLoadPctAutoload = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC4": - outputTool.MinLoadPctAutoload = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - - case "$TC_TPC5": - outputTool.ToolTypeCode = Convert.ToInt32(actualString.Split('=').LastOrDefault(), numberFormat); - break; - } - } - else - { - // If secondId is changed, change current edge id - if (lastSecondId != secondId) - edgeIndex = outputTool.EdgesData.FindIndex(x => x.Id == secondId); - - switch (GetStringUntilOrEmpty(actualString)) - { - case "$TC_DP1": - { - if (secondId == 1) // Read tool type from first edge type - outputTool.ToolType = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - // Add new edge - outputTool.EdgesData.Add(new EdgeModel() { Id = secondId }); - edgeIndex = outputTool.EdgesData.Count - 1; // Update Index - } - break; - - case "$TC_MOP1": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.TIME) - { - outputTool - .EdgesData[edgeIndex] - .PreAlmLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP2": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.TIME) - { - outputTool - .EdgesData[edgeIndex] - .ResidualLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP11": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.TIME) - { - outputTool - .EdgesData[edgeIndex] - .NominalLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP3": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.COUNT) - { - outputTool - .EdgesData[edgeIndex] - .PreAlmLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP4": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.COUNT) - { - outputTool - .EdgesData[edgeIndex] - .ResidualLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP13": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.COUNT) - { - outputTool - .EdgesData[edgeIndex] - .NominalLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP6": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.WEAR) - { - outputTool - .EdgesData[edgeIndex] - .ResidualLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP15": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.WEAR) - { - outputTool - .EdgesData[edgeIndex] - .NominalLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - case "$TC_MOP5": - { - if (outputTool.LifeType == SIEMENS_LIFE_TYPE.WEAR) - { - outputTool - .EdgesData[edgeIndex] - .PreAlmLife = Convert.ToDouble(actualString.Split('=').LastOrDefault()); - } - } - break; - - //case "$TC_DP2": - case "$TC_DP3": - case "$TC_DP4": - case "$TC_DP5": - case "$TC_DP6": - case "$TC_DP7": - case "$TC_DP8": - case "$TC_DP9": - case "$TC_DP10": - case "$TC_DP11": - case "$TC_DP12": - case "$TC_DP13": - case "$TC_DP14": - case "$TC_DP15": - case "$TC_DP16": - case "$TC_DP17": - case "$TC_DP21": - case "$TC_DP22": - case "$TC_DP23": - case "$TC_DP24": - case "$TC_DPV": - case "$TC_DPV3": - case "$TC_DPV4": - case "$TC_DPV5": - case "$TC_DPNT": - { - //// Clear tool - //outputTool - // .EdgesData[edgeIndex] - // .EdgeAdditionalParams - // .Clear(); - // Get tool type additional parameters - if (toolEdgesConfig.EdgesConfig.ContainsKey(outputTool.ToolType)) - { - // Find if actual param exist for the tool type - List toolTypeConfig = toolEdgesConfig.EdgesConfig[outputTool.ToolType]; - EdgeConfigModel paramConfig = toolTypeConfig - .Where(x => x.Path == (GetStringUntilOrEmpty(actualString) + "[")) - .FirstOrDefault(); - - // If Exist - if (paramConfig != null) - { - // Add additional param - outputTool - .EdgesData[edgeIndex] - .EdgeAdditionalParams - .Add(paramConfig.Name, Convert.ToDouble(actualString.Split('=').LastOrDefault(), numberFormat)); - } - } - } - break; - - case "$TC_DP25": - { - // Sets tool values with first edge value - if (outputTool.EdgesData[edgeIndex].Id == 1) - { - // Get tool general information - var toolInfo = Convert.ToInt32(actualString.Split('=').LastOrDefault()); - - // Convert rotation data - ROTATION rotation = ROTATION.NONE; - if (GetBitValue(toolInfo, 8)) - rotation = ROTATION.CLOCKWHISE; - else if (GetBitValue(toolInfo, 9)) - rotation = ROTATION.COUNTERCLOCKWHISE; - // Set new data - - outputTool.Cooling1 = GetBitValue(toolInfo, 10); - outputTool.Cooling2 = GetBitValue(toolInfo, 11); - outputTool.Rotation = rotation; - } - } - break; - } - } - } - } - - return outputTool; - } - - private EdgeModel SetUpEdgeData(string[] toolsString, SIEMENS_LIFE_TYPE lifeType, int toolId, int edgeId) - { - EdgeModel edgeData = new EdgeModel - { - Id = edgeId - }; - - switch (lifeType) - { - case SIEMENS_LIFE_TYPE.TIME: - { - edgeData.ResidualLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP2[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - edgeData.NominalLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP11[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - edgeData.PreAlmLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP1[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - } - break; - - case SIEMENS_LIFE_TYPE.COUNT: - { - edgeData.ResidualLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP4[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - edgeData.NominalLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP13[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - edgeData.PreAlmLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP3[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - } - break; - - case SIEMENS_LIFE_TYPE.WEAR: - { - // TODO: Check params - edgeData.ResidualLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP6[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - edgeData.NominalLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP15[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - edgeData.PreAlmLife = Convert.ToDouble(toolsString.FirstOrDefault(X => X.StartsWith("$TC_MOP5[" + toolId + "," + edgeId)).Split('=').LastOrDefault(), numberFormat); - } - break; - } - - return edgeData; - } - - private void ToolMngmtSvcHandler(Guid guid, ToolInfo[] toolInfos) - { - for (int i = 0; i < toolInfos.Count(); i++) - { // Choose behavior based on the actions5 - ActionsHandler(toolInfos[i]); - } - } - - private void ActionsHandler(ToolInfo toolInfo) - { - switch (toolInfo.action) - { - case ToolAction.TOOL_DELETED: - { - // Find tool - SiemensToolModel tool = ToolTableData.FirstOrDefault(t => t.Id == toolInfo.tNo); - if (tool != null) - { - // Delete Families - SiemensToolModel sameFamilyTool = ToolTableData.FirstOrDefault(x => x.FamilyName == tool.FamilyName && x.Id != tool.Id); - if (sameFamilyTool == null) - { - FamiliesData.RemoveAll(x => x.Name == tool.FamilyName); - } - - // Dismount tool if is mounted in magazine - if (MagazineMountedTools.FirstOrDefault(x => x.ToolId == tool.Id) != null) - MoveToolInMagazineList(tool.Id, 0, 0); - - // Remove - ToolTableData.Remove(tool); - } - //*SHANK FIX* else if (MULTITOOL_OPTION_ACTIVE) - // { - // // if tool not exist, find into shanks - // ShankModel shank = MultitoolsData.FirstOrDefault(t => t.Id == toolInfo.tNo); - // if (shank != null) - // try - // { - // Item item = new Item(string.Format("/Tool/MT/multitoolMyPlace[u1, {0}]", toolInfo.tNo)); - // DataSvc dataSvc = new DataSvc(); - // dataSvc.Read(item); - // } - // catch (Exception ex) - // { - // MultitoolsData.Remove(shank); - // } - // } - } - break; - - case ToolAction.TOOL_CREATED: - { - string[] toolsStrings = ReadToolTableFile(); - - SiemensToolModel newTool = CreateToolFromId((int)toolInfo.tNo, toolsStrings); - - if (newTool != null) - { - ToolTableData.Add(newTool); - AddOrUpdateFamilyChild(newTool.FamilyName); - - // Read magazine id and position id from NC - DataSvc dataSvc = new DataSvc(); - Item[] item = new Item[] { - new Item(){ Path = string.Format("/Tool/Data/toolInMag[{0}]", (int)toolInfo.tNo) }, - new Item(){ Path = string.Format("/Tool/Data/toolInPlace[{0}]", toolInfo.tNo) } - }; - dataSvc.Read(item); - - int magazineId = Convert.ToInt32(item[0].Value); - int positionId = Convert.ToInt32(item[1].Value); - - if (magazineId != 0 && positionId != 0) - MoveToolInMagazineList(newTool.Id, magazineId, positionId); - } - //*SHANK FIX*else if (MULTITOOL_OPTION_ACTIVE) - //{ - // ShankModel newMultitool = CreateMultitoolFromId((int)toolInfo.tNo, toolsStrings); - // MultitoolsData.Add(newMultitool); - //} - } - break; - - case ToolAction.TOOL_DATA_CHANGED: - { - string[] toolsStrings = ReadToolTableFile(); - - // Find edited toolIndex - int toolIndex = ToolTableData.FindIndex(t => t.Id == toolInfo.tNo); - // If the id belongs to a tool - if (toolIndex != -1) - { - // Get tool new data - SiemensToolModel newTool = CreateToolFromId((int)toolInfo.tNo, toolsStrings); - - // *ASSISTED TOOLING FIX* - // After one reset in an assisted tooling procedure siemens send a "tool_data_changed" message - // Check if tool is not mounted and remove from magazine - - - // Read magazine id and position id from NC - DataSvc dataSvc = new DataSvc(); - Item[] item = new Item[] { - new Item(){ Path = string.Format("/Tool/Data/toolInMag[{0}]", (int)toolInfo.tNo) }, - new Item(){ Path = string.Format("/Tool/Data/toolInPlace[{0}]", toolInfo.tNo) } - }; - dataSvc.Read(item); - - int magazineId = Convert.ToInt32(item[0].Value); - int positionId = Convert.ToInt32(item[1].Value); - - MoveToolInMagazineList(newTool.Id, magazineId, positionId); - - // Replace the element - if (newTool != null) - ToolTableData[toolIndex] = newTool; - } - - //*SHANK FIX*else if (MULTITOOL_OPTION_ACTIVE) - //{ - // // Find if its a multitool - // int multitoolIndex = MultitoolsData.FindIndex(t => t.Id == toolInfo.tNo); - - // if (multitoolIndex != -1) - // { - // // Create model with new data - // ShankModel newShank = CreateMultitoolFromId((int)toolInfo.tNo, toolsStrings); - // // Replace element - // if (newShank != null) - // MultitoolsData[multitoolIndex] = newShank; - // } - // else - // { - // ShankModel newMultitool = CreateMultitoolFromId((int)toolInfo.tNo, toolsStrings); - // MultitoolsData.Add(newMultitool); - // } - //} - - Array.Clear(toolsStrings, 0, toolsStrings.Count()); - } - break; - - case ToolAction.TOOL_MOVED: - { - int magazineId = 0; - int positionId = 0; - - var tool = GetToolOrMultitool(Convert.ToInt32(toolInfo.tNo)); - if (tool is SiemensToolModel) - { - try - { - // Read magazine id and position id from NC - DataSvc dataSvc = new DataSvc(); - Item[] item = new Item[] { - new Item(){ Path = string.Format("/Tool/Data/toolInMag[{0}]", (int)toolInfo.tNo) }, - new Item(){ Path = string.Format("/Tool/Data/toolInPlace[{0}]", toolInfo.tNo) } - }; - dataSvc.Read(item); - - magazineId = Convert.ToInt32(item[0].Value); - positionId = Convert.ToInt32(item[1].Value); - - } - catch (Exception ex) - { - magazineId = 0; - positionId = 0; - } - } - - MoveToolInMagazineList((int)toolInfo.tNo, magazineId, positionId); - - // moved MULTITOOL handler, add when Siemens Library bugs have been fixed - //*SHANK FIX*catch (Exception ex) - //{ - // int shankIdx = MultitoolsData.FindIndex(x => x.Id == toolInfo.tNo); - // if (shankIdx < 0) - // { - // // Check that the move is inside a multitool - // item = new Item() { Path = string.Format("/Tool/Data/toolInMultitool[{0}]", toolInfo.tNo) }; - // dataSvc.Read(item); - // // Check if readed multitool id exists - // int mtIndex = MultitoolsData.FindIndex(x => x.Id == Convert.ToInt32(item.Value)); - // if (mtIndex >= 0) - // { - // int childIndex = MultitoolsData[mtIndex].ChildsTools.FindIndex(x => x.Id == toolInfo.tNo); - // if (childIndex < 0) - // { - // MultitoolsData[mtIndex].ChildsTools.Add(new ShankChildModel() - // { - // Id = Convert.ToInt32(toolInfo.tNo), - // MultitoolId = MultitoolsData[mtIndex].ChildsTools.Count + 1 // TODO ADD TOOL DATA OR REMOVE - // }); - // } - // } - // } - //} - } - break; - } - } - - private void MoveToolInMagazineList(int toolId, int magazineId, int positionId) - { - // If load magazine != 0, add new tool - if (magazineId != 0 && positionId != 0) - { - // Find tool by id - var tool = GetToolOrMultitool(toolId); - if (tool != null) - { - if (magazineId == 9998) - { - // Get old tool position - MountedToolModel toolMounted = MagazineMountedTools - .Where(x => x.ToolId == toolId) - .FirstOrDefault(); - - if (toolMounted != null) - { - // Set magazine position as occupied - var position = MagazinePositionsData.Where(x => - x.MagazineId == toolMounted.MagazineId - && x.PositionId == toolMounted.PositionId) - .FirstOrDefault(); - - if (position != null) - position.Occupied = true; - } - } - else - { - // Free a position - var position = MagazinePositionsData.Where(x => x.MagazineId == magazineId && x.PositionId == positionId).FirstOrDefault(); - if (position != null) - position.Occupied = false; - } - - // Remove tool from list - MagazineMountedTools.RemoveAll(mountTool => PositionContainsTool(mountTool, toolId)); - - // Find if position exist - MountedToolModel mounted = MagazineMountedTools - .Where(x => x.MagazineId == magazineId && x.PositionId == positionId) - .FirstOrDefault(); - - if (mounted != null) - return; - - // Add tool to list - MagazineMountedTools.Add(new MountedToolModel() - { - MagazineId = magazineId, - PositionId = positionId, - ToolId = toolId - }); - } - } - else - { - // Remove tool from list - MagazineMountedTools.RemoveAll(mountTool => PositionContainsTool(mountTool, toolId)); - } - } - - private int GetLastToolId() - { - Item item = new Item() - { - Path = "/Tool/Catalogue/TnumWZV" - }; - - DataSvc dataSvc = new DataSvc(); - dataSvc.Read(item); - - return Convert.ToInt32(item.Value); - - // First implementation before the path was found - //Get tool ids - //List toolIds = ToolTableData.Select(x => x.Id).ToList(); - //// Sort tools ids array - //toolIds.Sort((x, y) => x.CompareTo(y)); - - //if (toolIds.Count == 0) - // return 1; - - //for (int i = 0; i < toolIds.Count - 1; i++) - //{ - // // If next id is different than previus + 1, there is an empty space in the array of numbers - // if (toolIds[i] + 1 != toolIds[i + 1]) - // { - // // Check if the empty space is a multitool - // ShankModel shank = MultitoolsData.Where(x => x.Id == (toolIds[i] + 1)).FirstOrDefault(); - // if (shank == null) - // { - // // return new id - // return toolIds[i] + 1; - // } - // } - //} - //// If there isn't an empty space return max id + 1 - //return toolIds[toolIds.Count - 1] + 1; - } - - private int GetLastMultitoolId() - { - Item item = new Item() - { - Path = "/Tool/MTCatalogue/MTnumWZV" - }; - - DataSvc dataSvc = new DataSvc(); - dataSvc.Read(item); - - return Convert.ToInt32(item.Value); - } - - private int GetLastEdgeId(int toolId) - { - // Get edgeIds ids - List edgeIds = ToolTableData.Where(x => x.Id == toolId).FirstOrDefault()?.EdgesData.Select(x => x.Id).ToList(); - // Sort tools ids array - edgeIds.Sort((x, y) => x.CompareTo(y)); - - if (edgeIds.Count == 0) - return 1; - - for (int i = 0; i < edgeIds.Count - 1; i++) - { - // If next id is different than previus + 1, there is an empty space in the array of numbers - if (edgeIds[i] + 1 != edgeIds[i + 1]) - { - // return new id - return edgeIds[i] + 1; - } - } - - // If there isn't an empty space return max id + 1 - return edgeIds[edgeIds.Count - 1] + 1; - } - - private void AddOrUpdateFamilyChild(string familyName) - { - // Find family - int familyIndex = FamiliesData.FindIndex(x => x.Name == familyName); - if (familyIndex < 0) - { - // Create family and add child - FamiliesData.Add(new FamilyModel() - { - Name = familyName - }); - } - } - - private static void GetIdsFromSiemensToolString(string text, out int toolId, out int edgeId) - { - toolId = 0; - edgeId = 0; - - if (!string.IsNullOrWhiteSpace(text)) - { - // Get indexes from string - int startingData = text.IndexOf("[", StringComparison.Ordinal) + 1; - int secondData = text.IndexOf(",", StringComparison.Ordinal) + 1; - int endingData = text.IndexOf("]", StringComparison.Ordinal); - // If the string is matching with the pattern STRING[N]=N or STRING[N,M]=N - if (startingData > 0 && endingData > 0) - // STRING[N,M]=N case - if (secondData > 0) - { - toolId = Convert.ToInt32(text.Substring(startingData, secondData - (startingData + 1))); - edgeId = Convert.ToInt32(text.Substring(secondData, endingData - (secondData))); - } - else // STRING[N]=N case - { - toolId = Convert.ToInt32(text.Substring(startingData, endingData - (startingData))); - edgeId = 0; - } - } - } - - #endregion Siemens Tool private functions - - #region Multitool Private region - - private void MultitoolsNumber_OnChange(Guid guid, Item item, DataSvcStatus status) - { - // Check if an element has been deleted - if ((uint)item.Value < multitoolsNumber) - { - // for each saved multitool - for (int i = 0; i < MultitoolsData.Count; i++) - { - try - { - // Check if exists - Item toolData = new Item(string.Format("/Tool/MT/multitoolMyPlace[u1, {0}]", MultitoolsData[i].Id)); - DataSvc dataSvc = new DataSvc(); - dataSvc.Read(toolData); - } - catch (Exception) - { - // Remove from list - MultitoolsData.RemoveAt(i); - } - } - } - // Update multitool counter - multitoolsNumber = (uint)item.Value; - } - - private int ToolIsInMultitool(int toolId) - { - // Loop through multitools - //foreach (ShankModel multi in MultitoolsData) - //{ - // if (multi.ChildsTools.Count() > 0) - // { - // // Find tool id in multitool childs - // int childIndex = multi.ChildsTools.FindIndex(x => x.Id == toolId); - // if (childIndex >= 0) - // return multi.Id; - // } - //} - - return 0; - } - - private CmsError CheckMultitoolData(ref ShankModel shank, bool isUpdate) - { - //Check if the NC is Connected - CmsError cmsError = CheckConnection(); - if (cmsError.IsError()) - return cmsError; - - // Truncate name - if (shank.Name.Count() > 32) - shank.Name = shank.Name.Substring(0, 32); - - int totalNumberOfChilds = 0; - foreach (ShankModel multi in MultitoolsData) - { - if (shank.Name == multi.Name && shank.Id != multi.Id) - return INCORRECT_PARAMETERS_ERROR; - // Calculate max number of childs - totalNumberOfChilds += multi.MaxChilds; - } - - // Check if there are enough locations - if (!isUpdate && totalNumberOfChilds + MAX_TOOLS_PER_MULTITOOL > MAX_MULTITOOLS_LOCATIONS) - return MAX_MULTITOOL_LOCATION_REACHED_ERROR; - - return NO_ERROR; - } - - private ShankModel CreateMultitoolFromId(int multitoolId, string[] toolsStrings) - { - SiemensEdgesConfiguration toolEdgesConfig = new SiemensEdgesConfiguration(); - - // Find tool status status - var toolStatusByte = toolsStrings.FirstOrDefault(X => X.StartsWith("$TC_MTP8[" + multitoolId)); - if (toolStatusByte == null) - return null; - - int toolStatus = Convert.ToInt32(toolStatusByte.Split('=').LastOrDefault()); - - // Find shank, in order to get childs - ShankModel oldShank = MultitoolsData.Where(x => x.Id == multitoolId).FirstOrDefault(); - - // Create shank model - ShankModel shank = new ShankModel() - { - Id = multitoolId, - Name = toolsStrings.FirstOrDefault(x => x.Contains("$TC_MTP2[" + multitoolId)).Split('=').LastOrDefault().Replace("\"", ""), - LeftSize = Convert.ToByte(toolsStrings.FirstOrDefault(X => X.StartsWith("$TC_MTP3[" + multitoolId)).Split('=').LastOrDefault()), - RightSize = Convert.ToByte(toolsStrings.FirstOrDefault(X => X.StartsWith("$TC_MTP4[" + multitoolId)).Split('=').LastOrDefault()), - MagazinePositionType = Convert.ToByte(toolsStrings.FirstOrDefault(x => x.StartsWith("$TC_MTP7[" + multitoolId)).Split('=').LastOrDefault()), - MaxChilds = Convert.ToInt32(toolsStrings.FirstOrDefault(x => x.StartsWith("$TC_MTPN[" + multitoolId)).Split('=').LastOrDefault()), - IsEnabled = GetBitValue(toolStatus, 1), - IsInhibited = GetBitValue(toolStatus, 2), - InChangeTool = GetBitValue(toolStatus, 5), - InFixedPlace = GetBitValue(toolStatus, 6), - InUse = GetBitValue(toolStatus, 7), - ChildsTools = new List() - }; - - // Populate shank childs - for (int i = 1; i <= MAX_TOOLS_PER_MULTITOOL; i++) - { - // Find if position exists - var multitoolPos = toolsStrings.FirstOrDefault(x => x.Contains("$TC_MTPP6[" + multitoolId + "," + i)); - if (multitoolPos != null) - { - // If not empty - int toolId = Convert.ToInt32(multitoolPos.Split('=').LastOrDefault()); - if (toolId != 0) - { - shank.ChildsTools.Add(new ShankChildModel() - { - Id = toolId, - MultitoolId = i - }); - } - } - } - - // Check position in magazine - // Read magazine id from memory - DataSvc dataSvc = new DataSvc(); - Item item = new Item() { Path = string.Format("/Tool/MT/multitoolInMag[{0}]", multitoolId) }; - // Read magazine position - dataSvc.Read(item); - int magazineId = Convert.ToInt32(item.Value); - item = new Item() { Path = string.Format("/Tool/MT/multitoolInPlace[{0}]", multitoolId) }; - dataSvc.Read(item); - int positionId = Convert.ToInt32(item.Value); - - MoveToolInMagazineList(multitoolId, magazineId, positionId); - - return shank; - } - - #endregion Multitool Private region } internal static class MEMORY_ADDRESS { // aree Parametri + internal static MEMORY_CELL PARAMETER_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 600, 20, 8000); + internal static MEMORY_CELL PARAMETER_RT_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 601, 8, 3200); // aree Moduli + internal static MEMORY_CELL MODULE_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 602, 18, 2304); + internal static MEMORY_CELL MODULE_RT_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 603, 12, 1536); - // riscaldi superiori - - // riscaldi inferiori + // riscaldi + internal static MEMORY_CELL RISC_OCC_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 64, 64); + internal static MEMORY_CELL RISC_CHP_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 128, 1024); + internal static MEMORY_CELL RISC_CFI_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 2176, 1024); + internal static MEMORY_CELL RISC_ICH_MIN_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 3200, 4096); + internal static MEMORY_CELL RISC_OCS_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 9472, 64); + internal static MEMORY_CELL RISC_ESP_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 9728, 1024); + internal static MEMORY_CELL RISC_OVP_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 10752, 1024); + internal static MEMORY_CELL RISC_ICH_DATA = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 500, 11776, 4096); // produzione + internal static MEMORY_CELL PROCESS_STATUS = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, 605, 0, 2); // ciclo produzione + internal const int TABLE = 604; - internal const int TABLE = 258; internal static MEMORY_CELL NC_WATCHDOG = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, TABLE, 0, 1); internal static MEMORY_CELL ACTIVE_WATCHDOG = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, TABLE, 2, 1); @@ -6993,7 +5490,6 @@ namespace CMS_CORE_Library.S7Net internal static MEMORY_CELL AXIS_RESET_PROCEDURE = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, TABLE, 2608, 1); - internal static MEMORY_CELL PROCESS_STATUS = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, TABLE, 2582, 24); internal static MEMORY_CELL SELECTED_PROCESS = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, TABLE, 2582, 24); internal static MEMORY_CELL SELECT_PROCESS = new MEMORY_CELL(MEMORY_TYPE.Siemens_DB, TABLE, 2606, 1); diff --git a/CMS_CORE_Library/packages.config b/CMS_CORE_Library/packages.config new file mode 100644 index 0000000..ed7aa38 --- /dev/null +++ b/CMS_CORE_Library/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/SiemensS7/Siemens-S7-Test/TestMainForm.cs b/SiemensS7/Siemens-S7-Test/TestMainForm.cs index 6402e35..e4000d2 100644 --- a/SiemensS7/Siemens-S7-Test/TestMainForm.cs +++ b/SiemensS7/Siemens-S7-Test/TestMainForm.cs @@ -116,6 +116,7 @@ namespace Siemens_S7_Test // timer uiTimer.Interval = 20; } + /// /// Imposto parametri PLC ///