aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/modbuslog.c49
-rw-r--r--src/modbuslog.cfg14
2 files changed, 38 insertions, 25 deletions
diff --git a/src/modbuslog.c b/src/modbuslog.c
index d42a024..910e53f 100644
--- a/src/modbuslog.c
+++ b/src/modbuslog.c
@@ -184,13 +184,13 @@ int main(int argc, char *argv[]) {
config_setting_lookup_int(register_element, "registertype",
&registertype);
- int startaddress;
- config_setting_lookup_int(register_element, "startaddress",
- &startaddress);
+ int startbyte;
+ config_setting_lookup_int(register_element, "startbyte",
+ &startbyte);
- int numregisters;
- config_setting_lookup_int(register_element, "numregisters",
- &numregisters);
+ int numbytes;
+ config_setting_lookup_int(register_element, "numbytes",
+ &numbytes);
const char *intervalunit;
config_setting_lookup_string(register_element, "intervalunit",
@@ -228,30 +228,36 @@ int main(int argc, char *argv[]) {
// if connection fails, skip rest of loop
syslog(
LOG_ERR,
- "Skipping read from slaveid=[%i], registertype=[%i], startaddress=[%i], numregisters=[%i]",
- slaveid, registertype, startaddress, numregisters);
+ "Skipping read from slaveid=[%i], registertype=[%i], startbyte=[%i], numbytes=[%i]",
+ slaveid, registertype, startbyte, numbytes);
continue;
}
int retry = 0;
+ // convert bytes to registers
+ int nummodbusregisters;
+ nummodbusregisters = (((numbytes-1)/2)+1);
+
+ int startmodbusregister;
+ startmodbusregister = (startbyte/2);
// handle timeouts and retries
do {
switch (registertype) {
case 3:
- rc = modbus_read_registers(ctx, startaddress,
- numregisters, tab_reg);
+ rc = modbus_read_registers(ctx, startmodbusregister,
+ nummodbusregisters, tab_reg);
break;
case 4:
- rc = modbus_read_input_registers(ctx, startaddress,
- numregisters, tab_reg);
+ rc = modbus_read_input_registers(ctx, startmodbusregister,
+ nummodbusregisters, tab_reg);
break;
default:
- rc = modbus_read_registers(ctx, startaddress,
- numregisters, tab_reg);
+ rc = modbus_read_registers(ctx, startmodbusregister,
+ nummodbusregisters, tab_reg);
break;
}
if (rc == -1) {
@@ -305,16 +311,23 @@ int main(int argc, char *argv[]) {
int p;
// data arrives in words, split into bytes
- for (p = 0; p < numregisters; p++) {
+ for (p = 0; p < nummodbusregisters; p++) {
MODBUS_SET_INT16_TO_INT8(byte, p * 2, tab_reg[p]);
//registervalue += tab_reg[i] + tab_reg[i + 1];
}
// bitshifting magic
- switch (numregisters*2) {
+ switch (numbytes) {
+ case 1:
+ registervalue = byte[0];
+ break;
case 2:
registervalue = ((byte[0]) << 8) + byte[1];
break;
+ case 3:
+ registervalue = ((byte[0]) << 16) + ((byte[1]) << 8)
+ + byte[2];
+ break;
case 4:
registervalue = ((byte[0]) << 24) + ((byte[1]) << 16)
+ ((byte[2]) << 8) + byte[3];
@@ -369,7 +382,7 @@ int main(int argc, char *argv[]) {
intervalid, utc.tm_year + 1900, utc.tm_mon + 1,
utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
lc.tm_year + 1900, lc.tm_mon + 1, lc.tm_mday,
- lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startaddress,
+ lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startbyte,
registervalue);
fclose(filehandle);
@@ -379,7 +392,7 @@ int main(int argc, char *argv[]) {
intervalid, utc.tm_year + 1900, utc.tm_mon + 1,
utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
lc.tm_year + 1900, lc.tm_mon + 1, lc.tm_mday,
- lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startaddress,
+ lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startbyte,
registervalue);
modbus_close(ctx);
diff --git a/src/modbuslog.cfg b/src/modbuslog.cfg
index 56a5d10..4cc7c35 100644
--- a/src/modbuslog.cfg
+++ b/src/modbuslog.cfg
@@ -23,7 +23,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 0;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
},
@@ -31,7 +31,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 2;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
},
@@ -39,7 +39,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 4;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
},
@@ -47,7 +47,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 6;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
},
@@ -55,7 +55,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 8;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
},
@@ -63,7 +63,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 10;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
},
@@ -71,7 +71,7 @@ reading= (
slaveid = 1;
registertype = 4;
startaddress = 14;
- numregisters = 2;
+ numbytes = 2;
intervalvalue = 1;
intervalunit = "m";
}