Beckhoff / ADS

Beckhoff protocol to communicate with TwinCAT devices.
MIT License
491 stars 193 forks source link

Passing IP from terminal #219

Open abramrace opened 7 months ago

abramrace commented 7 months ago

Let's say we want to pass the PLC IP from terminal to C++ code. Consider this code:

#include <ros/ros.h>
#include <rospack#include <std_msgs/String.h>
#include <AdsLib/AdsLib.h>
#include "AdsLib/AdsNotificationOOI.h"
#include "AdsLib/AdsVariable.h"

#include <array>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <unistd.h>

class BeckhoffClass
{

public:

    cv_bridge::CvImagePtr cvImageSubscribePtr;

    int imgWidth, imgHeight;
    int imgPixels;
    int rosRate;

    std::string plcNetId, plcIp, localNetId;

    AdsDevice* route = nullptr;
    bool AdsConenction = false;

    BeckhoffClass()
        : private_nh("~"),
          it_(nh_)
    {
        // 
    }

    ~BeckhoffClass()
    {

    }

    void connectTwinCatAds()
    {
        try 
        {

            static const AmsNetId remoteNetId { 169, 254, 94, 16, 1, 1};
            static const char remoteIpV4[] = "10.2.2.157";
            AdsSetLocalAddress({10, 2, 2, 128, 1, 1});

            route = new AdsDevice(remoteIpV4, remoteNetId, AMSPORT_R0_PLC_TC3);
            AdsConenction = true;
            std::cout << "Asd connected! \n";
        }
        catch (const AdsException& ex) 
        {
            std::cout << "Error: " << ex.errorCode << "\n";
            std::cout << "AdsException message: " << ex.what() << "\n";
        } 
        catch (const std::runtime_error& ex) 
        {
            std::cout << ex.what() << '\n';
        }
    }

};

int main(int argc, char** argv)
{

    std::string plcIp = arv[1];

    BeckhoffClass beckhoff_class(plcIp);

    return 0;
}

How can I initialize ADS struct inside Beckhoff class constructor?