Skip to main content

Focas Lib Note

機台連線基本流程

先跟機台取得交握碼 透過交握碼跟機台互動 將交握碼釋放

因為一台focas機台僅有5個交握碼可以使用,若沒釋放佔滿五個機台則無法進行連線

一台控制器不只五個交握碼可以使用

If an application program terminates without releasing the library handle number, the memory area assigned for the management of the handle remains allocated and accumulates. If it is repeated, the library handle number will become unable to be acquired at last.

但還是需要記得將交握碼釋放,才不會讓其他程式抓不到他的交握碼 但如果同一台電腦太常取得/釋放交握碼 要注意Outbound Connections的最大數量,因為當我們釋放交握碼時,PC的TCP連線會處於time_wait的狀態,需要等待電腦將他釋放,所以如果一時發起太多的request,就會讓PC自己的port被佔滿,然後程式就會壞掉

概略說明

取得交握碼

透過ip/port與機台取得交握碼

# variable
ip: 127.0.0.1(或是domain.com)
port: 8193
timeout: 3
handleCode: 交握碼
ret: 是否正常執行的回應碼

ret = cnc_allclibhndl3(ip,port,3,out handleCode)

釋放交握碼

取得交握碼後一定要記得釋放交握碼

ret = cnc_freelibhndl(handleCode)

取得警報資訊

在取得警報訊息前,官方建議要先依照下列的流程來取得警報訊息

  1. Stop sampling. This must be done. (by cnc_stopophis)
  2. Read history data count.(by cnc_rdophisno, cnc_rdalmhisno functions)
  3. Read history data.(by cnc_rdophistry, cnc_rdophistry2, cnc_rdophistry4, cnc_rdalmhistry, cnc_rdalmhistry2, cnc_rdalmhistry3, cnc_rdalmhistry5 functions)
  4. Restart sampling. This must be done. (by cnc_startophis function)

暫停機台擷取資料(cnc_stopophis)

cnc_stopophis(handleCode)

https://www.inventcom.net/fanuc-focas-library/History/cnc_stopophis

取得警報在機台的最新的alarm的位置

# variable
historyCountForNewestAlarm

# code
cnc_rdalmhisno(handleCode, out historyCountForNewestAlarm)

取得警報詳細資料(cnc_rdalmhistry5)

https://www.inventcom.net/fanuc-focas-library/history/cnc_rdalmhistry5

在這種機台型號下,得透過這個function跟機台進行連線

# variable
StartHistoryCountForAlarm
EndHistoryCountForAlarm
DataBlockLength
ODBAHIS5_data
ret: function執行成功或錯誤碼

ret = cnc_rdalmhistry5(handleCode,StartHistoryCountForAlarm,EndHistoryCountForAlarm,DataBlockLength,out ODBAHIS5_data)

HistoryCountForAlarm要用 cnc_rdalmhisno取得相對位置 比方cnc_rdalmhisno取得的值為50 則50為最新的內容,49為歷史第二筆資料,以此類推,而在這型號的機台,僅能儲存歷史10筆的alarm,所以這個數字最小只能是41

另外,關於DataBlockLength,在focas的文件說明提到

他回傳回來的資料的組成

1-2會是取得的開始history的count位置

3-4會是取的的最後history的count位置

接下來每一個警報歷史會是516為一組來進行循環

所以如果我們希望取得一組alarm history,則DataBlockLength的值為

4+516*1

如果兩組則為

4+516*2

所以如果要取得最新的警報訊息

ret = cnc_rdalmhistry5(handleCode,50,50, 4+516*1,out ODBAHIS5_data)

取得的資料會放在ODBAHIS5_data中

alarm的詳細資料會在

ODBAHIS5_data.alm_his.data1

取得不是最新的警報資料

在上述的說明中,如果我們需要取得最新兩筆警報歷史

ret = cnc_rdalmhistry5(handleCode,49,50, 4+516*2,out ODBAHIS5_data)

應該是要這樣存取的

但這個在我目前的lib中,他的解析方式會直接將取得回來的資料群組直接用520來進行資料組合

取得的資料如果為

bytesmean
1-2start history count
3-4end history count
5-520history data 1
521-1036history data 2

但在focas的dll解析時,會直接將history data的寬度當成520直接來解析,因此在解析的資料就會有位置上的誤差

bytesmeandll 解析
1-2start history count1-2
3-4end history count3-4
5-520history data 15-520
521-1036history data 2525-1040

就會導致取得的資料從第二筆之後就會不正確 因此如果需要取得多筆歷史記錄,會建議直接多次執行cnc_rdalmhistry5,並修改取得的位置

ret = cnc_rdalmhistry5(handleCode,50,50, 4+516*1,out ODBAHIS5_data)
ret = cnc_rdalmhistry5(handleCode,49,49, 4+516*1,out ODBAHIS5_data)

並將這兩筆的ODBAHIS5_data的alm_his.data1個別取出 就可以取得歷史的資料了

Reference

https://www.inventcom.net/fanuc-focas-library/general/fwlib32