#include <iostream>
#include <string>
#include <modbuspp.h>
#include <modbuspp/popl.h>
using namespace std;
int main (int argc, char **argv) {
string jsonfile ("../master.json");
if (argc > 1) {
jsonfile = argv[1];
}
Master mb (jsonfile,
"modbuspp-master");
cout <<
"Read/Write holding registers of slave[" << slv.
number() <<
"] on " <<
int ret;
cout <<
"R0=" << backup[0].
value() << endl;
cout <<
"R1=" << backup[1].
value() << endl;
cout <<
"R2=" << backup[2].
value() << endl;
cout <<
"R3=" << backup[3].
value() << endl;
}
else {
cerr <<
"Unable to read holding registers ! " << mb.
lastError() << endl;
exit (EXIT_FAILURE);
}
bank1[0] = 152.3;
bank1[1] = 1010.7;
bank1[2] = 45;
bank1[3] = 901;
if (ret < 0) {
cerr <<
"Unable to write holding registers ! " << mb.
lastError() << endl;
exit (EXIT_FAILURE);
}
else {
cout << ret << " registers written (16-bit)." << endl;
}
bool ok = true;
for (int i = 0; i < 4; i++) {
ok = ok && (bank1[i] == bank2[i]);
}
if (ok) {
cout << "Registers have been correctly written" << endl;
}
else {
cout << "Registers were not correctly written" << endl;
cout <<
"R0=" << bank2[0].
value() << endl;
cout <<
"R1=" << bank2[1].
value() << endl;
cout <<
"R2=" << bank2[2].
value() << endl;
cout <<
"R3=" << bank2[3].
value() << endl;
}
}
else {
cerr <<
"Unable to read holding registers ! " << mb.
lastError() << endl;
exit (EXIT_FAILURE);
}
if (ret < 0) {
cerr <<
"Unable to write holding registers ! " << mb.
lastError() << endl;
exit (EXIT_FAILURE);
}
else {
cout << ret << " restored registers (16-bit)." << endl;
}
}
else {
cerr <<
"Unable to open MODBUS connection to " << mb.
connection() <<
" : " exit (EXIT_FAILURE);
}
return 0;
}