DiscoveryΒΆ
EtherNet/IP allows to discover EIP devices in the network by using UDP broadcast messages.
EIPScanner provides DiscoveryManager class for this purpose:
//
// Created by Aleksey Timin on 12/17/19.
//
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <winsock2.h>
#define OS_Windows (1)
#endif
#include <DiscoveryManager.h>
#include <utils/Logger.h>
using eipScanner::DiscoveryManager;
using eipScanner::utils::Logger;
using eipScanner::utils::LogLevel;
int main() {
Logger::setLogLevel(LogLevel::DEBUG);
#if OS_Windows
WSADATA wsaData;
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (winsockStart != 0) {
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
return EXIT_FAILURE;
}
#endif
DiscoveryManager discoveryManager("172.28.255.255", 0xAF12, std::chrono::seconds(1));
auto devices = discoveryManager.discover();
for (auto& device : devices) {
Logger(LogLevel::INFO) << "Discovered device: "
<< device.identityObject.getProductName()
<< " with address " << device.socketAddress.toString();
}
#if OS_Windows
WSACleanup();
#endif
return EXIT_SUCCESS;
}
Method DiscoveryManager::discover sends broadcast UDP request in the network and waits for the responses from the devices. It returns a vector of structures for each discovered device that contain the IP addresses, ports and identity_objects. If there is no device in the network it returns an empty vector.