diff options
author | Jonathan McCrohan <jmccrohan@gmail.com> | 2012-03-25 00:48:46 +0000 |
---|---|---|
committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2012-03-25 02:23:27 +0100 |
commit | 5be6f437eed213cdfec5b82e5b4a12af5cc367c3 (patch) | |
tree | e5371736a5ae66b9b719b7bd9c6b60e68cfdd237 /src/modbuslog.c | |
parent | 0af3e1a1af86c52991af450107a73b036fc42cb0 (diff) | |
download | verteco-5be6f437eed213cdfec5b82e5b4a12af5cc367c3.tar.gz |
modbuslog: move exclusively to numbytes
Diffstat (limited to 'src/modbuslog.c')
-rw-r--r-- | src/modbuslog.c | 49 |
1 files changed, 31 insertions, 18 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", ®istertype); - 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); |