You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

#include "OPCUAClient.h"
#include <iostream>
#include <unistd.h>
int main(int argc, char* argv[]) {
OPCUAClient opcClient;
if (argc < 3 || argc > 4) {
std::cerr
<< "Usage: " << argv[0]
<< " <Server-URL> <Node-ID> [Interval-in-seconds]\n\n"
<< "Example:\n"
<< argv[0]
<< " opc.tcp://192.168.1.100:4840 \"ns=2;i=2\" 1\n";
return 1;
}
const std::string endpoint = argv[1];
const std::string nodeText = argv[2];
const unsigned int interval =
(argc == 4) ? std::strtoul(argv[3], nullptr, 10) : 0;
// 1. Connect
std::cout << "Connecting to server..." << std::endl;
if (!opcClient.Connect(endpoint)) {
return -1;
}
// 2. Read Node Value
uint16_t nsIndex = std::stoi(nodeText.substr(nodeText.find("ns=") + 3, nodeText.find(";") - (nodeText.find("ns=") + 3)));
std::size_t sPos = nodeText.find(";s=");
std::string stringNodeId;
uint32_t numericNodeId;
std::string value;
if (sPos != std::string::npos) stringNodeId = nodeText.substr(sPos + 3);
else numericNodeId = std::stoi(nodeText.substr(nodeText.find("i=") + 2));
do {
if (sPos != std::string::npos) value = opcClient.ReadString(nsIndex, stringNodeId);
else value = opcClient.ReadNumeric(nsIndex, numericNodeId);
std::cout << "Value: " << value << std::endl;
if (interval > 0) usleep(interval * 1000000);
} while (interval > 0);
// 4. Close
std::cout << "\nClosing connection." << std::endl;
opcClient.Close();
return 0;
}