using System; using System.Text; using System.Threading; using System.Diagnostics; using System.Text.RegularExpressions; namespace FanucProgramManager { class FANUCMachine { private static string get_cnc_path(int path = 1) { return "//CNC_MEM/USER/PATH" + path.ToString() + "/"; } /// /// Parametri connessione /// private string Name; private string IPAddress; //IP ethernet private string Port; //Port ethernet private int Node; //nodo HSSB per connessione si tipo allclibhndl2 private int ReadTime; private int ConnectionType; private Boolean compatibiltyMode; public bool CONNECTED; private string ConnectionStatus { get; set; } public bool ConnectionRequest; short[] ret_array = new short[11] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // valore di ritorno della funzioni richiamate dalle librerie FOCAS, in generale se ret == 0 // la funzione e' terminata senza errori // in ordine nell'array: // 0 -> connect // 1 -> disconnect // 2 -> cicle time // 3 -> pieces counter // 4 -> alarm messages // 5 -> operator messages // 6 -> machine state // 7 -> main program // 8 -> comment program // 9 -> download // 10 -> upload ushort hndl; // handle di comunicazione: questo valore viene scritto una volta eseguita la connessione // questo valore si passa come riferimento ad ogni funzione focas e rappresenta i // parametri di connessione private string FOCAS_ERRORS = ""; private string machine_status = ""; /// /// Strutture per la lettura dati da controlli FANUC. /// // Strutture per tempo ciclo e contapezzi Focas1.IODBPSD cicloMSp1 = new Focas1.IODBPSD(); Focas1.IODBPSD ciclo2MINp1 = new Focas1.IODBPSD(); Focas1.IODBPSD ContaPezzip1 = new Focas1.IODBPSD(); // struttura dati per memorizzazione contapezzi path 1 private string StrContaPezzip1; private string StrTempoCiclop1; // Strutture per nome programma Focas1.PRGDIR3 info_prg = new Focas1.PRGDIR3(); Focas1.ODBPRO prognum = new Focas1.ODBPRO(); private string NomeProgrammaISO; private string NomeSubProgrammaISO; private string CommentoProgrammaISO; // Strutture per allarmi private string[] AlarmMessage = new string[17]; // Strutture per messaggi operatore private string[] OperatorMessage = new string[17]; // Strutture per stati Focas1.ODBST odbst = new Focas1.ODBST(); //Strutture per letture range di dati pmc Focas1.IODBPMC0 iodbpmcByte = new Focas1.IODBPMC0(); // 8 Bit Focas1.IODBPMC1 iodbpmcShort = new Focas1.IODBPMC1(); // 16 Bit Focas1.IODBPMC2 iodbpmcInt = new Focas1.IODBPMC2(); //32bit //relative al contapezzi short CPMacroPiecesCounter = -1; //Variabile macro dove viene aggiornato il contapezzi #500 a #999 memria ritentiva short CPMacroLength = 1; //quante variabili macro leggere short CPlength = 4; //numero di byte scelto in CPdata_type (X) //short CPdata_type = 2; // 0 Byte (1), 1 Short(2), 2 int (4) //ushort CPStart; public string GetFocasErrorsBuffer() { string errorBuffer = FOCAS_ERRORS; FOCAS_ERRORS = ""; return errorBuffer; } public string GetStatus() { return machine_status; } public string GetNomeProgramma() { if (NomeProgrammaISO == null) { return "null"; } else return NomeProgrammaISO; } public string GetSubProgramma() { if (NomeSubProgrammaISO == null) { return "null"; } else return NomeSubProgrammaISO; } public string GetCommentoProgramma() { if (CommentoProgrammaISO == null) { return "Commento Assente"; } else return CommentoProgrammaISO; } public string[] GetAlarmMessage() { return AlarmMessage; } public string[] GetOperatorMessage() { return OperatorMessage; } public string GetStrContaPezzip1() { if (StrContaPezzip1 == null) { return "0"; } else return StrContaPezzip1; } public string GetStrTempoCiclop1() { if (StrTempoCiclop1 == null) { return "null"; } else return StrTempoCiclop1; } public string GetName() { if (Name == null) { return "null"; } else return Name; } public ushort GetHandle() { return hndl; } private string getFocasFunctionName(short index) { string functionName = ""; switch (index) { case 0: functionName = "connect"; break; case 1: functionName = "disconnect"; break; case 2: functionName = "cicle time"; break; case 3: functionName = "pieces counter"; break; case 4: functionName = "alarm messages"; break; case 5: functionName = "operator messages"; break; case 6: functionName = "machine state"; break; case 7: functionName = "main program and comment"; break; case 8: functionName = "download"; break; case 9: functionName = "upload"; break; } return functionName; } public FANUCMachine(string name, string ipaddress, string port, Boolean compatibiltyMode, int readtime, int connectiontype, int MacroPiecesCounter) { this.Name = name; this.IPAddress = ipaddress; this.Port = port; this.compatibiltyMode = compatibiltyMode; this.Node = -1; this.ConnectionStatus = "Not Connected"; this.ConnectionRequest = false; this.NomeProgrammaISO = ""; this.NomeSubProgrammaISO = ""; this.ReadTime = readtime; this.ConnectionType = connectiontype; this.ConnectionRequest = true; this.CONNECTED = false; this.CPMacroPiecesCounter = (short)MacroPiecesCounter; } public FANUCMachine(string name, int node, Boolean compatibiltyMode, int readtime, int connectiontype, int MacroPiecesCounter) //Per controlli HSSB { this.Name = name; this.IPAddress = null; this.Port = null; this.compatibiltyMode = compatibiltyMode; this.Node = node; //il paramatro node serve solo nel caso connectiontype = 2 altrimenti puo essere messo a 0 this.ConnectionStatus = "Not Connected"; this.ConnectionRequest = false; this.NomeProgrammaISO = ""; this.NomeSubProgrammaISO = ""; this.ReadTime = readtime; this.ConnectionType = connectiontype; this.ConnectionRequest = true; this.CONNECTED = false; this.CPMacroPiecesCounter = (short)MacroPiecesCounter; } private short readCicleTime() { // read cycle time short time_ret = 0; //millisecondi time_ret = Focas1.cnc_rdparam(hndl, 6757, 0, 8, cicloMSp1); //minuti time_ret = (short)(time_ret + Focas1.cnc_rdparam(hndl, 6758, 0, 8, ciclo2MINp1)); StrTempoCiclop1 = ciclo2MINp1.u.ldata.ToString() + " minuti : " + (cicloMSp1.u.ldata / 1000).ToString() + " secondi"; return time_ret; } private short readAlarmMessages() { // Dati di stato macchina // Allarmi short num_alm = 17; short almmsg_ret = -1; if (compatibiltyMode) { Focas1.ODBALMMSG_data_custom[] alm = new Focas1.ODBALMMSG_data_custom[17]; almmsg_ret = Focas1.cnc_rdalmmsg_custom(hndl, 0, ref num_alm, alm); for (int i = 0; i < num_alm; i++) { AlarmMessage[i] = ""; if (alm[i].msg_len > 0) AlarmMessage[i] = alm[i].alm_msg; } } else { //Plese use cnc_rdalmmsg2 fucntion for Series 30i/31i/32i, 0i-D/F and PMi-A. Focas1.ODBALMMSG2_data_custom[] alm = new Focas1.ODBALMMSG2_data_custom[17]; almmsg_ret = Focas1.cnc_rdalmmsg2_custom(hndl, -1, ref num_alm, alm); for (int i = 0; i < num_alm; i++) { AlarmMessage[i] = ""; if (alm[i].msg_len > 0) AlarmMessage[i] = alm[i].alm_msg; } } return almmsg_ret; } private short readOperatorMessages() { // Messaggi operatore short num_alm = 1; short opmsg_ret = 0; Focas1.OPMSG3_data_custom[] opmsg = new Focas1.OPMSG3_data_custom[17]; opmsg_ret = Focas1.cnc_rdopmsg3_custom(hndl, 4, ref num_alm, opmsg); for (int i = 0; i < num_alm; i++) { OperatorMessage[i] = ""; if (opmsg[i].char_num > 0) OperatorMessage[i] = opmsg[i].data; } return opmsg_ret; } private short readMachineState() { // Stati macchina short state_ret = Focas1.cnc_statinfo(hndl, odbst); if (state_ret == Focas1.EW_OK) { machine_status = ""; switch (odbst.aut) { case 0: machine_status += "MDI -"; break; case 1: machine_status += "AUTO -"; break; case 3: machine_status += "EDIT -"; break; case 4: machine_status += "HNDL -"; break; case 5: machine_status += "JOG -"; break; default: machine_status += "**** -"; break; } if (odbst.aut != 3) { ret_array[7] = readMainProgram(); ret_array[8] = readMainProgramComment(); } machine_status += " "; switch (odbst.run) { case 1: machine_status += "STOP - "; break; case 3: machine_status += "ESEC ISO - "; break; default: machine_status += " **** - "; break; } machine_status += " "; switch (odbst.motion) { case 1: machine_status += "IN MOVIMENTO - "; break; case 2: machine_status += "PAUSA - "; break; default: machine_status += "*** - "; break; } machine_status += " "; switch (odbst.mstb) { case 1: machine_status += "FIN - "; break; default: machine_status += "*** - "; break; } machine_status += " "; switch (odbst.emergency) { case 1: machine_status += "EMERGENZA -"; break; case 2: machine_status += "RST -"; break; default: machine_status += "*** -"; break; } machine_status += " "; switch (odbst.alarm) { case 1: machine_status += "ALM"; break; default: machine_status += "***"; break; } } return state_ret; } public short readMainProgram() { short program_ret = 0; Focas1.ODBPRO prog_num = new Focas1.ODBPRO(); // nome programma program_ret = Focas1.cnc_rdprgnum(hndl, prog_num); NomeSubProgrammaISO = "O" + prog_num.data.ToString(); NomeProgrammaISO = "O" + prog_num.mdata.ToString(); if (!compatibiltyMode) { byte[] nome_main = new byte[242]; // nome programma program_ret = Focas1.cnc_pdf_rdmain(hndl, nome_main); string str_main = ASCIIEncoding.ASCII.GetString(nome_main).TrimEnd('\0'); string[] words = str_main.Split('/'); int indiceUltimoElemento = words.Length - 1; if (NomeProgrammaISO != words[indiceUltimoElemento]) NomeProgrammaISO = words[indiceUltimoElemento]; } return program_ret; } public short readMainProgramComment() { short program_ret = 0; string[] words; byte[] alpha_comment = new byte[100]; if (compatibiltyMode) { ushort caratteriDaLeggere = 100; program_ret = Focas1.cnc_rdexecprog(hndl, ref caratteriDaLeggere, out short blknumber, alpha_comment); } else { uint lunghezza = 100; //va lasciato qui perche' viene riscritto ogni volta da "cnc_rdpdf_line" uint numero_righe = 1; //va lasciato qui perche' viene riscritto ogni volta da "cnc_rdpdf_line" byte[] nome_main = new byte[242]; program_ret = Focas1.cnc_pdf_rdmain(hndl, nome_main); string str_main = ASCIIEncoding.ASCII.GetString(nome_main).TrimEnd('\0'); program_ret = Focas1.cnc_rdpdf_line(hndl, str_main, 0, alpha_comment, ref numero_righe, ref lunghezza); } string str_alpha_comment = ASCIIEncoding.ASCII.GetString(alpha_comment).TrimEnd('\0'); // caso commento presente if (str_alpha_comment.Contains("(")) { words = str_alpha_comment.Split('(', ')'); int indiceUltimoElemento = words.Length - 2; //CommentoProgrammaISO = '(' + words[indiceUltimoElemento] + ')'; int open_index = str_alpha_comment.IndexOf('('); int close_index = str_alpha_comment.IndexOf(')'); if (open_index > 0) { CommentoProgrammaISO = str_alpha_comment.Substring(open_index + 1, close_index - open_index - 1); } } // caso di programma senza commento else { // CommentoProgrammaISO = '(' + NomeProgrammaISO + ')'; CommentoProgrammaISO = "Commento Assente"; } return program_ret; } public void Read() { // leggo i dati // Dati di produzione // path 1 Focas1.cnc_setpath(hndl, 1); short a, b; Focas1.cnc_getpath(hndl, out a, out b); ret_array[2] = readCicleTime(); ret_array[3] = readContaPezzi(); ret_array[4] = readAlarmMessages(); ret_array[5] = readOperatorMessages(); ret_array[6] = readMachineState(); CheckErrors(ret_array); } public void Connect() { //Thread.Sleep(500); if (ConnectionRequest) { if (!CONNECTED && ConnectionType == 3) { // apro il canale di comunicazione verso la macchina short timeout = 4; //sec ret_array[0] = Focas1.cnc_allclibhndl3(IPAddress, Convert.ToUInt16(Port), timeout, out hndl); } else if (!CONNECTED && ConnectionType == 0) { ret_array[0] = Focas1.cnc_allclibhndl(out hndl); } else if (!CONNECTED && ConnectionType == 2) { ret_array[0] = Focas1.cnc_allclibhndl2(Node, out hndl); } if (ret_array[0] == 0) { // abilito il timer: comincio a fare il refresh dei dati su Cn e Robot CONNECTED = true; machine_status = "CONNECTED"; ConnectionRequest = false; } else { ConnectionRequest = true; CONNECTED = false; machine_status = "DISCONNECTED"; CheckErrors(ret_array); } } } public void Disconnect() { ret_array[1] = Focas1.cnc_freelibhndl(hndl); if (ret_array[1] == 0) { ConnectionRequest = true; CONNECTED = false; machine_status = "DISCONNECTED"; } resetMachineRet(); } private void resetMachineRet() { for (short index = 0; index < ret_array.Length; index++) { ret_array[index] = 0; } } private void CheckErrors(short[] error) { FOCAS_ERRORS = ""; bool disconnect = false; for (short index = 0; index < error.Length; index++) { string focas_function = getFocasFunctionName(index); switch (error[index]) { case (short)Focas1.focas_ret.EW_PROTOCOL: /* protocol error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "protocol error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_SOCKET: /* Windows socket error */ disconnect = CONNECTED; FOCAS_ERRORS += "Error " + error[index] + ": " + "windows socket error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_NODLL: /* DLL not exist error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "DLL not exist error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_BUS: /* bus error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "bus error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_SYSTEM2: /* system error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "system error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_HSSB: /* hssb communication error */ FOCAS_ERRORS += "Error " + error[index] + ": " + " hssb communication error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_HANDLE: /* Windows library handle error */ FOCAS_ERRORS += "Error " + error[index] + ": " + " Windows library handle error" + " in " + focas_function + "\n"; disconnect = CONNECTED; break; case (short)Focas1.focas_ret.EW_VERSION: /* CNC/PMC version missmatch */ FOCAS_ERRORS += "Error " + error[index] + ": " + " CNC/PMC version missmatch" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_UNEXP: /* abnormal error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "abnormal error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_SYSTEM: /* system error */ FOCAS_ERRORS += "Error " + error[index] + ": " + " system error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_PARITY: /* shared RAM parity error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "shared RAM parity error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_MMCSYS: /* emm386 or mmcsys install error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "emm386 or mmcsys install error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_RESET: /* reset or stop occured error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "reset or stop occured error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_BUSY: /* busy error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "busy error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_OK: /* no problem */ break; case (short)Focas1.focas_ret.EW_FUNC: /* command prepare error --or-- pmc not exist */ FOCAS_ERRORS += "Error " + error[index] + ": " + "command prepare error --or-- pmc not exist" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_LENGTH: /* data block length error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "data block length error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_NUMBER: /* data number error --or-- address range error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "data number error --or-- address range error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_ATTRIB: /* data attribute error --or-- data type error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "data attribute error --or-- data type error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_DATA: /* data error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "data error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_NOOPT: /* no option error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "no option error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_PROT: /* write protect error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "write protect error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_OVRFLOW: /* memory overflow error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "memory overflow error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_PARAM: /* cnc parameter not correct error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "cnc parameter not correct error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_BUFFER: /* buffer error */ FOCAS_ERRORS += "Error " + error[index] + ": " + " buffer error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_PATH: /* path error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "path error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_MODE: /* cnc mode error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "cnc mode error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_REJECT: /* execution rejected error */ FOCAS_ERRORS += "Error " + error[index] + ": " + "execution rejected error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_DTSRVR: /* data server error */ FOCAS_ERRORS += "Error " + error[index] + ": " + " data server error" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_ALARM: /* alarm has been occurred */ FOCAS_ERRORS += "Error " + error[index] + ": " + "alarm has been occurred" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_STOP: /* CNC is not running */ FOCAS_ERRORS += "Error " + error[index] + ": " + " CNC is not running" + " in " + focas_function + "\n"; break; case (short)Focas1.focas_ret.EW_PASSWD: /* protection data error */ FOCAS_ERRORS += "Error " + error[index] + ": " + " protection data error" + " in " + focas_function + "\n"; break; default: break; } if (FOCAS_ERRORS == "") { FOCAS_ERRORS = "OK"; } if (disconnect) { Disconnect(); } } } private short readContaPezzi() { short pieces_ret = 0; try { if (CPMacroPiecesCounter == -1) { // Conta pezzi pieces_ret = Focas1.cnc_rdparam(hndl, 6711, 0, 8, ContaPezzip1); StrContaPezzip1 = ContaPezzip1.u.ldata.ToString(); } else { CPlength = (short)(8 + 8 * 1); Focas1.ODBM macro = new Focas1.ODBM(); pieces_ret = Focas1.cnc_rdmacro(hndl, CPMacroPiecesCounter, 10, macro); if (pieces_ret == Focas1.EW_OK) { StrContaPezzip1 = string.Format("{0:d9}", Math.Abs(macro.mcr_val)); if (0 < macro.dec_val) StrContaPezzip1 = StrContaPezzip1.Insert(9 - macro.dec_val, "."); if (macro.mcr_val < 0) StrContaPezzip1 = "-" + StrContaPezzip1; int index = StrContaPezzip1.IndexOf("."); if (index >= 0) StrContaPezzip1 = StrContaPezzip1.Substring(0, index); Console.WriteLine("{0}", StrContaPezzip1); } else { Console.WriteLine("ERR-READ-MACRO**********"); } } } catch { StrContaPezzip1 = "Errore Contapezzi"; } return pieces_ret; } private void DownloadStd(short cnc_path = 1) { var PROG_MEMORY_PATH = get_cnc_path(cnc_path); int num; int len; string program = "%\n\n%"; if (CONNECTED) { ret_array[9] = Focas1.cnc_pdf_del(hndl, PROG_MEMORY_PATH + "OSTD"); try { ret_array[9] = Focas_StartNCFileDownload(PROG_MEMORY_PATH, FanucProgram.FILE_TYPE.CNC_PROGRAM); if (ret_array[9] != Focas1.EW_OK) { Focas_EndNCFileDownload(); ret_array[9] = ret_array[9]; CheckErrors(ret_array); return; } len = program.Length; int startPos = 0; while (len > 0) { if (len >= 1024) { num = 1024; } else { num = len; } char[] prg_temp = new char[num]; program.CopyTo(startPos, prg_temp, 0, num); ret_array[9] = Focas_NCFileDownload(ref num, prg_temp); if (ret_array[9] == (short)Focas1.focas_ret.EW_BUFFER) { Thread.Sleep(100); continue; } if (ret_array[9] == Focas1.EW_OK) { startPos += num; len -= num; } if (ret_array[9] != Focas1.EW_OK) { Focas1.ODBERR odd = new Focas1.ODBERR(); Focas1.cnc_getdtailerr(hndl, odd); Debug.WriteLine(odd.err_no + " " + odd.err_dtno); break; } } if (ret_array[9] != Focas1.EW_OK) { Focas_EndNCFileDownload(); CheckErrors(ret_array); return; } Focas_EndNCFileDownload(); Focas1.ODBERR odd2 = new Focas1.ODBERR(); Focas1.cnc_getdtailerr(hndl, odd2); Debug.WriteLine(odd2.err_no + " " + odd2.err_dtno); } catch { CheckErrors(ret_array); return; } ret_array[9] = Focas1.cnc_pdf_slctmain(hndl, PROG_MEMORY_PATH + "OSTD"); CheckErrors(ret_array); } } private bool canDownloadNC() { ret_array[9] = Focas1.cnc_statinfo(hndl, odbst); if (ret_array[9] != Focas1.EW_OK) { CheckErrors(ret_array); return false; } else if (odbst.aut != 0 //MDI STATE && odbst.aut != 3) //EDIT STATE { ret_array[9] = (short)Focas1.focas_ret.EW_MODE; CheckErrors(ret_array); return false; } return true; } /** * SEND PROGRAM TO CNC MEMORY */ public short Download(string file, FanucProgram.FILE_TYPE type, short cnc_path = 1) { if (!canDownloadNC()) return ret_array[9]; Focas1.cnc_setpath(hndl, cnc_path); var PROG_MEMORY_PATH = get_cnc_path(cnc_path); int num; int len; string prog_name = ""; if (CONNECTED) { if (type == FanucProgram.FILE_TYPE.CNC_PROGRAM) { try { prog_name = FanucProgram.GetProgramTitle(file); } catch (Exception e) { FOCAS_ERRORS = "tentativo Download fallito nome file in formato errato\n" + e.StackTrace; return (short)Focas1.focas_ret.EW_DATA; } if (!compatibiltyMode) { ret_array[9] = Focas1.cnc_pdf_slctmain(hndl, PROG_MEMORY_PATH + "OSTD"); if (ret_array[9] != Focas1.EW_OK) DownloadStd(); } ret_array[9] = Focas_DeleteNCProgram(prog_name, PROG_MEMORY_PATH); if (ret_array[9] != Focas1.EW_OK && ret_array[9] != (short)Focas1.focas_ret.EW_BUSY && ret_array[9] != (short)Focas1.focas_ret.EW_DATA) { CheckErrors(ret_array); } } try { ret_array[9] = Focas_StartNCFileDownload(PROG_MEMORY_PATH, type); if (ret_array[9] != Focas1.EW_OK) { Focas_EndNCFileDownload(); CheckErrors(ret_array); return ret_array[9]; } len = file.Length; int startPos = 0; while (len > 0) { if (len >= 1024) { num = 1024; } else { num = len; } char[] prg_temp = new char[num]; file.CopyTo(startPos, prg_temp, 0, num); string a = file.Substring(startPos); ret_array[9] = Focas_NCFileDownload(ref num, prg_temp); if (ret_array[9] == (short)Focas1.focas_ret.EW_BUFFER) { Thread.Sleep(100); continue; } if (ret_array[9] == Focas1.EW_OK) { startPos += num; len -= num; } if (ret_array[9] != Focas1.EW_OK) { Focas1.ODBERR odd = new Focas1.ODBERR(); Focas1.cnc_getdtailerr(hndl, odd); Debug.WriteLine(odd.err_no + " " + odd.err_dtno); break; } } if (ret_array[9] != Focas1.EW_OK) { CheckErrors(ret_array); Focas_EndNCFileDownload(); return ret_array[9]; } Focas_EndNCFileDownload(); } catch { CheckErrors(ret_array); Focas_EndNCFileDownload(); return ret_array[9]; } if (!compatibiltyMode && type == FanucProgram.FILE_TYPE.CNC_PROGRAM) ret_array[9] = Focas1.cnc_pdf_slctmain(hndl, PROG_MEMORY_PATH + prog_name); if (ret_array[9] != Focas1.EW_OK) { CheckErrors(ret_array); return 18; } return Focas1.EW_OK; } return (short)Focas1.focas_ret.EW_BUSY; } private short Focas_DeleteNCProgram(string prog_name, string prog_path) { Regex rx = new Regex(@"(\b[O]+\d{4}\b)|(\b\d{4}\b)"); //cerca per O#### o #### dove # è un carattere numerico // Find matches. MatchCollection matches = rx.Matches(prog_name); if (matches.Count > 0) { prog_name = prog_name.Trim(new Char[] { 'O' }); return Focas1.cnc_delete(hndl, (short)Convert.ToUInt32(prog_name)); } else { return Focas1.cnc_pdf_del(hndl, prog_path + prog_name); } } private short Focas_StartNCFileDownload(string file_path, FanucProgram.FILE_TYPE type) { //il path del file non può essere selezionato se compatibilityMode = true if (compatibiltyMode) return Focas1.cnc_dwnstart3(hndl, (short)type); else return Focas1.cnc_dwnstart4(hndl, (short)type, file_path); } private short Focas_NCFileDownload(ref int program_lenght, char[] program) { if (compatibiltyMode) return Focas1.cnc_download3(hndl, ref program_lenght, program); else return Focas1.cnc_download4(hndl, ref program_lenght, program); } private short Focas_EndNCFileDownload() { if (compatibiltyMode) return Focas1.cnc_dwnend3(hndl); else return Focas1.cnc_dwnend4(hndl); } private short Focas_StartNCFileUpload(string file_path, FanucProgram.FILE_TYPE type) { //il file_path cambia a seconda di compatibilityMode if (compatibiltyMode) { try { //file_path deve essere il codice del programma e.g. O1234 --> file_path = 1234 int file_code = Convert.ToInt32(file_path); return Focas1.cnc_upstart3(hndl, (short)type, file_code, file_code); } catch { return (short)Focas1.focas_ret.EW_DATA; } } else //file_path deve essere il path del programma compreso il nome file e.g. "//CNC_MEM/USER/PATH1/SAMPLE" dove SAMPLE è il nome del programma return Focas1.cnc_upstart4(hndl, (short)type, file_path); } private short Focas_NCFileUpload(int program_lenght, char[] program) { //il path del file non può essere selezionato se compatibilityMode = true if (compatibiltyMode) return Focas1.cnc_upload4(hndl, ref program_lenght, program); else return Focas1.cnc_upload4(hndl, ref program_lenght, program); } private short Focas_EndNCFileUpload() { //il path del file non può essere selezionato se compatibilityMode = true if (compatibiltyMode) return Focas1.cnc_upend3(hndl); else return Focas1.cnc_upend4(hndl); } private string Focas_GetMainForNCDownloadUpload() { if (compatibiltyMode) { Focas1.ODBPRO buf = new Focas1.ODBPRO(); ret_array[10] = Focas1.cnc_rdprgnum(hndl, buf); CheckErrors(ret_array); //buf.data->programma in esecuzione //buf.mdata-> main program return buf.mdata.ToString(); } else { byte[] nome_main = new byte[242]; // nome programma ret_array[10] = Focas1.cnc_pdf_rdmain(hndl, nome_main); string path = ASCIIEncoding.ASCII.GetString(nome_main).TrimEnd('\0'); CheckErrors(ret_array); return path; } } /** * GET MAIN PROGRAM FROM CNC MEMORY */ public string Upload(FanucProgram.FILE_TYPE type, short cnc_path = 1) { Focas1.cnc_setpath(hndl, cnc_path); //file_path = PROG_MEMORY_PATH //file_name = "SAMPLE"; int BUFFER_SIZE = 10000;//dimensione del buffer di caratteri. Solitamente scarica meno di 1000 caratteri al ciclo char[] buffer = new char[BUFFER_SIZE + 1]; string buf; int len; string output = ""; char[] path = new char[244]; if (CONNECTED == false) return ""; else { ret_array[10] = Focas_StartNCFileUpload(Focas_GetMainForNCDownloadUpload(), type); if (ret_array[10] != Focas1.EW_OK) { Focas_EndNCFileUpload(); CheckErrors(ret_array); return ""; } do { len = BUFFER_SIZE; ret_array[10] = Focas_NCFileUpload(len, buffer); if (ret_array[10] == (short)Focas1.focas_ret.EW_BUFFER) { Thread.Sleep(100); continue; } if (ret_array[10] == Focas1.EW_OK) { buffer[len] = '\0'; ; buf = new string(buffer).TrimEnd('\0'); output = output + buf; } if (buffer[len - 1] == '%') { break; } } while ((ret_array[10] == Focas1.EW_OK) || (ret_array[10] == (short)Focas1.focas_ret.EW_BUFFER)); ret_array[10] = Focas_EndNCFileUpload(); return output; } } } }