modbuspp  1.1.40
C++ wrapper for the libmodbus library
server/simple-server-json/main.cpp
#include <csignal>
#include <thread>
#include <iostream>
#include <modbuspp.h>
using namespace std;
using namespace Modbus;
Server srv; // instantiates new MODBUS Server
// -----------------------------------------------------------------------------
// Signal trap, triggers during a CTRL+C or if kill is called
void
sighandler (int sig) {
srv.close();
cout << "everything was closed." << endl << "Have a nice day !" << endl;
exit (EXIT_SUCCESS);
}
// -----------------------------------------------------------------------------
int main (int argc, char **argv) {
if (argc < 2) {
cerr << "Error: the JSON filename must be provided as a parameter on the command line !" << endl;
cerr << "e.g. : " << argv[0] << " virtual-server-tcp.json" << endl;
exit (EXIT_FAILURE);
}
string jsonfile = argv[1];
cout << "Simple Server" << endl;
// CTRL+C and kill call triggers the trap sighandler()
signal (SIGINT, sighandler);
signal (SIGTERM, sighandler);
cout << "Press CTRL+C to stop... " << endl << endl;
try {
cout << "opening " << jsonfile << "..." << endl;
srv.setConfig (jsonfile, "modbuspp-server");
if (srv.open ()) { // open a connection
cout << "Listening server on " <<
srv.connection() << ":" << srv.settings() << "..." << endl << endl;
srv.run();
while (srv.isOpen()) {
// std::this_thread::yield();
std::this_thread::sleep_for (std::chrono::milliseconds (200));
}
}
}
catch (std::exception & e) {
cerr << "Error: " << e.what() << endl;
}
catch (...) {
cerr << "Unattended exception !" << endl;
}
return EXIT_FAILURE;
}
/* ========================================================================== */