Linux与倍福ADS通信

倍福虚拟学院:C++ ADS 通讯

在这里插入图片描述

倍福提供了官方的GitHub支持,阅读后发现支持还是很全面的

https://github.com/Beckhoff/ADS

在这里插入图片描述

用adstool进行一些简单的测试,可以获取netid,但是获取其它信息时则会报错

redwall@redwall-G3-3500:~/ADS/build$ ./adstool 169.254.254.142 netid
169.254.142.16.1.1

redwall@redwall-G3-3500:~/ADS/build$ ./adstool 169.254.254.142.1.1 license systemid
2022-09-27T17:24:59+0800 Error: AdsException message: Ads operation failed with error code 6.

按照下边的博客进行简单的通讯测试

linux系统的ROS与倍福PLC之间利用ADS实现通讯

这里会涉及到官方提供的ADS库的编译与链接,CMakeLists.txt如下

cmake_minimum_required(VERSION 2.8)
project(ads_test)

set(CMAKE_CXX_STANDARD "14")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/AdsLib)

#添加ADS库
set(SOURCES
  AdsLib/AdsDef.cpp
  AdsLib/AdsDevice.cpp
  AdsLib/AdsFile.cpp
  AdsLib/AdsLib.cpp
  AdsLib/Frame.cpp
  AdsLib/LicenseAccess.cpp
  AdsLib/Log.cpp
  AdsLib/RouterAccess.cpp
  AdsLib/RTimeAccess.cpp
  AdsLib/Sockets.cpp

  AdsLib/standalone/AdsLib.cpp
  AdsLib/standalone/AmsConnection.cpp
  AdsLib/standalone/AmsNetId.cpp
  AdsLib/standalone/AmsPort.cpp
  AdsLib/standalone/AmsRouter.cpp
  AdsLib/standalone/NotificationDispatcher.cpp
)

add_library(ads ${SOURCES})

target_include_directories(ads PUBLIC ./AdsLib)

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  target_link_libraries(ads PUBLIC wsock32)
endif()

if(WIN32 EQUAL 1)
    target_link_libraries(ads PUBLIC ws2_32)
endif()

#添加测试程序
add_executable(link_test src/link_test.cpp)
target_link_libraries(link_test ads)

link_test.cpp

#include "AdsLib.h"
#include "AdsNotification.h"
#include "AdsVariable.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    static const AmsNetId remoteNetID{169, 254, 254, 142, 1, 1};
    static const string remoteIPV4 = "169.254.254.142";

    AdsDevice route{remoteIPV4, remoteNetID, AMSPORT_R0_PLC_TC3};

    AdsVariable<int> var_to_write{route, "MAIN.test_var"};
    cout << "This is" << var_to_write << endl;
    var_to_write = 10;

    return 0;
}

但可能是一些细节没有注意到,没有成功

redwall@redwall-G3-3500:~/Test/ADS_test/bin$ ./link_test 
terminate called after throwing an instance of 'AdsException'
  what():  Ads operation failed with error code 1861.
已放弃 (核心已转储)

上位机使用C++通过ADS协议与倍福PLC通信例程-布尔变量的读取

关于倍福EtherCAT3的ADS通讯


把下面的博客又稍微仔细得看了一遍

TwinCAT3与ROS之间的ADS通信实现

又对倍福ADS通信做了简单了解

倍福EtherCAT的ADS通讯

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

我将倍福与编程PC的AdsAmsNetId均设置为与其IPV4地址相对应

//倍福
169.254.254.142
169.254.254.142.1.1

//编程PC
169.254.254.100
169.254.254.100.1.1

//Ubuntu
169.254.254.88
169.254.254.88.1.1

这里用了一个交换机,交换机的供电比较粗糙,后面要加一个专门的头

看博客中说要在TwinCAT中设置静态路由,这一步卡了好久,远程登录Windows添加总是报错

倍福TwinCAT3 Ads错误快查

倍福TwinCAT无法添加ADS路由的排查方法

在这里插入图片描述
虽然能大概知道错误原因,但也不知道解决方法

最后用官方提供的adstool成功添加

redwall@redwall-G3-3500:~/ADS/build$ ./adstool 169.254.254.142 addroute --addr=169.254.254.88 --netid=169.254.254.88.1.1 --password=1 --routename=DELL_G3

在这里插入图片描述
link_test.cpp简单修改,打印通信信息,获取电机使能布尔量状态并反转

#include "AdsLib.h"
#include "AdsNotification.h"
#include "AdsVariable.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    static const AmsNetId remoteNetID{169, 254, 254, 142, 1, 1};
    static const string remoteIPV4 = "169.254.254.142";

    AdsDevice route{remoteIPV4, remoteNetID, AMSPORT_R0_PLC_TC3};
    DeviceInfo routeInfo = route.GetDeviceInfo();
    cout << "Device name:" << routeInfo.name << endl;
    cout << "ADS version:" << routeInfo.version.version << endl;
    cout << "Local port:" << route.GetLocalPort() << endl;
    AdsDeviceState routeState = route.GetState();
    cout << "ADS state:" << routeState.ads << endl;
    cout << "Device state:" << routeState.device << endl;

    AdsVariable<bool> motor_enable{route, "MAIN.power_do"};
    AdsVariable<int> int_var{route, "MAIN.int_var"};
    // int a;
    // int_var.Read(sizeof(a), &a);
    // cout << a << endl;

    while (true)
    {
        if (!motor_enable)
        {
            cout << "Motor enabled!" << endl;
        }
        else
        {
            cout << "Motor disabled!" << endl;
        }
        motor_enable = !motor_enable;
        sleep(5);
    }

    return 0;
}
redwall@redwall-G3-3500:~/Test/ADS_test/bin$ ./link_test 
Device name:Plc30 App
ADS version:
Local port:30000
ADS state:5
Device state:0
Motor enabled!
Motor disabled!
Motor enabled!
Motor disabled!
Motor enabled!
Motor disabled!
Motor enabled!
Motor disabled!

可以在编程PC中观察到值的变化,或者直接触摸判断电机是否使能

在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prejudices

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值