-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function read_write_multiple_registers (#6)
- Loading branch information
Ómar Högni Guðmarsson
authored
Apr 3, 2024
1 parent
599fe65
commit 0679100
Showing
13 changed files
with
295 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
|
||
add_executable(client_example client_example.cpp) | ||
target_link_libraries(client_example PRIVATE modbus ) | ||
target_link_libraries(client_example PRIVATE modbus) | ||
|
||
add_executable(server_example server_example.cpp) | ||
target_link_libraries(server_example PRIVATE modbus ) | ||
target_link_libraries(server_example PRIVATE modbus) | ||
|
||
add_executable(client_example_read_write_registers client_example_read_write_registers.cpp) | ||
target_link_libraries(client_example_read_write_registers PRIVATE modbus) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2023, Skaginn3x (https://skaginn3x.com) | ||
|
||
#include <iostream> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include <modbus/client.hpp> | ||
|
||
void on_io_error(std::error_code const& error) { | ||
std::cout << "Read error: " << error.message() << "\n"; | ||
} | ||
|
||
int main(int argc, const char** argv) { | ||
if (argc < 3) { | ||
std::cout << "Usage: " << argv[0] << " <hostname> <port>\n"; | ||
return -1; | ||
} | ||
|
||
std::vector<std::thread> pool; | ||
asio::io_context ctx; | ||
|
||
std::string_view hostname = argv[1]; | ||
std::string_view port = argv[2]; | ||
|
||
modbus::client client{ ctx }; | ||
|
||
co_spawn( | ||
ctx, | ||
[&]() -> asio::awaitable<void> { | ||
auto [error] = | ||
co_await client.connect(std::string(hostname), std::string(port), asio::as_tuple(asio::use_awaitable)); | ||
if (error) { | ||
std::cerr << "Error connecting: " << error.message() << '\n'; | ||
exit(-1); | ||
} | ||
std::cout << "Connected!" << '\n'; | ||
|
||
for (size_t i = 0; i < 25; i++) { | ||
auto response = | ||
co_await client.read_write_multiple_registers(0, 1000, 10, 1010, { 1, 2, 3, 4, 5, 6 }, asio::use_awaitable); | ||
if (!response) { | ||
std::cerr << "Error reading: " << response.error().message() << '\n'; | ||
exit(-1); | ||
} | ||
std::cout << "Read registers: "; | ||
for (unsigned short i : response->values) { | ||
std::cout << "\t\t" << i << " "; | ||
} | ||
std::cout << '\n'; | ||
std::this_thread::sleep_for(std::chrono::milliseconds{ 150 }); | ||
} | ||
client.close(); | ||
}, | ||
asio::detached); | ||
|
||
ctx.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.