-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathmain.cpp
More file actions
54 lines (41 loc) · 1.5 KB
/
main.cpp
File metadata and controls
54 lines (41 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Reads input registers of SolarPi pressure meter
// This example code is in the public domain.
#include <iostream>
#include <string>
#include <modbuspp.h>
using namespace std;
using namespace Modbus;
// -----------------------------------------------------------------------------
int main (int argc, char **argv) {
string port ("/dev/ttyUSB0");
if (argc > 1) {
port = argv[1]; // the serial port can be provided as a parameter on the command line.
}
Master mb (Rtu, port , "38400E1"); // new master on RTU
// if you have to handle the DE signal of the line driver with RTS,
// you should uncomment the lines below...
// mb.rtu().setRts(RtsDown);
// mb.rtu().setSerialMode(Rs485);
Slave & slv = mb.addSlave (33); // SolarPi Pressure meter
cout << "Reads input registers of slave[" << slv.number() << "] on " <<
mb.connection() << " (" << mb.settings() << ")" << endl;
if (mb.open ()) { // open a connection
// success, do what you want here
uint16_t values[2];
if (slv.readInputRegisters (1, values, 2) == 2) {
cout << "R0=" << values[0] << endl;
cout << "R1=" << values[1] << endl;
}
else {
cerr << "Unable to read input registers ! " << mb.lastError() << endl;
exit (EXIT_FAILURE);
}
mb.close();
}
else {
cerr << "Unable to open MODBUS connection to " << port << " : " << mb.lastError() << endl;
exit (EXIT_FAILURE);
}
return 0;
}
/* ========================================================================== */