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

发布时间 2023-08-04 09:03:33作者: 一杯清酒邀明月

Qt实现TwinCAT通讯

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

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

https://infosys.beckhoff.com/english.php?content=../content/1033/tcadsdll2/12444708363.html&id=1786290812669485475

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

我所使用的环境是Qt5.12.3+VS2017+Win7,安装的TwinCAT是TC31-Full-Setup.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 {
 2   QString targetNetId = "169.254.71.20.1.1";
 3   int targetPort = 851;
 4   
 5   //这里的hostNetId是我写错了,感谢评论区 用户“Lee轮回” 的指出。谢谢。
 6   //AmsAddr targetAddr = createAddr(hostNetId, targetPort);
 7   
 8   AmsAddr targetAddr = createAddr(targetNetId, targetPort);
 9   
10   unsigned short data = 0; //用来存放数据的缓冲区
11   qDebug() << AdsSyncWriteReq(&targetAddr, 0x00004020, 512028, 2, &data);
12   qDebug() << AdsSyncReadReq(&targetAddr, 0x00004020, 512028, 2, &data) << data;
13 }
14 //这个createAddr是自己定义的函数,作用是把字符串初始化adsapi所使用的AmsAddr
15 AmsAddr createAddr(QString netId, int port)
16 {
17     AmsAddr addr;
18     addr.port = port;
19 
20     QStringList ids = netId.split(".");
21     for(int i = 0; i < 6; i++)
22     {
23         addr.netId.b[i] = ids[i].toUInt();
24     }
25 
26     return  addr;
27 }

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