Fanuc Focas1/2 in C# - 檔案讀寫

 

 

fanuc有提供幾種檔案下載...,不是很夠用啊~~~~~!

而且還要注意%

下載回來的檔案頭尾都有%

但是上傳時,頭不可有%但必須視\n、尾必須是%

 

 

        //(CNC ← PC) 讀取檔案

        public int ReadFile(short func, out List<String> list)

     {

            /*

            檔案 Type: (此範例不適用 Type 0)

            0 : NC program

            1 : Tool offset data

            2 : Parameter

            3 : Pitch error compensation data

            4 : Custom macro variables

            5 : Work zero offset data

            18 : Rotary table dynamic fixture offset

            */

 

            list = new List<string>();

 

            //設定Buf 大小

            char[] buf=new char[1025];

            int len=1024;

           

            //Focas2回傳值

            short ret;

            //資料處理站存用

            String tmp="";

 

            //下載開始,設定類型

            ret=Focas1.cnc_upstart4(FFlibHndl, func, "") ;

            //開始失敗則回傳結果

            //(-1)EW_BUSY:busy

            //(5)EW_DATA:error

            //(4)EW_ATTRIB:error

            //(6)EW_NOOPT:No option

            if(ret!=Focas1.EW_OK) return ret;

 

            //收資料

            do

            {

                len=1024;

                buf = new char[1024];

                //取得部分資料

                ret=Focas1.cnc_upload4(FFlibHndl, ref len, buf);

                //(-2)EW_RESET

                //(1)EW_FUNC

                //(2)EW_LENGTH

                //(7)EW_PROT

                //(10)EW_BUFFER

                //判斷Buf 內為空的

                if (ret == (short)Focas1.focas_ret.EW_BUFFER)

                {

                    //重試

                    continue;

                }

                //取得資料成功

                if(ret==Focas1.EW_OK)

                {

                    //收到的資料可能會有一半的,所以要用舊資料+新資料

                    String data=new String(buf,0,len);

                    tmp += data;

                }

                else

                {

                    //其他錯誤

                    return ret;

                }

 

                //結束

                if(buf[len-1]=='%')

                {

                    break;

                }

            }while((ret==Focas1.EW_OK)||(ret==(short)Focas1.focas_ret.EW_BUFFER));

            //關閉傳輸

            ret=Focas1.cnc_upend4(FFlibHndl);

 

            //2019.02.11  update

  list = str.Split('\n').ToList<string>();
for (int i = 0; i < list.Count; i++)
{
                list[i] = list[i].Trim();
}

 

            return ret;

        }

 

 

//(CNC ← PC) 讀取檔案

        public int WriteFile(short func, List<String> list)

        {

            /*

            檔案 Type: (此範例不適用 Type 0)

            0 : NC program

            1 : Tool offset data

            2 : Parameter

            3 : Pitch error compensation data

            4 : Custom macro variables

            5 : Work zero offset data

            18 : Rotary table dynamic fixture offset

            */

 

            int len;

            short ret;

            ret=Focas1.cnc_dwnstart4(FFlibHndl, func, "");

            if(ret!=Focas1.EW_OK) return ret;

 

            int i=0;

            String Cmd;

            while(i<list.Count)

            {

                Cmd=list[i]+"\n";

 

                len=Cmd.Length;

                ret=Focas1.cnc_download4(FFlibHndl, ref len, Cmd);

                if (ret == (short)Focas1.focas_ret.EW_BUFFER) continue;

                if(ret!=Focas1.EW_OK) break;

 

                i++;

            }

 

            ret=Focas1.cnc_dwnend4(FFlibHndl);

            return ret;

 

        }

 

 

Ex:

        private void test()

        {

            List<String> list = new List<String>();

            list.Add("");

            //磨耗

            for (int i = 0; i < 128; i++)

            {

                list.Add("G10P" + (i + 1).ToString() + "X0.0000Z0.0000R0.0000Q0Y0.0000");

            }

            //形狀

            for (int i = 0; i < 128; i++)

            {

                list.Add("G10P" + (i + 10001).ToString() + "X0.0000Z0.0000R0.0000Y0.0000");

            }

            list.Add("%");

            int ret = WriteFile(1, list);

            if (ret != Focas1.EW_OK)

            {

                MessageBox.Show("補正寫入失敗。");

            }

        }

 

 

相關文章:

文章標籤
全站熱搜
創作者介紹
創作者 史克威爾凱特 的頭像
史克威爾凱特

史克威爾凱特的部落格

史克威爾凱特 發表在 痞客邦 留言(2) 人氣(2,353)