#include <csignal>
#include <thread>
#include <iostream>
#include <modbuspp.h>
using namespace std;
void
sighandler (int sig) {
cout << "everything was closed." << endl << "Have a nice day !" << endl;
}
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] << " router-tcp-rs232.json" << endl;
exit (EXIT_FAILURE);
}
string jsonfile = argv[1];
cout << "--- Json Modbus Router ---" << endl;
signal (SIGINT, sighandler);
signal (SIGTERM, sighandler);
cout << "Press CTRL+C to stop... " << endl;
try {
cout << "opening " << jsonfile << "..." << endl;
router.
setConfig (jsonfile,
"modbuspp-router");
cout << endl;
for (
const auto & m : router.
masters()) {
auto master = m.second;
cout << "Master " << m.first << " connected through " << flush;
cout << master->connection() << ":" ;
cout << master->settings() << " with the slaves below:" << endl;
for (
const auto & s : router.
slaves()) {
auto slave = s.second;
if (slave->device() == master.get()) {
cout << "> id: " << slave->number() << endl;
}
}
cout << endl;
}
}
cout << "Listening server on " <<
<< endl << endl;
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;
}