Skip to content

Commit a7e88cf

Browse files
authored
Merge pull request #49 from nimbuscontrols/issue-45-add-msvc-support
Add winsock startup-cleanup to examples and link ws2_32
2 parents c1fa5c1 + f4f8b9a commit a7e88cf

8 files changed

Lines changed: 346 additions & 186 deletions

examples/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ target_link_libraries(implicit_messaging EIPScanner)
1414

1515
add_executable(parameter_object_example ParameterObjectExample.cpp)
1616
target_link_libraries(parameter_object_example EIPScanner)
17+
1718
add_executable(discovery_example DiscoveryManagerExample.cpp)
1819
target_link_libraries(discovery_example EIPScanner)
1920

2021
add_executable(yaskawa_assembly_object_example vendors/yaskawa/mp3300iec/Yaskawa_AssemblyObjectExample.cpp)
2122
target_link_libraries(yaskawa_assembly_object_example EIPScanner)
23+
24+
if(WIN32)
25+
target_link_libraries(explicit_messaging ws2_32)
26+
target_link_libraries(file_object_example ws2_32)
27+
target_link_libraries(identity_object_example ws2_32)
28+
target_link_libraries(implicit_messaging ws2_32)
29+
target_link_libraries(parameter_object_example ws2_32)
30+
target_link_libraries(discovery_example ws2_32)
31+
target_link_libraries(yaskawa_assembly_object_example ws2_32)
32+
endif()

examples/DiscoveryManagerExample.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Created by Aleksey Timin on 12/17/19.
33
//
44

5+
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
6+
#include <winsock2.h>
7+
#define OS_Windows (1)
8+
#endif
9+
510
#include <DiscoveryManager.h>
611
#include <utils/Logger.h>
712

@@ -10,14 +15,29 @@ using eipScanner::utils::Logger;
1015
using eipScanner::utils::LogLevel;
1116

1217
int main() {
13-
Logger::setLogLevel(LogLevel::DEBUG);
18+
Logger::setLogLevel(LogLevel::DEBUG);
19+
20+
#if OS_Windows
21+
WSADATA wsaData;
22+
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
23+
if (winsockStart != 0) {
24+
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
25+
return EXIT_FAILURE;
26+
}
27+
#endif
28+
29+
DiscoveryManager discoveryManager("172.28.255.255", 0xAF12, std::chrono::seconds(1));
30+
auto devices = discoveryManager.discover();
31+
32+
for (auto& device : devices) {
33+
Logger(LogLevel::INFO) << "Discovered device: "
34+
<< device.identityObject.getProductName()
35+
<< " with address " << device.socketAddress.toString();
36+
}
1437

15-
DiscoveryManager discoveryManager("172.28.255.255", 0xAF12, std::chrono::seconds(1));
16-
auto devices = discoveryManager.discover();
38+
#if OS_Windows
39+
WSACleanup();
40+
#endif
1741

18-
for (auto& device : devices) {
19-
Logger(LogLevel::INFO) << "Discovered device: "
20-
<< device.identityObject.getProductName()
21-
<< " with address " << device.socketAddress.toString();
22-
}
23-
}
42+
return EXIT_SUCCESS;
43+
}

examples/ExplicitMessagingExample.cpp

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Created by Aleksey Timin on 11/16/19.
33
//
44

5+
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
6+
#include <winsock2.h>
7+
#define OS_Windows (1)
8+
#endif
9+
510
#include <cstdlib>
611
#include <sstream>
712
#include <cip/connectionManager/NetworkConnectionParams.h>
@@ -22,50 +27,63 @@ using eipScanner::utils::Logger;
2227
using eipScanner::utils::LogLevel;
2328

2429
int main() {
25-
Logger::setLogLevel(LogLevel::DEBUG);
26-
auto si = std::make_shared<SessionInfo>("127.0.0.1", 0xAF12, std::chrono::seconds(10));
27-
auto messageRouter = std::make_shared<MessageRouter>();
30+
Logger::setLogLevel(LogLevel::DEBUG);
31+
32+
#if OS_Windows
33+
WSADATA wsaData;
34+
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
35+
if (winsockStart != 0) {
36+
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
37+
return EXIT_FAILURE;
38+
}
39+
#endif
40+
41+
auto si = std::make_shared<SessionInfo>("127.0.0.1", 0xAF12, std::chrono::seconds(10));
42+
auto messageRouter = std::make_shared<MessageRouter>();
2843

29-
// Read attribute
30-
auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE,
31-
EPath(0x01, 1, 1),
32-
{});
44+
// Read attribute
45+
auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE,
46+
EPath(0x01, 1, 1),
47+
{});
3348

34-
if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
35-
Buffer buffer(response.getData());
36-
CipUint vendorId;
37-
buffer >> vendorId;
49+
if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
50+
Buffer buffer(response.getData());
51+
CipUint vendorId;
52+
buffer >> vendorId;
3853

39-
Logger(LogLevel::INFO) << "Vendor ID is " << vendorId;
40-
} else {
41-
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
42-
}
54+
Logger(LogLevel::INFO) << "Vendor ID is " << vendorId;
55+
} else {
56+
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
57+
}
4358

44-
//Write attribute
45-
// See OpenEr EDS 160 line
46-
Buffer assembly151;
47-
assembly151 << CipUsint(1)
48-
<< CipUsint(2)
49-
<< CipUsint(3)
50-
<< CipUsint(4)
51-
<< CipUsint(5)
52-
<< CipUsint(6)
53-
<< CipUsint(7)
54-
<< CipUsint(8)
55-
<< CipUsint(9)
56-
<< CipUsint(10);
59+
//Write attribute
60+
// See OpenEr EDS 160 line
61+
Buffer assembly151;
62+
assembly151 << CipUsint(1)
63+
<< CipUsint(2)
64+
<< CipUsint(3)
65+
<< CipUsint(4)
66+
<< CipUsint(5)
67+
<< CipUsint(6)
68+
<< CipUsint(7)
69+
<< CipUsint(8)
70+
<< CipUsint(9)
71+
<< CipUsint(10);
5772

5873

59-
response = messageRouter->sendRequest(si, ServiceCodes::SET_ATTRIBUTE_SINGLE,
60-
EPath(0x04, 151, 3),
61-
assembly151.data());
74+
response = messageRouter->sendRequest(si, ServiceCodes::SET_ATTRIBUTE_SINGLE,
75+
EPath(0x04, 151, 3),
76+
assembly151.data());
6277

63-
if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
64-
Logger(LogLevel::INFO) << "Writing is successful";
65-
} else {
66-
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
67-
}
78+
if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
79+
Logger(LogLevel::INFO) << "Writing is successful";
80+
} else {
81+
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
82+
}
6883

84+
#if OS_Windows
85+
WSACleanup();
86+
#endif
6987

70-
return 0;
71-
}
88+
return EXIT_SUCCESS;
89+
}

examples/FileObjectExample.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Created by Aleksey Timin on 11/24/19.
33
//
44

5+
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
6+
#include <winsock2.h>
7+
#define OS_Windows (1)
8+
#endif
9+
510
#include <fstream>
611
#include "FileObject.h"
712
#include "utils/Logger.h"
@@ -14,18 +19,34 @@ using eipScanner::utils::LogLevel;
1419
using eipScanner::FileObject;
1520

1621
int main() {
17-
Logger::setLogLevel(LogLevel::DEBUG);
18-
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);
19-
20-
FileObject edsFile(0xC8, si);
21-
edsFile.beginUpload(si, [](GeneralStatusCodes status, const std::vector<uint8_t> &fileContent) {
22-
if (status == GeneralStatusCodes::SUCCESS) {
23-
std::ofstream outFile("Device.eds", std::ios::out | std::ios::trunc | std::ios::binary);
24-
outFile.write(reinterpret_cast<const char *>(fileContent.data()), fileContent.size());
25-
}
26-
});
27-
28-
while (edsFile.handleTransfers(si)) {
29-
continue;
30-
};
31-
}
22+
Logger::setLogLevel(LogLevel::DEBUG);
23+
24+
#if OS_Windows
25+
WSADATA wsaData;
26+
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
27+
if (winsockStart != 0) {
28+
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
29+
return EXIT_FAILURE;
30+
}
31+
#endif
32+
33+
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);
34+
35+
FileObject edsFile(0xC8, si);
36+
edsFile.beginUpload(si, [](GeneralStatusCodes status, const std::vector<uint8_t> &fileContent) {
37+
if (status == GeneralStatusCodes::SUCCESS) {
38+
std::ofstream outFile("Device.eds", std::ios::out | std::ios::trunc | std::ios::binary);
39+
outFile.write(reinterpret_cast<const char *>(fileContent.data()), fileContent.size());
40+
}
41+
});
42+
43+
while (edsFile.handleTransfers(si)) {
44+
continue;
45+
};
46+
47+
#if OS_Windows
48+
WSACleanup();
49+
#endif
50+
51+
return EXIT_SUCCESS;
52+
}

examples/IdentityObjectExample.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Created by Aleksey Timin on 12/19/19.
33
//
44

5+
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
6+
#include <winsock2.h>
7+
#define OS_Windows (1)
8+
#endif
9+
510
#include "IdentityObject.h"
611
#include "utils/Logger.h"
712

@@ -11,16 +16,31 @@ using eipScanner::utils::Logger;
1116
using eipScanner::utils::LogLevel;
1217

1318
int main() {
14-
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);
15-
IdentityObject identityObject(1, si);
16-
17-
Logger(LogLevel::INFO) << identityObject.getVendorId()
18-
<< identityObject.getDeviceType()
19-
<< identityObject.getProductCode()
20-
<< identityObject.getRevision().toString()
21-
<< identityObject.getStatus()
22-
<< identityObject.getSerialNumber()
23-
<< identityObject.getProductName();
24-
25-
return 0;
26-
}
19+
Logger::setLogLevel(LogLevel::DEBUG);
20+
21+
#if OS_Windows
22+
WSADATA wsaData;
23+
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
24+
if (winsockStart != 0) {
25+
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
26+
return EXIT_FAILURE;
27+
}
28+
#endif
29+
30+
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);
31+
IdentityObject identityObject(1, si);
32+
33+
Logger(LogLevel::INFO) << identityObject.getVendorId()
34+
<< identityObject.getDeviceType()
35+
<< identityObject.getProductCode()
36+
<< identityObject.getRevision().toString()
37+
<< identityObject.getStatus()
38+
<< identityObject.getSerialNumber()
39+
<< identityObject.getProductName();
40+
41+
#if OS_Windows
42+
WSACleanup();
43+
#endif
44+
45+
return EXIT_SUCCESS;
46+
}

0 commit comments

Comments
 (0)