Linux与倍福ADS通信

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

在这里插入图片描述

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

https://github.com/Beckhoff/ADS

Linux与倍福ADS通信_第1张图片

用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 
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通讯


Linux与倍福ADS通信_第2张图片
Linux与倍福ADS通信_第3张图片

我将倍福与编程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路由的排查方法

Linux与倍福ADS通信_第4张图片
虽然能大概知道错误原因,但也不知道解决方法

最后用官方提供的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

Linux与倍福ADS通信_第5张图片
link_test.cpp简单修改,打印通信信息,获取电机使能布尔量状态并反转

#include "AdsLib.h"
#include "AdsNotification.h"
#include "AdsVariable.h"
#include 
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中观察到值的变化,或者直接触摸判断电机是否使能

Linux与倍福ADS通信_第6张图片
Linux与倍福ADS通信_第7张图片

你可能感兴趣的:(倍福,linux,c++,运维)