forked from aslubsky/cpp-simple-scada
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_bus_reader.cpp
70 lines (58 loc) · 1.61 KB
/
mod_bus_reader.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "mod_bus_reader.h"
#include <modbus.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
ModBusReader::ModBusReader(config_t cfg)
{
if(!config_lookup_bool(&cfg, "main.debug", &this->debug)) {
this->debug = false;
}
if(!config_lookup_string(&cfg, "reader.port", &this->_address)) {
throw "Reader port is not defined";
}
if(!config_lookup_int(&cfg, "reader.baud_rate", &this->baud)) {
baud = 115200;
}
if(!config_lookup_int(&cfg, "reader.slave_id", &this->slave_id)) {
throw "Reader slave_id is not defined";
}
if(!config_lookup_int(&cfg, "reader.register_number", &this->register_number)) {
throw "Reader register_number is not defined";
}
}
ModBusReader::~ModBusReader()
{
modbus_close(this->mb);
modbus_free(this->mb);
}
void ModBusReader::connect()
{
this->mb = modbus_new_rtu(this->_address, this->baud, 'N', 8, 2);
if (this->mb == NULL) {
throw "Unable to allocate libmodbus context";
}
//modbus_set_debug(this->mb, TRUE);
modbus_set_error_recovery(this->mb,
(modbus_error_recovery_mode)(MODBUS_ERROR_RECOVERY_LINK |
MODBUS_ERROR_RECOVERY_PROTOCOL));
modbus_set_slave(mb, this->slave_id);
if (modbus_connect(mb) == -1) {
modbus_free(mb);
throw modbus_strerror(errno);
}
}
double ModBusReader::read()
{
int rc;
uint16_t tab_reg[16];
rc = modbus_read_registers(this->mb, this->register_number, 1, tab_reg);
if (rc == -1) {
throw modbus_strerror(errno);
}
if(this->debug == 1) {
std::cout << this->_address << ": " << ((double)tab_reg[0]/10) << std::endl;
}
return (double)((double)tab_reg[0]/10);
}