From 6275f1eca51bb3480f656ca2f19cafcf145c23b7 Mon Sep 17 00:00:00 2001 From: Lucio Maranta Date: Wed, 28 Nov 2018 16:12:24 +0000 Subject: [PATCH] Fanuc tool table improved --- CMS_CORE_Application/Form1.cs | 87 +++---- CMS_CORE_Library/Fanuc/Nc_Fanuc.cs | 346 +++++++++++++++++-------- CMS_CORE_Library/Models/NcToolModel.cs | 8 +- CMS_CORE_Library/Osai/Nc_Osai.cs | 2 +- CMS_CORE_Library/Utils/Nc_Utils.cs | 19 +- 5 files changed, 290 insertions(+), 172 deletions(-) diff --git a/CMS_CORE_Application/Form1.cs b/CMS_CORE_Application/Form1.cs index a285946..588ac11 100644 --- a/CMS_CORE_Application/Form1.cs +++ b/CMS_CORE_Application/Form1.cs @@ -73,81 +73,52 @@ namespace CMS_CORE_Application N = SetNcByType(); CmsError cmsError = N.NC_Connect(); - // cmsError = N.NC_RModelName(ref ModelName); - //cmsError = N.NC_WLanguage(new System.Globalization.CultureInfo("it")); + cmsError = N.NC_RModelName(ref ModelName); + cmsError = N.NC_WLanguage(new System.Globalization.CultureInfo("it")); bool error = false; string serial = ""; while (true) { - // cmsError = N.PROC_RMode(1, ref mode); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + /////////////////////////////////// Load basic data + cmsError = N.PROC_RMode(1, ref mode); + if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } - //cmsError = N.PROC_RStatus(1, ref status); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + cmsError = N.PROC_RStatus(1, ref status); + if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } - //cmsError = N.PROC_RActiveAlarms(1, ref procAlarms); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + cmsError = N.PROC_RActiveAlarms(1, ref procAlarms); + if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } - //cmsError = N.NC_RActiveAlarms(ref ncAlarms); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + cmsError = N.NC_RActiveAlarms(ref ncAlarms); + if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } - //cmsError = N.PLC_RActiveMessages(ref plcMsg); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + cmsError = N.PLC_RActiveMessages(ref plcMsg); + if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } - //cmsError = N.NC_RDateTime(ref NcTime); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + cmsError = N.NC_RDateTime(ref NcTime); + if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } - //cmsError = N.NC_RSerialNumber(ref serial); - //if (cmsError.IsError()) - //{ - // error = true; - // SetError(Lines, cmsError); - //} + cmsError = N.NC_RSerialNumber(ref serial); + if (cmsError.IsError()){ error = true; SetError(Lines, cmsError); } List axes = new List(); - - //ToolTableConfiguration conf = new ToolTableConfiguration(); - // N.TOOLS_RConfiguration(ref conf); - ////////////// SPEED TEST + + ////////////////////////////////////////// TEST Stopwatch st = new Stopwatch(); sw.Restart(); + + cmsError = N.FILES_WDeactivateProgram(1); - cmsError = N.PROC_RActiveAlarms(1, ref ncAlarms); - if (cmsError.IsError()) + List obj = new List() { - error = true; - SetError(Lines, cmsError); - } - ActiveProgramDataModel active = new ActiveProgramDataModel(); - cmsError = N.FILES_RActiveProgramData(2, ref active); - N.FILES_WDeactivateProgram(1); - InfoFile file = new InfoFile(); - cmsError = N.FILES_RGetFileInfo(active.Path, ref file); + new NcToolModel() { ToolId = 22, FamilyId = 1, OffsetId1 = 1, OffsetId2 = 2, OffsetId3 = 3, OffsetLength = 4, ResidualLife = 5, ResidualRevive = 6, ShankId = 7 }, + new NcToolModel() { ToolId = 23, FamilyId = 4, OffsetId1 = 1, OffsetId2 = 2, OffsetId3 = 3, OffsetLength = 4, ResidualLife = 8, ResidualRevive = 8, ShankId = 7 } + }; + N.TOOLS_WUpdateTools(obj); + ActiveProgramDataModel active = new ActiveProgramDataModel(); + cmsError = N.FILES_RActiveProgramData(1, ref active); + Console.WriteLine("ICON" + sw.ElapsedMilliseconds); sw.Stop(); diff --git a/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs b/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs index 80400b0..eb8fc07 100644 --- a/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs +++ b/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs @@ -42,6 +42,8 @@ namespace CMS_CORE_Library.Fanuc private const string PLC_MESSAGE_PATH = @"C:\CMS\FANUC\"; + private const string BLANK_PART_PROGRAM_PATH = "//CNC_MEM/MTB1/BLANK"; + // Const private const int TOOL_OFFSET = 300; @@ -198,7 +200,7 @@ namespace CMS_CORE_Library.Fanuc #region High level methods - //Get the NC Language + // Get the NC Language public override CmsError NC_RLanguage(ref CultureInfo Language) { // Read static data @@ -211,7 +213,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Set the NC Language + // Set the NC Language public override CmsError NC_WLanguage(CultureInfo Language) { // set but 7 to TRUE @@ -226,7 +228,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the NC Serial Number + // Get the NC Serial Number public override CmsError NC_RSerialNumber(ref string SN) { // Read static data @@ -239,7 +241,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the NC Software Version + // Get the NC Software Version public override CmsError NC_RSoftwareVersion(ref string SWV) { // Read static data @@ -252,7 +254,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the NC model Name + // Get the NC model Name public override CmsError NC_RModelName(ref string ModelName) { // Read static data @@ -265,7 +267,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the processes-count configurated + // Get the processes-count configurated public override CmsError NC_RProcessesNum(ref ushort ProcNumber) { //Read static data @@ -278,7 +280,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the NC Time + // Get the NC Time public override CmsError NC_RDateTime(ref DateTime ActualTime) { //Check if the NC is Connected @@ -314,7 +316,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the Machine Number + // Get the Machine Number public override CmsError NC_RMachineNumber(ref string MachNumber) { ushort Value = 0; @@ -329,7 +331,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Get the NC Alarms + // Get the NC Alarms public override CmsError NC_RActiveAlarms(ref List alarms) { //// Check if the NC is Connected @@ -409,7 +411,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - //Set the Nc Active Page of the NC + // Set the Nc Active Page of the NC public override CmsError NC_SetScreenVisible(SCREEN_PAGE screen) { short actualPage, nReturn; @@ -2261,13 +2263,17 @@ namespace CMS_CORE_Library.Fanuc try { - char[] filePath = new char[242]; + char[] filePath = new char[240]; // Read main program data short nReturn = Focas1.cnc_pdf_rdmain(nLibHandle[processId - 1], filePath); if (nReturn != 0) return GetNcError(nReturn); + // Clean the read path string path = new string(filePath).TrimEnd('\0'); + // If program is CMS blank program, simulates that there are no progs + if (path == BLANK_PART_PROGRAM_PATH) + return NO_ERROR; ushort length = 200; char[] text = new char[201]; @@ -2292,7 +2298,41 @@ namespace CMS_CORE_Library.Fanuc public override CmsError FILES_WDeactivateProgram(int processId) { - return FUNCTION_NOT_ALLOWED_ERROR; + // Check if the NC is connected + CmsError cmsError = CheckConnection(); + if (cmsError.IsError()) + return cmsError; + + try + { + // Select main data + short nReturn = Focas1.cnc_pdf_slctmain(nLibHandle[processId - 1], BLANK_PART_PROGRAM_PATH); + if (nReturn != 0) + { + // Check error type + Focas1.ODBERR errorDetails = new Focas1.ODBERR(); + nReturn = Focas1.cnc_getdtailerr(nLibHandle[0], errorDetails); + + // If file not exist then create it + if (errorDetails.err_no == 2) + { + // Create File + cmsError = WritePartProgramContent("//CNC_MEM/MTB1/", "%\n\n%"); + if (cmsError.IsError()) + return cmsError; + // Deactivate again + return FILES_WDeactivateProgram(processId); + } + else + return GetNcError(nReturn); + } + } + catch (Exception ex) + { + return ManageException(ex); + } + + return NO_ERROR; } public override CmsError FILES_UploadPartProgram(string localPath, string name, ref string newFilePath) @@ -2762,7 +2802,7 @@ namespace CMS_CORE_Library.Fanuc public override CmsError TOOLS_WStartEditData() { - //Check if the NC is Connected + // Check if the NC is Connected CmsError cmsError = CheckConnection(); if (cmsError.IsError()) return cmsError; @@ -2821,93 +2861,135 @@ namespace CMS_CORE_Library.Fanuc public override CmsError TOOLS_WUpdateTools(List toolsList) { - Stopwatch st = new Stopwatch(); - st.Restart(); - - List zeros = Enumerable.Repeat(0, TOOL_TABLE_INDEX.Size / 4).ToList(); - CmsError cmsError = MEM_RWIntegerList(W, 0, TOOL_TABLE_INDEX.MemType, TOOL_TABLE_INDEX.Address, TOOL_TABLE_INDEX.Size / 4, ref zeros); + // Check if the NC is Connected + CmsError cmsError = CheckConnection(); if (cmsError.IsError()) return cmsError; - Console.WriteLine("Tempo: " + st.ElapsedMilliseconds); - foreach (NcToolModel tool in toolsList) - { - // Get the offset of the id in the memory - cmsError = TOOLS_RPositionId(tool.ToolId, TOOL_TABLE_INDEX.Address, out int offset); - if (cmsError.IsError()) - return cmsError; + // Create new values of the tools table + List newMemoryValues = TOOLS_CreateNewToolTableMemoryData(toolsList, TOOL_TABLE_INDEX); + // Write in memory + return MEM_RWIntegerList(W, 0, TOOL_TABLE_INDEX.MemType, TOOL_TABLE_INDEX.Address, newMemoryValues.Count(), ref newMemoryValues); + + //List zeros = Enumerable.Repeat(0, TOOL_TABLE_INDEX.Size / 4).ToList(); + //CmsError cmsError = MEM_RWIntegerList(W, 0, TOOL_TABLE_INDEX.MemType, TOOL_TABLE_INDEX.Address, TOOL_TABLE_INDEX.Size / 4, ref zeros); + //if (cmsError.IsError()) + // return cmsError; - // Write object in memory - cmsError = TOOLS_WObjectInMemory(tool, TOOL_TABLE_INDEX.Address, offset); - if (cmsError.IsError()) - return cmsError; - } + //foreach (NcToolModel tool in toolsList) + //{ + // // Get the offset of the id in the memory + // cmsError = TOOLS_RPositionOfNextShortId(tool.ToolId, TOOL_TABLE_INDEX.Address, out int offset); + // if (cmsError.IsError()) + // return cmsError; - return NO_ERROR; + // // Write object in memory + // cmsError = TOOLS_WObjectInMemory(tool, TOOL_TABLE_INDEX.Address, offset); + // if (cmsError.IsError()) + // return cmsError; + //} + + //return NO_ERROR; } public override CmsError TOOLS_WUpdateFamilies(List familyList) { - List zeros = Enumerable.Repeat(0, FAMILY_TABLE_INDEX.Size / 4).ToList(); - CmsError cmsError = MEM_RWIntegerList(W, 0, FAMILY_TABLE_INDEX.MemType, FAMILY_TABLE_INDEX.Address, FAMILY_TABLE_INDEX.Size / 4, ref zeros); + // Check if the NC is Connected + CmsError cmsError = CheckConnection(); if (cmsError.IsError()) return cmsError; - foreach (NcFamilyModel fam in familyList) - { - // Get the offset of the id in the memory - cmsError = TOOLS_RPositionId(fam.FamilyId, FAMILY_TABLE_INDEX.Address, out int offset); - if (cmsError.IsError()) - return cmsError; + // Create new values of the family table + List newMemoryValues = TOOLS_CreateNewToolTableMemoryData(familyList, FAMILY_TABLE_INDEX); - // Write object in memory - cmsError = TOOLS_WObjectInMemory(fam, FAMILY_TABLE_INDEX.Address, offset); - if (cmsError.IsError()) - return cmsError; - } + return MEM_RWIntegerList(W, 0, FAMILY_TABLE_INDEX.MemType, FAMILY_TABLE_INDEX.Address, newMemoryValues.Count(), ref newMemoryValues); - return NO_ERROR; + //List zeros = Enumerable.Repeat(0, FAMILY_TABLE_INDEX.Size / 4).ToList(); + //CmsError cmsError = MEM_RWIntegerList(W, 0, FAMILY_TABLE_INDEX.MemType, FAMILY_TABLE_INDEX.Address, FAMILY_TABLE_INDEX.Size / 4, ref zeros); + //if (cmsError.IsError()) + // return cmsError; + + //foreach (NcFamilyModel fam in familyList) + //{ + // // Get the offset of the id in the memory + // cmsError = TOOLS_RPositionOfNextShortId(fam.FamilyId, FAMILY_TABLE_INDEX.Address, out int offset); + // if (cmsError.IsError()) + // return cmsError; + + // // Write object in memory + // cmsError = TOOLS_WObjectInMemory(fam, FAMILY_TABLE_INDEX.Address, offset); + // if (cmsError.IsError()) + // return cmsError; + //} + + //return NO_ERROR; } public override CmsError TOOLS_WUpdateShanks(List shankList) { - List zeros = Enumerable.Repeat(0, SHANK_TABLE_INDEX.Size / 4).ToList(); - CmsError cmsError = MEM_RWIntegerList(W, 0, SHANK_TABLE_INDEX.MemType, SHANK_TABLE_INDEX.Address, SHANK_TABLE_INDEX.Size / 4, ref zeros); + // Check if the NC is Connected + CmsError cmsError = CheckConnection(); if (cmsError.IsError()) return cmsError; - foreach (NcShankModel shank in shankList) - { - // Get the offset of the id in the memory - cmsError = TOOLS_RPositionId(shank.ShankId, SHANK_TABLE_INDEX.Address, out int offset); - if (cmsError.IsError()) - return cmsError; + // Create new values of the shank table + List newMemoryValues = TOOLS_CreateNewToolTableMemoryData(shankList, SHANK_TABLE_INDEX); - // Write object in memory - cmsError = TOOLS_WObjectInMemory(shank, SHANK_TABLE_INDEX.Address, offset); - if (cmsError.IsError()) - return cmsError; - } + // Write in memory + return MEM_RWIntegerList(W, 0, SHANK_TABLE_INDEX.MemType, SHANK_TABLE_INDEX.Address, newMemoryValues.Count(), ref newMemoryValues); - return NO_ERROR; + //List zeros = Enumerable.Repeat(0, SHANK_TABLE_INDEX.Size / 4).ToList(); + //CmsError cmsError = MEM_RWIntegerList(W, 0, SHANK_TABLE_INDEX.MemType, SHANK_TABLE_INDEX.Address, SHANK_TABLE_INDEX.Size / 4, ref zeros); + //if (cmsError.IsError()) + // return cmsError; + + //foreach (NcShankModel shank in shankList) + //{ + // // Get the offset of the id in the memory + // cmsError = TOOLS_RPositionOfNextShortId(shank.ShankId, SHANK_TABLE_INDEX.Address, out int offset); + // if (cmsError.IsError()) + // return cmsError; + + // // Write object in memory + // cmsError = TOOLS_WObjectInMemory(shank, SHANK_TABLE_INDEX.Address, offset); + // if (cmsError.IsError()) + // return cmsError; + //} + + // return NO_ERROR; } public override CmsError TOOLS_WUpdateMagazinePositions(List magPosList) { - foreach (NcMagazinePositionModel pos in magPosList) - { - // Get the offset of the id in the memory - CmsError cmsError = TOOLS_RPositionId(pos.PositionId, SHANK_TABLE_INDEX.Address, out int offset); - if (cmsError.IsError()) - return cmsError; + // Check if the NC is Connected + CmsError cmsError = CheckConnection(); + if (cmsError.IsError()) - // Write object in memory - cmsError = TOOLS_WObjectInMemory(pos, SHANK_TABLE_INDEX.Address, offset); - if (cmsError.IsError()) - return cmsError; - } + return cmsError; + // Create new values of the magazine positions table + List newMemoryValues = TOOLS_CreateNewToolTableMemoryData(magPosList, MAG_POS_TABLE_INDEX); + // Write in memory + return MEM_RWIntegerList(W, 0, MAG_POS_TABLE_INDEX.MemType, MAG_POS_TABLE_INDEX.Address, newMemoryValues.Count(), ref newMemoryValues); + + //List zeros = Enumerable.Repeat(0, SHANK_TABLE_INDEX.Size / 4).ToList(); + //CmsError cmsError = MEM_RWIntegerList(W, 0, MAG_POS_TABLE_INDEX.MemType, MAG_POS_TABLE_INDEX.Address, MAG_POS_TABLE_INDEX.Size / 4, ref zeros); + //if (cmsError.IsError()) + // return cmsError; + + //foreach (NcMagazinePositionModel pos in magPosList) + //{ + // // Get the offset of the id in the memory + // cmsError = TOOLS_RPositionOfNextByteId(pos.PositionId, MAG_POS_TABLE_INDEX.Address, out int offset); + // if (cmsError.IsError()) + // return cmsError; - return NO_ERROR; + // // Write object in memory + // cmsError = TOOLS_WObjectInMemory(pos, MAG_POS_TABLE_INDEX.Address, offset); + // if (cmsError.IsError()) + // return cmsError; + //} + + //return NO_ERROR; } public override CmsError TOOLS_WStartEditTooling(int magazineId) @@ -3005,7 +3087,7 @@ namespace CMS_CORE_Library.Fanuc return NO_ERROR; } - private CmsError TOOLS_RPositionId(int id, int startIndex, out int offset) + private CmsError TOOLS_RPositionOfNextShortId(int id, int startIndex, out int offset) { offset = 0; @@ -3015,25 +3097,47 @@ namespace CMS_CORE_Library.Fanuc if (cmsError.IsError()) return cmsError; - ushort[] bytes = new ushort[300]; + ushort[] shorts = new ushort[300]; // Copy into ushorts - Buffer.BlockCopy(ints.ToArray(), 0, bytes, 0, ints.Count * sizeof(ushort)); + Buffer.BlockCopy(ints.ToArray(), 0, shorts, 0, ints.Count * sizeof(ushort)); // Find id offset = Array - .FindIndex(bytes, x => x == id); + .FindIndex(shorts, x => x == id); // if not found, get last number if (offset == -1) { offset = 0; - for (int i = 0; i < bytes.Count(); i++) - if (bytes[i] != 0) + for (int i = 0; i < shorts.Count(); i++) + if (shorts[i] != 0) offset = i + 1; } return NO_ERROR; } + private CmsError TOOLS_RPositionOfNextByteId(int id, int startIndex, out int offset) + { + offset = 0; + + // Read 300 bytes + List ints = new List(); + CmsError cmsError = MEM_RWIntegerList(false, 0, Nc.MEMORY_TYPE.Fanuc_D, startIndex, 75, ref ints); + if (cmsError.IsError()) + return cmsError; + + byte[] bytes = new byte[300]; + // Copy into ushorts + Buffer.BlockCopy(ints.ToArray(), 0, bytes, 0, ints.Count * sizeof(ushort)); + + offset = 0; + for (int i = 0; i < bytes.Count(); i++) + if (bytes[i] != 0) + offset = i + 1; + + return NO_ERROR; + } + private CmsError TOOLS_WObjectInMemory(T item, int startTableIndex, int objOffset) where T : class { PropertyInfo[] properties = typeof(T).GetProperties(); @@ -3090,12 +3194,13 @@ namespace CMS_CORE_Library.Fanuc return cmsError; } - private CmsError TOOLS_WObjectsInMemory(List items, int startTableIndex) where T : class + private List TOOLS_CreateNewToolTableMemoryData(List items, MEMORY_CELL tableInfo) where T : class { PropertyInfo[] properties = typeof(T).GetProperties(); - - CmsError cmsError = NO_ERROR; - int nextFieldIndex = startTableIndex; + byte[] values = new byte[tableInfo.Size]; + + // Start from the begin of the table + int nextFieldIndex = 0; // Index of the int objOffset = 0; @@ -3109,36 +3214,51 @@ namespace CMS_CORE_Library.Fanuc // Add obj index to the parameter memory int currentFieldIndex = nextFieldIndex + objOffset * size; - // Write based on the size + // Choose action based on the size switch (size) { case 1: { byte b = Convert.ToByte(property.GetValue(item)); - cmsError = MEM_RWByte(W, 0, MEMORY_TYPE.Fanuc_R, currentFieldIndex, 0, ref b); - if (cmsError.IsError()) - return cmsError; + values[currentFieldIndex] = b; + + // cmsError = MEM_RWByte(W, 0, MEMORY_TYPE.Fanuc_R, currentFieldIndex, 0, ref b); + //if (cmsError.IsError()) + // return cmsError; } break; case 2: { ushort u = Convert.ToUInt16(property.GetValue(item)); - cmsError = MEM_RWWord(W, 0, MEMORY_TYPE.Fanuc_R, currentFieldIndex, ref u); - if (cmsError.IsError()) - return cmsError; + // Split ushort into 2 bytes + byte[] bytesVal = NumberToByte(u); + // Overwrite bytes with new values + values[currentFieldIndex] = bytesVal[0]; + values[currentFieldIndex + 1] = bytesVal[1]; + + // cmsError = MEM_RWWord(W, 0, MEMORY_TYPE.Fanuc_R, currentFieldIndex, ref u); + //if (cmsError.IsError()) + // return cmsError; } break; case 4: { - uint w = Convert.ToUInt32(property.GetValue(item)); - cmsError = MEM_RWDWord(W, 0, MEMORY_TYPE.Fanuc_R, currentFieldIndex, ref w); - if (cmsError.IsError()) - return cmsError; + uint i = Convert.ToUInt32(property.GetValue(item)); + // Split uint into 4 bytes + byte[] bytesVal = NumberToByte(i); + // Overwrite bytes + values[currentFieldIndex] = bytesVal[0]; + values[currentFieldIndex + 1] = bytesVal[1]; + values[currentFieldIndex + 2] = bytesVal[2]; + values[currentFieldIndex + 3] = bytesVal[3]; + + //cmsError = MEM_RWDWord(W, 0, MEMORY_TYPE.Fanuc_R, currentFieldIndex, ref w); + //if (cmsError.IsError()) + // return cmsError; } break; - default: break; } @@ -3149,9 +3269,12 @@ namespace CMS_CORE_Library.Fanuc // Increment offset objOffset++; + + // Reset counters + nextFieldIndex = 0; } - return cmsError; + return ListOfByteToListOf(values, BitConverter.ToInt32); } private string GenerateCsvStringFromModel(List items) where T : class @@ -3613,23 +3736,20 @@ namespace CMS_CORE_Library.Fanuc // Start write nReturn = Focas1.cnc_dwnstart4(nLibHandle[0], 0, partProgramPath); + if (nReturn != 0) + return GetNcError(nReturn); - //Throw Exception if there's an error - ErrorHandler(nReturn); - - //int lenghtMax = fileData.Length; - Thread.Sleep(1000); + // int lenghtMax = fileData.Length; + Thread.Sleep(500); int lenght = partProgramContent.Length; // Write program nReturn = Focas1.cnc_download4(nLibHandle[0], ref lenght, partProgramContent); - - //Throw Exception if there's an error - ErrorHandler(nReturn); + if (nReturn != 0) + return GetNcError(nReturn); nReturn = Focas1.cnc_dwnend4(nLibHandle[0]); - - //Throw Exception if there's an error - ErrorHandler(nReturn); + if (nReturn != 0) + return GetNcError(nReturn); return NO_ERROR; } @@ -3649,8 +3769,9 @@ namespace CMS_CORE_Library.Fanuc // Start read process nReturn = Focas1.cnc_upstart4(nLibHandle[0], 0, partProgramPath); - //Throw Exception if there's an error - ErrorHandler(nReturn); + // Return if there's an error + if (nReturn != 0) + return GetNcError(nReturn); do { @@ -3685,12 +3806,18 @@ namespace CMS_CORE_Library.Fanuc return GetNcError(nReturn); } } + + if (lenght <= 256) + fileEnded = true; + } while ((nReturn == (short)Focas1.focas_ret.EW_OK || nReturn == (short)Focas1.focas_ret.EW_BUFFER) && !fileEnded); // End read nReturn = Focas1.cnc_upend4(nLibHandle[0]); - ErrorHandler(nReturn); + // Return if there's an error + if (nReturn != 0) + return GetNcError(nReturn); partProgramContent = partProgramContent.Replace("\0", string.Empty); @@ -3823,10 +3950,9 @@ namespace CMS_CORE_Library.Fanuc // Tool table indexes internal static MEMORY_CELL TOOL_TABLE_INDEX = new MEMORY_CELL(MEMORY_TYPE.Fanuc_D, 35000, 6900); - internal static MEMORY_CELL FAMILY_TABLE_INDEX = new MEMORY_CELL(MEMORY_TYPE.Fanuc_D, 42000, 7500); internal static MEMORY_CELL SHANK_TABLE_INDEX = new MEMORY_CELL(MEMORY_TYPE.Fanuc_D, 49600, 2100); - internal static MEMORY_CELL MAG_POS_TABLE_INDEX = new MEMORY_CELL(MEMORY_TYPE.Fanuc_D, 51800, 900); + internal static MEMORY_CELL MAG_POS_TABLE_INDEX = new MEMORY_CELL(MEMORY_TYPE.Fanuc_D, 51800, 1200); internal static MEMORY_CELL COUNTERS_DATA = new MEMORY_CELL(MEMORY_TYPE.Fanuc_R, 0, 64); internal static MEMORY_CELL COUNTERS_ACK = new MEMORY_CELL(MEMORY_TYPE.Fanuc_R, 0, 2); diff --git a/CMS_CORE_Library/Models/NcToolModel.cs b/CMS_CORE_Library/Models/NcToolModel.cs index 8e26be6..b31cebb 100644 --- a/CMS_CORE_Library/Models/NcToolModel.cs +++ b/CMS_CORE_Library/Models/NcToolModel.cs @@ -36,9 +36,13 @@ namespace CMS_CORE_Library.Models public byte MagazineId { get; set; } - public byte PositionId { get; set; } + public ushort PositionId { get; set; } public byte MagazinePositionType { get; set; } + + public byte OriginMagazineId { get; set; } + + public ushort OriginPositionId { get; set; } } public class NcToolModel @@ -168,7 +172,7 @@ namespace CMS_CORE_Library.Models { public byte MagazineId { get; set; } - public byte PositionId { get; set; } + public ushort PositionId { get; set; } public byte Type { get; set; } diff --git a/CMS_CORE_Library/Osai/Nc_Osai.cs b/CMS_CORE_Library/Osai/Nc_Osai.cs index 47d2b3b..5e12ab0 100644 --- a/CMS_CORE_Library/Osai/Nc_Osai.cs +++ b/CMS_CORE_Library/Osai/Nc_Osai.cs @@ -3777,7 +3777,7 @@ namespace CMS_CORE_Library.Osai internal const int STARTING_ADDRESS = 12000; internal static MEMORY_CELL NC_WATCHDOG = new MEMORY_CELL(MEMORY_TYPE.Osai_MW, STARTING_ADDRESS, 1); - internal static MEMORY_CELL ACTIVE_WATCHDOG = new MEMORY_CELL(MEMORY_TYPE.Osai_MW, STARTING_ADDRESS + 2, 1); + internal static MEMORY_CELL ACTIVE_WATCHDOG = new MEMORY_CELL(MEMORY_TYPE.Osai_MW, STARTING_ADDRESS + 1, 1); internal static MEMORY_CELL MATR_MACCH_OSAI = new MEMORY_CELL(MEMORY_TYPE.Osai_MW, 3403, 0, 1); internal static MEMORY_CELL FUNCTION_ACCESS = new MEMORY_CELL(MEMORY_TYPE.Osai_MW, 12003, 8); diff --git a/CMS_CORE_Library/Utils/Nc_Utils.cs b/CMS_CORE_Library/Utils/Nc_Utils.cs index 80590be..ba4dd1f 100644 --- a/CMS_CORE_Library/Utils/Nc_Utils.cs +++ b/CMS_CORE_Library/Utils/Nc_Utils.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using static CMS_CORE_Library.Models.DataStructures; namespace CMS_CORE_Library.Utils @@ -148,7 +149,7 @@ namespace CMS_CORE_Library.Utils return fileContent; } - // Get a substring from the beginning until the specified character + // Get a substring from the beginning until the specified character -> defautl "[" public static string GetUntilOrEmpty(string text, string stopAt = "[") { // Get string unti @@ -178,6 +179,7 @@ namespace CMS_CORE_Library.Utils .ToArray(); } + // Convert a byte into an array of bools public static bool[] ByteToBits(byte val) { return new BitArray(new byte[] { val }) @@ -185,6 +187,21 @@ namespace CMS_CORE_Library.Utils .ToArray(); } + // Convert a byte into an array of bools + public static byte[] NumberToByte(object val) + { + return BitConverter.GetBytes(Convert.ToInt32(val)); + } + + public static List ListOfByteToListOf(byte[] array, Func bitConverter) + { + var size = Marshal.SizeOf(typeof(T)); + return Enumerable.Range(0, array.Length / size) + .Select(i => bitConverter(array, i * size)) + .ToList(); + } + + public static SIEMENS_MAGAZINE_TYPE ConvertToSiemensMagazineType(NC_MAGAZINE_TYPE type) { switch (type)