为了这个Fanuc(发那科)数控机床数据的采集也花费了不少精力,先是去供应商那里了解,基本都是代理商,没有技术支持。
在网上也有关于Fanuc的以太网Ethernet连接文档,那里面有说明,大概是开发需要SDK(focas2),知道这点就是进步很大,就在淘宝上买了SDK,但是打开才发现里面的接口众多,光看这接口文档就花了不少时间,终于找到了关于网络通信的两个重要接口,打开连接
public static extern short cnc_allclibhndl3([In, MarshalAs(UnmanagedType.AsAny)] Object ip, ushort port, int timeout, out ushort FlibHndl);
关闭连接 public static extern short cnc_resetconnect(ushort FlibHndl); 这一步很重要,但是后面的就难了。
最基本的,我想知道Fanuc当前已完成的工件数,要想取工件计数就不知道调用哪个接口了,大海捞针。几乎我把所有和read相关的接口都试了一遍,和当前机台面板上的工件计数比较都不对。这深层次的计数问题,找代理商根本没用,他们要不是电话不通,就是“我也不了解”、“我不懂开发”。
最后发现,发那科的C#开发包只用到了 Fwlib32.dll 和 fwlibe1.dll 是关于以太网通信的。
调用的接口 public static extern short cnc_rdmacro(ushort FlibHndl, short a, short b, [Out, MarshalAs(UnmanagedType.LPStruct)] ODBM c); 也就是读取Fanuc里面的宏变量的值。
具体代码C#:
1 private bool ConnectFanuc(string ip, ref ushort handler, ushort port = 10000)
2 {
3 try
4 {
5 short result = Focas1.cnc_allclibhndl3(ip, port, 3, out handler);
6 return result == 0;
7 }
8 catch (Exception err)
9 {
10 _logger.Error(ip, err);
11 return false;
12 }
13 }
14
15 private void CloseFanuc(ushort handler)
16 {
17 try
18 {
19 short result = Focas1.cnc_resetconnect(handler);
20 if (result != 0)
21 {
22 _logger.Error("Fanuc关闭连接异常");
23 }
24 }
25 catch (Exception err)
26 {
27 _logger.Error("Fanuc关闭连接", err);
28 }
29 }
30
31 private int GetFanucData(ushort handler)
32 {
33 try
34 {
35 Focas1.ODBM result = new Focas1.ODBM();
36 short r = Focas1.cnc_rdmacro(handler, 0xF3D, 0xA, result);
37 var qty = result.mcr_val.ToString().Substring(0, result.mcr_val.ToString().Length - result.dec_val);
38 return Convert.ToInt32(qty);
39 }
40 catch (Exception err)
41 {
42 _logger.Error(err);
43 return -1;
44 }
45 }
来源:http://www.cnblogs.com/jonney-wang/p/6253437.html