public static void UpdateOperatinHistoryMacroLocation(string ipAddress, ushort portNo, short macroLocation, int value) { short ret = -20; ushort focasLibHandle = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return; } FocasData.WriteMacro(focasLibHandle, macroLocation, value); FocasLib.cnc_freelibhndl(focasLibHandle); }
public static short ReadRunningProgramNumber(string ipAddress, ushort portNo, out short mainProgram) { ushort focasLibHandle = 0; short ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(mainProgram = 0); } FocasLibBase.ODBPRO odbpro = new FocasLibBase.ODBPRO(); FocasLib.cnc_rdprgnum(focasLibHandle, odbpro); mainProgram = odbpro.mdata; FocasLib.cnc_freelibhndl(focasLibHandle); return(odbpro.data); }
public static int ReadOperatinHistoryDPrintLocation(string ipAddress, ushort portNo, short macroLocation) { short ret = -20; ushort focasLibHandle = 0; int dprintValue = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(dprintValue); } dprintValue = FocasData.ReadMacro(focasLibHandle, macroLocation); FocasLib.cnc_freelibhndl(focasLibHandle); return(dprintValue); }
//to delete EW_PASSWD public static bool DeletePrograms(string ipAddress, ushort portNo, short programNumber) { bool isdeleted = false; focas_ret ret; int ret1 = -1; ushort focasLibHandle = 0; ret1 = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret1 != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret1); return(false); } short retShort = FocasLib.cnc_delete(focasLibHandle, programNumber); Enum.TryParse <focas_ret>(retShort.ToString(), out ret); switch (ret) { case focas_ret.EW_OK: Logger.WriteDebugLog("PROGRAM" + programNumber + " has been deleted."); isdeleted = true; break; case focas_ret.EW_DATA: Logger.WriteDebugLog("PROGRAM " + programNumber + " doesn't exist."); break; case focas_ret.EW_PROT: Logger.WriteDebugLog("Program " + programNumber + " is PROTECTED."); break; case focas_ret.EW_BUSY: Logger.WriteDebugLog("CNC is busy. Program " + programNumber + " delete REJECTED."); break; case focas_ret.EW_PASSWD: Logger.WriteDebugLog("Specified program number cannot be deleted because the data is protected."); break; } return(isdeleted); }
public static DataTable ReadOperationHistory(string machineId, string ipAddress, ushort portNo) { int ret = 0; ushort focasLibHandle = 0; DataTable operationHistoryTable = Utility.get_OperationTable(); try { ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret == 0) { ret = FocasLib.cnc_stopophis(focasLibHandle); ushort totalOperationHistoryRecords = 0; ret = FocasLib.cnc_rdophisno(focasLibHandle, out totalOperationHistoryRecords); if (totalOperationHistoryRecords == 0) { return(operationHistoryTable); } //Call C++ Dll to read the operation history details //read one records at a time OR read a rage of records at a time OR Read all records at a time???? //C++ will write all the records to a file //C# will process the file content and insert to database. } } catch (Exception ex) { //TODO } finally { if (focasLibHandle > 0) { ret = FocasLib.cnc_startophis(focasLibHandle); } } return(operationHistoryTable); }
//to search /* * The behavior of this function depends on the CNC mode. * EDIT, MEM mode : foreground search * Other mode : background search */ public static bool SearchPrograms(string ipAddress, ushort portNo, short programNumber) { bool isFound = false; focas_ret ret; int ret1 = -1; ushort focasLibHandle = 0; ret1 = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret1 != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret1); return(false); } short retShort = FocasLib.cnc_search(focasLibHandle, programNumber); Enum.TryParse <focas_ret>(retShort.ToString(), out ret); switch (ret) { case focas_ret.EW_OK: Logger.WriteDebugLog("PROGRAM " + programNumber + " has been searched."); isFound = true; break; case focas_ret.EW_DATA: Logger.WriteDebugLog("PROGRAM " + programNumber + " doesn't exist."); break; case focas_ret.EW_PROT: Logger.WriteDebugLog("Program " + programNumber + " is PROTECTED."); break; case focas_ret.EW_BUSY: Logger.WriteDebugLog("CNC is busy. Program " + programNumber + " search REJECTED."); break; } return(isFound); }
public static string ReadOperationHistoryRangeCppDLL(string machineId, string ipAddress, ushort portNo, string filePath) { int ret = 0; StringBuilder sb = new StringBuilder(); ushort focasLibHandle = 0; //Call C++ Dll method, which will fetch range of data from CNC and return the data as string to C# //C# will parse the return string and create the DataTable for BulkCopy try { ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret == 0) { FocasLib.cnc_stopophis(focasLibHandle); ushort totalHistoryRecords = 0; FocasLib.cnc_rdophisno(focasLibHandle, out totalHistoryRecords); for (ushort i = 1; i <= totalHistoryRecords; i++) { //TODO - Call C++ DLL function and appent the result to stringBuilder. sb.Append(""); } } } catch (Exception ex) { //TODO Log it } finally { FocasLib.cnc_startophis(focasLibHandle); } return(sb.ToString()); }
public static string ReadFullProgramPathRunningProgram(string ipAddress, ushort portNo) { short ret = -20; ushort focasLibHandle = 0; char[] buf = new char[256 + 1]; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(string.Empty); } ret = FocasLib.cnc_pdf_rdmain(focasLibHandle, buf); if (ret != 0) { return(string.Empty); } StringBuilder str = new StringBuilder(); str.Append(buf); return(str.ToString().Trim('\0')); }
//to download use below functions //cnc_upstart3,cnc_upload3, cnc_upend3 public static bool DownloadOperationHistory(string ipAddress, ushort portNo, string file) { short ret = -20; int bufsize = 1280; int dataLength = 0; int programNo = 0; ushort focasLibHandle = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); Logger.WriteErrorLog("Not able to connect to CNC machine. Please check the network connection and try again"); return(false); } int count = 0; ret = short.MinValue; while (ret != 0 && count < 20) { ret = FocasLib.cnc_upstart3(focasLibHandle, 7, programNo, programNo); count++; if (ret == -1) { Thread.Sleep(400); } } if (ret == -1) { Logger.WriteErrorLog("CNC is busy. Please try after some time."); //return false; } if (ret != 0) { Logger.WriteErrorLog("cnc_upstart3() failed. return value is = " + ret); FocasLib.cnc_freelibhndl(focasLibHandle); return(false); } StringBuilder programStr = new StringBuilder(bufsize * 10); bool endFound = false; do { char[] buf = new char[bufsize + 1]; dataLength = bufsize; ret = FocasLib.cnc_upload3(focasLibHandle, ref dataLength, buf); if (ret == 10) // if buffer is empty retry { Thread.Sleep(400); continue; } if (ret == -2) //if buffer is in reset mode { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } if (ret == -16) { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_upload3() failed.return value is = " + ret); break; } continue; } if (ret != 0) { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } char[] tempBuf = new char[dataLength]; Array.Copy(buf, tempBuf, dataLength); programStr.Append(tempBuf); if (buf[dataLength - 1] == '%') { endFound = true; break; } Thread.Sleep(100); } while (endFound == false); ret = FocasLib.cnc_upend3(focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_upend3() failed. return value is = " + ret); //return false; } if (string.IsNullOrEmpty(Convert.ToString(programStr))) { Logger.WriteDebugLog("program is empty."); FocasLib.cnc_freelibhndl(focasLibHandle); return(false); } programStr.Replace("\r", "").Replace("\n", "\r\n"); try { File.WriteAllText(file, programStr.ToString()); } catch (Exception ex) { Logger.WriteDebugLog(ex.ToString()); } FocasLib.cnc_freelibhndl(focasLibHandle); return(true); }
public static bool DownloadOperationHistory_TEST(string ipAddress, ushort portNo, int programNo) { short ret = -20; int bufsize = 2560; int dataLength = 0; ushort focasLibHandle = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(false); } ret = FocasLib.cnc_upstart3(focasLibHandle, 7, programNo, programNo); if (ret != 0) { Logger.WriteErrorLog("cnc_upstart3() failed. return value is = " + ret); return(false); } StringBuilder programStr = new StringBuilder(bufsize * 10); bool endFound = false; do { char[] buf = new char[bufsize + 1]; dataLength = bufsize; ret = FocasLib.cnc_upload3(focasLibHandle, ref dataLength, buf); if (ret == 10) // if buffer is empty retry { Thread.Sleep(400); continue; } if (ret == -2) //if buffer is in reset mode { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } if (ret != 0) { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } char[] tempBuf = new char[dataLength]; Array.Copy(buf, tempBuf, dataLength); programStr.Append(tempBuf); if (buf[dataLength - 1] == '%') { endFound = true; break; } Thread.Sleep(100); } while (endFound == false); ret = FocasLib.cnc_upend3(focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_upend3() failed. return value is = " + ret); } File.WriteAllText("D:\\O9999.txt", programStr.Replace("\r", "").ToString()); return(true); }
public static string DownloadProgram(string ipAddress, ushort portNo, int programNo, out bool result, string folderPath, bool isProgramFolderSupports) { result = false; short ret = -20; int bufsize = 1280; int dataLength = 0; ushort focasLibHandle = 0; string programDownloaded = string.Empty; string programNoStr = ""; if (isProgramFolderSupports) { programNoStr = "O" + programNo; } ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret + "Not able to connect to CNC machine. Please check the network connection and try again"); return(string.Empty); } int count = 0; ret = short.MinValue; while (ret != 0 && count < 60) { //ret = FocasLib.cnc_upstart3(focasLibHandle, 0, programNo, programNo); if (isProgramFolderSupports) { ret = FocasLib.cnc_upstart4(focasLibHandle, 0, Path.GetDirectoryName(folderPath).Replace("\\", "/") + "/" + programNoStr); } else { ret = FocasLib.cnc_upstart3(focasLibHandle, 0, programNo, programNo); } count++; if (ret == -1) { Thread.Sleep(1000); ret = FocasLib.cnc_upend4(focasLibHandle); } } if (ret == -1) { Logger.WriteDebugLog("CNC is busy. Please try after some time."); FocasLib.cnc_freelibhndl(focasLibHandle); return(string.Empty); } if (ret != 0) { Logger.WriteErrorLog("cnc_upstart3() failed. return value is = " + ret); FocasLib.cnc_freelibhndl(focasLibHandle); return(string.Empty); } StringBuilder programStr = new StringBuilder(bufsize * 10); bool endFound = false; do { char[] buf = new char[bufsize + 1]; dataLength = bufsize; //ret = FocasLib.cnc_upload3(focasLibHandle, ref dataLength, buf); if (isProgramFolderSupports) { ret = FocasLib.cnc_upload4(focasLibHandle, ref dataLength, buf); } else { ret = FocasLib.cnc_upload3(focasLibHandle, ref dataLength, buf); } if (ret == 10) // if buffer is empty retry { Thread.Sleep(400); continue; } if (ret == -2) //if buffer is in reset mode Write protected on CNC side { Logger.WriteErrorLog("Reset or stop request. The data to read is nothing. cnc_upload3() failed. return value is = " + ret); break; } if (ret == 7) //if buffer is in reset mode Write protected on CNC side { Logger.WriteErrorLog("Write protected on CNC side.. cnc_upload3() failed. return value is = " + ret); break; } if (ret != 0) { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } char[] tempBuf = new char[dataLength]; Array.Copy(buf, tempBuf, dataLength); programStr.Append(tempBuf); if (buf[dataLength - 1] == '%') { result = endFound = true; break; } Thread.Sleep(600); } while (endFound == false); if (ret == 7) { FocasLib.cnc_freelibhndl(focasLibHandle); return(string.Empty); } //ret = FocasLib.cnc_upend3(focasLibHandle); if (isProgramFolderSupports) { ret = FocasLib.cnc_upend4(focasLibHandle); } else { ret = FocasLib.cnc_upend3(focasLibHandle); } if (ret != 0) { Logger.WriteErrorLog("cnc_upend3() failed. return value is = " + ret); } if (string.IsNullOrEmpty(Convert.ToString(programStr))) { Logger.WriteDebugLog("program is empty."); FocasLib.cnc_freelibhndl(focasLibHandle); return(string.Empty); } programStr.Replace("\r", "").Replace("\n", "\r\n"); result = true; FocasLib.cnc_freelibhndl(focasLibHandle); return(programStr.ToString()); }
/*(9) CNC parameter error CNC parameter '3112#2' must be 1. */ public static List <OprMessageDTO> ReadExternalOperatorMessageHistory18i(string machineID, string ipAddress, ushort portNo) { int ret = 0; ushort focasLibHandle = 0; List <OprMessageDTO> oprMessagesObj = new List <OprMessageDTO>(); try { ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(null); } ret = FocasLib.cnc_stopomhis(focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_stopomhis() failed. return value is = " + ret); return(null); } FocasLibBase.ODBOMIF a = new FocasLibBase.ODBOMIF(); ret = FocasLib.cnc_rdomhisinfo(focasLibHandle, a); if (ret != 0) { Logger.WriteErrorLog("cnc_rdomhisinfo() failed. return value is = " + ret); return(null); } if (a.om_max == 0) { return(oprMessagesObj); } //TODO - check it? int totalMessages = a.om_max; //10 rows at a time ushort loop_count = (ushort)(totalMessages / 10); ushort remainder = (ushort)(totalMessages % 10); ushort i = 0, s_no = 0, e_no = 0; //ushort length = 4 + 512 * 10; for (i = 0; i <= loop_count; i++) { if (i == loop_count) { if (remainder > 0) { s_no = (ushort)(i * 10 + 1); e_no = (ushort)(s_no + (remainder - 1)); FocasLibBase.ODBOMHIS obj = new FocasLibBase.ODBOMHIS(); ushort length = (ushort)Marshal.SizeOf(obj); ret = FocasLib.cnc_rdomhistry(focasLibHandle, s_no, ref e_no, obj); if (ret != 0) { Logger.WriteErrorLog("cnc_rdomhistry() failed. return value is = " + ret); continue; } AddMessagesToList(obj, oprMessagesObj); try { oprMessagesObj.RemoveRange(totalMessages, oprMessagesObj.Count - totalMessages); } catch (Exception exxx) { Logger.WriteErrorLog(exxx.ToString()); } } } else { s_no = (ushort)(i * 10 + 1); e_no = (ushort)(s_no + 9); FocasLibBase.ODBOMHIS obj = new FocasLibBase.ODBOMHIS(); ushort length = (ushort)Marshal.SizeOf(obj); ret = FocasLib.cnc_rdomhistry(focasLibHandle, s_no, ref e_no, obj); if (ret != 0) { Logger.WriteErrorLog("cnc_rdomhistry() failed. return value is = " + ret); continue; } //TODO - add the message based on s_no and e_no AddMessagesToList(obj, oprMessagesObj); } } } catch (Exception ex) { Logger.WriteErrorLog(ex.ToString()); } finally { if (focasLibHandle > 0) { ret = FocasLib.cnc_startophis(focasLibHandle); FocasData.cnc_freelibhndl(focasLibHandle); } } return(oprMessagesObj); }
public static DataTable ReadAlarmHistory(string machineID, string ipAddress, ushort portNo) { int ret = 0; ushort focasLibHandle = 0; DataTable alarms = Utility.get_table(); try { ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret == 0) { ret = FocasLib.cnc_stopophis(focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_stopophis() failed. return value is = " + ret); } ushort totalAlarms = 0; ret = FocasLib.cnc_rdalmhisno(focasLibHandle, out totalAlarms); if (ret != 0) { Logger.WriteErrorLog("cnc_rdalmhisno() failed. return value is = " + ret); } if (totalAlarms == 0) { return(alarms); } //10 rows at a time DataRow row = default(DataRow); FocasLibBase.ODBAHIS5 obj = new FocasLibBase.ODBAHIS5(); List <FocasLibBase.ALM_HIS5_data> focasAlarmsObj = new List <FocasLibBase.ALM_HIS5_data>();//almhisdata object list ushort loop_count = (ushort)(totalAlarms / 10); ushort remainder = (ushort)(totalAlarms % 10); ushort i = 0, s_no = 0, e_no = 0; //ushort length = 4 + 512 * 10; ushort length = (ushort)Marshal.SizeOf(obj); for (i = 0; i <= loop_count; i++) { if (i == loop_count) { if (remainder == 0) { continue; } s_no = (ushort)(i * 10 + 1); e_no = (ushort)(s_no + (remainder - 1)); ret = FocasLib.cnc_rdalmhistry5(focasLibHandle, s_no, e_no, length, obj); if (ret != 0) { Logger.WriteErrorLog("cnc_rdalmhistry5() failed. return value is = " + ret); } focasAlarmsObj.Clear(); get_ahd_objects(ref focasAlarmsObj, obj); for (int j = 0; j < remainder; j++) { row = alarms.NewRow(); Utility.get_datatable_row(focasAlarmsObj[j], ref row); row["MachineID"] = machineID; alarms.Rows.Add(row); } } else { s_no = (ushort)(i * 10 + 1); e_no = (ushort)(s_no + 9); ret = FocasLib.cnc_rdalmhistry5(focasLibHandle, s_no, e_no, length, obj); if (ret != 0) { Logger.WriteErrorLog("cnc_rdalmhistry5() failed. return value is = " + ret); } focasAlarmsObj.Clear(); get_ahd_objects(ref focasAlarmsObj, obj);// for (int j = 0; j < 10; j++) { row = alarms.NewRow(); Utility.get_datatable_row(focasAlarmsObj[j], ref row); row["MachineID"] = machineID; alarms.Rows.Add(row); } } } } } catch (Exception ex) { Logger.WriteErrorLog(ex.ToString()); } finally { if (focasLibHandle > 0) { ret = FocasLib.cnc_startophis(focasLibHandle); FocasData.cnc_freelibhndl(focasLibHandle); } } return(alarms); }
public static bool DeletePrograms(string ipAddress, ushort portNo, string programNumber, string folderPath, bool isProgramFolderSupports, bool showMsg = true) { short tempProgramNo = 0; string tempProgramNoStr = ""; if (isProgramFolderSupports == false) { short.TryParse(programNumber.TrimStart(new char[] { 'O', ' ' }), out tempProgramNo); if (tempProgramNo == 0) { return(false); } } else { tempProgramNoStr = programNumber; } bool isdeleted = false; focas_ret ret; int ret1 = -1; ushort focasLibHandle = 0; ret1 = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret1 != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret1); Logger.WriteErrorLog("Not able to connect to CNC machine. Please check the network connection and try again"); return(false); } if (isProgramFolderSupports == true) { string fileName = folderPath + tempProgramNoStr; ret1 = FocasLib.cnc_pdf_del(focasLibHandle, fileName); } else { ret1 = FocasLib.cnc_delete(focasLibHandle, tempProgramNo); } if (ret1 != 0 && ret1 != 5) { Logger.WriteErrorLog("Not able to delete program. Return value = " + ret1); } Enum.TryParse <focas_ret>(ret1.ToString(), out ret); switch (ret) { case focas_ret.EW_OK: if (showMsg) { Logger.WriteDebugLog("PROGRAM " + programNumber + " has been deleted."); // MessageBox.Show("Program " + programNumber + " has been deleted."); } isdeleted = true; break; case focas_ret.EW_DATA: Logger.WriteDebugLog("PROGRAM " + programNumber + " doesn't exist."); //MessageBox.Show("PROGRAM " + programNumber + " doesn't exist."); break; case focas_ret.EW_PROT: Logger.WriteDebugLog("Program " + programNumber + " is PROTECTED."); break; case focas_ret.EW_BUSY: Logger.WriteDebugLog("CNC is busy. Program " + programNumber + " delete REJECTED."); break; case focas_ret.EW_PASSWD: Logger.WriteDebugLog("Specified program number cannot be deleted because the data is protected."); break; //case focas_ret.DEL_FAILED: // break; } FocasLib.cnc_freelibhndl(focasLibHandle); return(isdeleted); }
public static short DownloadProgram(string ipAddress, ushort portNo, string programNoToDownload, string destSystemFolder, string CNCfolderPath, bool isProgramFolderSupports) { int programNo = 0; string programNoStr = ""; if (isProgramFolderSupports == false) { programNo = Convert.ToInt32(programNoToDownload.Replace(" ", "").TrimStart(new char[] { 'O' })); } else { programNoStr = programNoToDownload; } short ret = -20; int bufsize = 1280; int dataLength = 0; ushort focasLibHandle = 0; string programDownloaded = string.Empty; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(2); } int count = 0; ret = short.MinValue; while (ret != 0 && count < 100) { if (isProgramFolderSupports) { ret = FocasLib.cnc_upstart4(focasLibHandle, 0, CNCfolderPath + programNoStr); } else { ret = FocasLib.cnc_upstart3(focasLibHandle, 0, programNo, programNo); } count++; if (ret == -1) { Thread.Sleep(200); } } if (ret == -1) { Logger.WriteErrorLog("CNC is busy. Please try after some time."); FocasLib.cnc_freelibhndl(focasLibHandle); return(3); } if (ret != 0) { Logger.WriteErrorLog("cnc_upstart3/4() failed. return value is = " + ret); FocasLib.cnc_freelibhndl(focasLibHandle); return(2); } StringBuilder programStr = new StringBuilder(bufsize * 10); bool endFound = false; do { char[] buf = new char[bufsize + 1]; dataLength = bufsize; if (isProgramFolderSupports) { ret = FocasLib.cnc_upload4(focasLibHandle, ref dataLength, buf); } else { ret = FocasLib.cnc_upload3(focasLibHandle, ref dataLength, buf); } if (ret == 10) // if buffer is empty retry { Thread.Sleep(400); continue; } if (ret == -2) //if buffer is in reset mode Write protected on CNC side { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } if (ret == 7) //if buffer is in reset mode Write protected on CNC side { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } if (ret != 0) { Logger.WriteErrorLog("cnc_upload3() failed. return value is = " + ret); break; } char[] tempBuf = new char[dataLength]; Array.Copy(buf, tempBuf, dataLength); programStr.Append(tempBuf); if (buf[dataLength - 1] == '%') { endFound = true; break; } Thread.Sleep(100); } while (endFound == false); if (ret == 7) { FocasLib.cnc_freelibhndl(focasLibHandle); return(2); } if (isProgramFolderSupports) { ret = FocasLib.cnc_upend4(focasLibHandle); } else { ret = FocasLib.cnc_upend3(focasLibHandle); } if (ret != 0) { Logger.WriteErrorLog("cnc_upend34() failed. return value is = " + ret); FocasLib.cnc_freelibhndl(focasLibHandle); return(2); } if (string.IsNullOrEmpty(Convert.ToString(programStr))) { Logger.WriteDebugLog("program is empty."); FocasLib.cnc_freelibhndl(focasLibHandle); return(6); } programStr.Replace("\r", "").Replace("\n", "\r\n"); // string comments = FindProgramComment(programStr.ToString()); programNoToDownload = string.Format("{0}", isProgramFolderSupports ? programNoStr : "O" + programNo.ToString()); int status = 0; string version = GetVersion(destSystemFolder, programNoToDownload, programStr, out status); if (status == 1) { Logger.WriteDebugLog(version); version = string.Empty; return(0); } if (version.ToLower().IndexOf("ERROR") >= 0) { Logger.WriteDebugLog("Code Error"); FocasLib.cnc_freelibhndl(focasLibHandle); return(7); } try { File.WriteAllText(version, programStr.ToString()); } catch (Exception ex) { Logger.WriteDebugLog(ex.ToString()); } if (File.Exists(version)) { programDownloaded = Path.GetFileNameWithoutExtension(version); Logger.WriteDebugLog(string.Format("Program {0} downloaded/saved successfully.", programDownloaded)); } FocasLib.cnc_freelibhndl(focasLibHandle); return(0); }
//to sent to cnc //cnc_dwnstart3, cnc_download3, cnc_dwnend3 public static bool UploadProgramHytech(string ipAddress, ushort portNo, string programContentToSend, string folderPath, bool isProgramFolderSupports) { short ret = -20; int len, n; ushort focasLibHandle = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(false); } int count = 0; ret = short.MinValue; while (ret != 0 && count < 100) { if (isProgramFolderSupports == false) { ret = FocasLib.cnc_dwnstart3(focasLibHandle, 0); } else { ret = FocasLib.cnc_dwnstart4(focasLibHandle, 0, folderPath); } count++; if (ret == -1) { Thread.Sleep(200); } } if (ret == -1) { if (isProgramFolderSupports == false) { FocasLib.cnc_dwnend(focasLibHandle); } else { FocasLib.cnc_dwnend4(focasLibHandle); } FocasLib.cnc_freelibhndl(focasLibHandle); Logger.WriteErrorLog("CNC is busy. Please try after some time."); return(false); } if (ret != 0) { Logger.WriteErrorLog("cnc_dwnstart3() failed. return value is = " + ret); FocasLib.cnc_freelibhndl(focasLibHandle); return(false); } if (ret == FocasLib.EW_OK) { len = programContentToSend.Length; while (len > 0) { n = programContentToSend.Length; if (isProgramFolderSupports == false) { ret = FocasLib.cnc_download3(focasLibHandle, ref n, programContentToSend); } else { ret = FocasLib.cnc_download4(focasLibHandle, ref n, programContentToSend); } if (ret == 10) //if buffer is empty { continue; } if (ret == -2) // if buffer in reset mode { break; } if (ret != FocasLib.EW_OK) { Logger.WriteErrorLog("cnc_download3() failed. return value is = " + ret); break; } if (ret == FocasLib.EW_OK) { //program += n; //len -= n; programContentToSend = programContentToSend.Substring(n, len - n); len -= n; } } //Thread.Sleep(4000); if (isProgramFolderSupports == false) { ret = FocasLib.cnc_dwnend(focasLibHandle); } else { ret = FocasLib.cnc_dwnend4(focasLibHandle); } if (ret != FocasLib.EW_OK) { FocasLibBase.ODBERR a = new FocasLibBase.ODBERR(); FocasLib.cnc_getdtailerr(focasLibHandle, a); Logger.WriteErrorLog("cnc_dwnend() failed. return value is = " + ret + " ; Error code : " + a.err_no); FocasLib.cnc_freelibhndl(focasLibHandle); if (ret != 7) { Logger.WriteErrorLog("cnc_dwnend() failed. return value is = " + ret + " ; Error code : " + a.err_no); } if (ret == 7) { Logger.WriteErrorLog("Upload program failed because write protected on CNC side."); } return(false); } FocasLib.cnc_freelibhndl(focasLibHandle); } return(true); }
//cnc_rdprogdir3 public static List <ProgramDTO> ReadAllPrograms(string ipAddress, ushort portNo, short programDetailType = 2) { List <ProgramDTO> programs = new List <ProgramDTO>(); int topProgram = 0; short prgromsToRead = 10; short ret = 0; ushort focasLibHandle = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(null); } ret = 0; while (prgromsToRead >= 10) { FocasLibBase.PRGDIR3 d = new FocasLibBase.PRGDIR3(); try { ret = FocasLib.cnc_rdprogdir3(focasLibHandle, programDetailType, ref topProgram, ref prgromsToRead, d); } catch (Exception ex) { Logger.WriteErrorLog(ex.ToString()); } if (ret != 0) { Logger.WriteErrorLog("cnc_rdprogdir3() failed. return value is = " + ret); break; } if (prgromsToRead == 0) { Logger.WriteErrorLog("No programs found."); break; } if (prgromsToRead >= 1) { get_program(programs, d.dir1); } if (prgromsToRead >= 2) { get_program(programs, d.dir2); } if (prgromsToRead >= 3) { get_program(programs, d.dir3); } if (prgromsToRead >= 4) { get_program(programs, d.dir4); } if (prgromsToRead >= 5) { get_program(programs, d.dir5); } if (prgromsToRead >= 6) { get_program(programs, d.dir6); } if (prgromsToRead >= 7) { get_program(programs, d.dir7); } if (prgromsToRead >= 8) { get_program(programs, d.dir8); } if (prgromsToRead >= 9) { get_program(programs, d.dir9); } if (prgromsToRead >= 10) { get_program(programs, d.dir10); } topProgram = programs[programs.Count - 1].ProgramNo + 1; } return(programs); }
//to sent to cnc //cnc_dwnstart3, cnc_download3, cnc_dwnend3 public static bool UploadProgram(string ipAddress, ushort portNo, string program) { short ret = -20; int len, n; program = File.ReadAllText("D:\\O12345.txt"); ushort focasLibHandle = 0; ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out focasLibHandle); if (ret != 0) { Logger.WriteErrorLog("cnc_allclibhndl3() failed. return value is = " + ret); return(false); } ret = FocasLib.cnc_dwnstart3(focasLibHandle, 0); if (ret != 0) { Logger.WriteErrorLog("cnc_dwnstart3() failed. return value is = " + ret); return(false); } if (ret == FocasLib.EW_OK) { len = program.Length; while (len > 0) { n = program.Length; ret = FocasLib.cnc_download3(focasLibHandle, ref n, program); if (ret == 10) //if buffer is empty { continue; } if (ret == -2) // if buffer in reset mode { break; } if (ret != FocasLib.EW_OK) { Logger.WriteErrorLog("cnc_download3() failed. return value is = " + ret); break; } if (ret == FocasLib.EW_OK) { //program += n; //len -= n; program = program.Substring(n, len - n); len -= n; } } ret = FocasLib.cnc_dwnend(focasLibHandle); if (ret != FocasLib.EW_OK) { Logger.WriteErrorLog("cnc_dwnend() failed. return value is = " + ret); } } return(true); }
public static short cnc_allclibhndl3(string ip, ushort port, int timeout, out ushort handle) { return(FocasLib.cnc_allclibhndl3(ip, port, timeout, out handle)); }
//TODO - complte it with datatable and database insert public static DataTable ReadOperationhistory18i(string machineID, string ipAddress, ushort portNo) { DataTable table = new DataTable(); ushort handle = ushort.MinValue; int ret = 0; try { ret = FocasLib.cnc_allclibhndl3(ipAddress, portNo, 10, out handle); if (ret == 0) { ret = FocasLib.cnc_stopophis(handle); if (ret != 0) { Logger.WriteErrorLog("cnc_stopophis() failed. return value is = " + ret); } ushort totalHistoryRecords = 0; ret = FocasLib.cnc_rdophisno(handle, out totalHistoryRecords); if (ret != 0) { Logger.WriteErrorLog("cnc_rdophisno() failed. return value is = " + ret); } Logger.WriteDebugLog("Total number of rows found in Operation History = " + totalHistoryRecords); if (totalHistoryRecords == 0) { return(table); } ushort returnRecordnumber = 0; FocasLibBase.ODBHIS obj = new FocasLibBase.ODBHIS(); ushort objLength = (ushort)Marshal.SizeOf(obj); int length = totalHistoryRecords; returnRecordnumber = 1; for (ushort i = 1; i <= length; i++) { returnRecordnumber = i; obj = new FocasLibBase.ODBHIS(); ret = FocasLib.cnc_rdophistry(handle, i, returnRecordnumber, objLength, obj); /* * rec_type : Record type Description * 0 : MDI key history * 1 : Signal history * 2 : Alarm history * 3 : Date history * 4 : Time history * 5 : MDI key history for SUB (only Series 160/180/210, Power Mate i) * 6 : Signal history for SUB (only Series 160/180/210, Power Mate i) * 7 : Alarm history for SUB (only Series 160/180/210, Power Mate i) * 10 : MDI key history for 3rd Path (only Series 160i) * 11 : Signal history for 3rd Path (only Series 160i) * 12 : Alarm history for 3rd Path (only Series 160i) */ if (obj.data.data1.rec_type == 3) //3 : Date history { Debug.Print("YEAR = " + Convert.ToString(obj.data.data1.date_year.ToString())); Debug.Print("MONTH = " + obj.data.data1.date_month.ToString()); Debug.Print("DAY = " + obj.data.data1.date_day.ToString()); } if (obj.data.data1.rec_type == 4) //4 : Time history { Debug.Print("hh = " + Convert.ToString(obj.data.data1.time_hour)); Debug.Print("MM = " + obj.data.data1.time_minute.ToString()); Debug.Print("SS = " + obj.data.data1.time_second.ToString()); } if (obj.data.data1.rec_type == 1) // 1 : Signal history { Debug.Print("sgn_sig_name = " + Convert.ToString(obj.data.data1.sgn_sig_name)); Debug.Print("sgn_sig_no = " + obj.data.data1.sgn_sig_no.ToString()); Debug.Print("nEW = " + obj.data.data1.sgn_sig_new.ToString()); Debug.Print("OLD " + obj.data.data1.sgn_sig_old.ToString()); } if (obj.data.data1.rec_type == 2) // 2 : Alarm history { Debug.Print("alm_alm_no = " + Convert.ToString(obj.data.data1.alm_alm_no)); Debug.Print("alm_alm_grp = " + obj.data.data1.alm_alm_grp); Debug.Print("alm_axis_no = " + obj.data.data1.alm_axis_no); Debug.Print("alm_dummy " + obj.data.data1.alm_dummy); } } } } catch (Exception ex) { Logger.WriteErrorLog(ex.ToString()); } finally { if (handle != ushort.MinValue) { FocasLib.cnc_startophis(handle); FocasData.cnc_freelibhndl(handle); } } return(table); }