Qt 调用Beckhoff倍福TwinCAT通讯模块(TcAdsDll)


  目前这种方式是通过调用TwinCAT提供的AdsApi与倍福PLC通讯的。要求本机安装TwinCAT(无需作为主机,但是可能这个api依赖TwinCAT的一些服务)。

  关于AdsApi的官方资料请看这里,有函数的详细解释,还有例子。你值得拥有。

  infosys.beckhoff/english.php?content=../content/1033/tcadsdll2/124447083&id=1786290812669485475

  用ads与TwinCAT通讯时,需要注意各自的数据类型的区别。

  我所使用的环境是Qt5.12.3+VS2017+Win7,安装的TwinCAT是TC31FullSetup.3.1.4022.16.exe;但是那个AdsApi提供的库是c语言的,所以只要是支持C语言的编译环境应该都可以按照类似的方法调用。

  这里进行通讯的前提是,你已经有了一个TwinCAT的主机(安装了TwinCAT的电脑,或者是倍福PLC),这个主机就是你要与之通讯的对象。

1.首先要把主机添加到你本机的TwinCAT设备列表中。添加的步骤查看下面的截图。

2.然后就是把TwinCAT提供的AdsApi的库包含到Qt工程中(具体路径要看你的TwinCAT的安装目录)。

1 INCLUDEPATH += F:\TwinCAT\AdsApi\TcAdsDll\Include 2 LIBS += F:\TwinCAT\AdsApi\TcAdsDll\x64\lib\TcAdsDll.lib

3.接着include头文件
这里要特别说明一下,要include三个头文件,如下

1 #include <Windows.h> 2 #include <TcAdsDef.h> 3 #include <TcAdsAPI.h>

且include这三个头文件的顺序必须如此,因为下面那两个头文件用到BOOL类型,而BOOL类型是在Windos.h中定义的;TcAdsAPI.h中的一些类型是在TcAdsDef.h中定义的。
4.接下来就是编程了。
目前我这边主要测试了三个功能:读、写、监听。
分别对应这三个函数(这三个函数的声明都在#include <TcAdsAPI.h>中):

1 long AdsSyncWriteReq(AmsAddr* pServerAddr, // Ams address of ADS server 2 unsigned long indexGroup, // index group in ADS server interface 3 unsigned long indexOffset, // index offset in ADS server interface 4 unsigned long length, // count of bytes to write 5 void* pData // pointer to the client buffer 6 ); 7 8 long AdsSyncReadReq(AmsAddr* pAddr, // Ams address of ADS server 9 unsigned long indexGroup, // index group in ADS server interface 10 unsigned long indexOffset, // index offset in ADS server interface 11 unsigned long length, // count of bytes to read 12 void* pData // pointer to the client buffer 13 ); 14 __declspec( dllexport ) 15 long AdsSyncAddDeviceNotificationReq(AmsAddr* pAddr, // Ams address of ADS server 16 unsigned long indexGroup, // index group in ADS server interface 17 unsigned long indexOffset,// index offset in ADS server interface 18 AdsNotificationAttrib* pNoteAttrib, // attributes of notification request 19 PAdsNotificationFuncEx pNoteFunc, // address of notification callback 20 unsigned long hUser, // user handle 21 unsigned long *pNotification // pointer to notification handle (return value) 22 );

这几个函数中的indexGroup的意思可以查看文章开始时提供的那个连接,里面有介绍到。

我这边要读取和写入的对象是存放在M寄存器中的。所以我的indexGroup = 0x00004020;变量的地址按照下图所示查看:

所以地址偏移(indexOffset)是512028

1 14 //这个createAddr是自己定义的函数,作用是把字符串初始化adsapi所使用的AmsAddr 15 AmsAddr createAddr(QString netId, int port) 16 25 26 return addr; 27 }

这样子就实现了简单的读写。
监听的有空再详细介绍。



上一篇:Beckhoff EL7031模块参数设置

下一篇:Mitsubishi 三菱FXPLC 常用基本编程环节


Qt 倍福(Beckhoff)
Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图