diff --git a/CMS_CORE_Application/Form1.cs b/CMS_CORE_Application/Form1.cs index fdf950b..6b8648c 100644 --- a/CMS_CORE_Application/Form1.cs +++ b/CMS_CORE_Application/Form1.cs @@ -221,7 +221,8 @@ namespace CMS_CORE_Application { TXTip.Enabled = true; TXTport.Enabled = true; - TXTip.Text = "192.168.139.1"; + //TXTip.Text = "192.168.139.1"; + TXTip.Text = "10.69.48.24"; //TXTip.Text = "10.69.36.33"; TXTport.Text = "8193"; } diff --git a/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs b/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs index 287f885..21a0840 100644 --- a/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs +++ b/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs @@ -1148,7 +1148,19 @@ namespace CMS_CORE_Library.Fanuc public override CmsError PLC_WScadaValue(string memIndex, SCADA_MEM_TYPE memType, object value) { - string[] index = memIndex.Split('.'); + string[] index = memIndex.Split('_'); + MEMORY_TYPE fanucMemtype = MEMORY_TYPE.Fanuc_R; + + if (index.Count() != 2) + return INCORRECT_PARAMETERS_ERROR; + + switch (index[0].ToUpper()) + { + case "R": fanucMemtype = MEMORY_TYPE.Fanuc_R; break; + case "D": fanucMemtype = MEMORY_TYPE.Fanuc_D; break; + }; + + index = index[1].Split('.'); if (index.Count() == 0) { @@ -1158,27 +1170,27 @@ namespace CMS_CORE_Library.Fanuc { if (memType == SCADA_MEM_TYPE.INT) { - // READ INT - int val = 0; - CmsError cmsError = MEM_RWInteger(W, 0, MEMORY_TYPE.Demo, Convert.ToInt32(index[0]), ref val); + // WRITE INT + int val = Convert.ToInt32(value); + CmsError cmsError = MEM_RWInteger(W, 0, fanucMemtype, Convert.ToInt32(index[0]), ref val); if (cmsError.IsError()) return cmsError; value = val; } else if (memType == SCADA_MEM_TYPE.WORD) - { // READ WORD - ushort val = 0; - CmsError cmsError = MEM_RWWord(W, 0, MEMORY_TYPE.Demo, Convert.ToInt32(index[0]), ref val); + { // WRITE WORD + ushort val = Convert.ToUInt16(value); + CmsError cmsError = MEM_RWWord(W, 0, fanucMemtype, Convert.ToInt32(index[0]), ref val); if (cmsError.IsError()) return cmsError; value = val; } else - { // READ byte - byte val = 0; - CmsError cmsError = MEM_RWByte(W, 0, MEMORY_TYPE.Demo, Convert.ToInt32(index[0]), 0, ref val); + { // WRITE byte + byte val = Convert.ToByte(value); + CmsError cmsError = MEM_RWByte(W, 0, fanucMemtype, Convert.ToInt32(index[0]), 0, ref val); if (cmsError.IsError()) return cmsError; @@ -1186,9 +1198,9 @@ namespace CMS_CORE_Library.Fanuc } } else if (index.Count() == 2 && memType == SCADA_MEM_TYPE.BOOL) - { // READ BOOL - bool val = false; - CmsError cmsError = MEM_RWBoolean(W, 0, MEMORY_TYPE.Demo, Convert.ToInt32(index[0]), Convert.ToInt32(index[1]), ref val); + { // WRITE BOOL + bool val = Convert.ToBoolean(value); + CmsError cmsError = MEM_RWBoolean(W, 0, fanucMemtype, Convert.ToInt32(index[0]), Convert.ToInt32(index[1]), ref val); if (cmsError.IsError()) return cmsError; @@ -3753,8 +3765,8 @@ namespace CMS_CORE_Library.Fanuc List byteValues = IntsToBytes(ints); // Index of the - int objOffset = 0; - + int objIndex = 0; + int propertyStartIndex = 0; int propertyIndex = 0; // Start from the begin of the table for (int memoryIndex = 0; memoryIndex < byteValues.Count();) @@ -3767,28 +3779,63 @@ namespace CMS_CORE_Library.Fanuc { case 1: { - properties[propertyIndex].SetValue(models[objOffset], byteValues[memoryIndex]); + properties[propertyIndex].SetValue(models[objIndex], byteValues[memoryIndex]); + memoryIndex += 1; } break; case 2: { short u = BitConverter.ToInt16(new byte[2] { byteValues[memoryIndex], byteValues[memoryIndex + 1] }, 0); - // If is in the ID's memory range - if (memoryIndex < TOOL_OFFSET) - // Create TMP item with generic type declaration - models.Add((T)Activator.CreateInstance(typeof(T))); - properties[propertyIndex].SetValue(models[objOffset], u); + bool skipAssignment = false; + // If indexes are pointing to the ID range + if (propertyIndex == 0) + { + // Check if id = 0, if not create new object + if(byteValues[memoryIndex] != 0) + { + // Create TMP item with generic type declaration + models.Add((T)Activator.CreateInstance(typeof(T))); + // Update object index + objIndex = models.Count() - 1; + } + else + { + // If i've found the first id = 0, stop creating items and skip to the next property + // I've read all the ids, skip to the next property + memoryIndex = propertyStartIndex = TOOL_OFFSET * size; + propertyIndex = 1; - memoryIndex += 2; + // Restart object + objIndex = -1; + // Skip assignment + skipAssignment = true; + } + } + + if (!skipAssignment) + { + // Check if property is unsigned or not + if (properties[propertyIndex].GetType() == typeof(ushort)) + properties[propertyIndex].SetValue(models[objIndex], (ushort)u); + else + properties[propertyIndex].SetValue(models[objIndex], u); + + memoryIndex += 2; + } } break; case 4: { int i = BitConverter.ToInt32(new byte[4] { byteValues[memoryIndex], byteValues[memoryIndex + 1], byteValues[memoryIndex + 2], byteValues[memoryIndex + 3], }, 0); - properties[propertyIndex].SetValue(models[objOffset], i); + + // Check if property is unsigned or not + if (properties[propertyIndex].GetType() == typeof(uint)) + properties[propertyIndex].SetValue(models[objIndex], (uint)i); + else + properties[propertyIndex].SetValue(models[objIndex], i); memoryIndex += 4; } @@ -3799,18 +3846,26 @@ namespace CMS_CORE_Library.Fanuc } // Check if there is another object - if (objOffset + 1 >= models.Count()) + if (objIndex + 1 >= models.Count()) { - objOffset = 0; + objIndex = 0; // check if there is another property if (propertyIndex + 1 >= properties.Count()) // if not, i wrote all the object's properties so exit from loop - propertyIndex = byteValues.Count(); + memoryIndex = byteValues.Count(); else - propertyIndex += 1; + { + // Wait until memoryIndex is in ID range + if (propertyIndex != 0) + { + propertyStartIndex += size * TOOL_OFFSET; + memoryIndex = propertyStartIndex; + propertyIndex += 1; + } + } } else - objOffset++; + objIndex++; } }