diff options
Diffstat (limited to 'modbuslog.c')
-rw-r--r-- | modbuslog.c | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/modbuslog.c b/modbuslog.c index f9c85e4..c2b34b7 100644 --- a/modbuslog.c +++ b/modbuslog.c @@ -18,7 +18,6 @@ */ // gcc modbuslog.c -o modbuslog `pkg-config --libs --cflags libmodbus libconfig` - #include <stdio.h> #include <unistd.h> #include <stdlib.h> @@ -116,7 +115,8 @@ int main(int argc, char *argv[]) { } modbus_t *ctx; - uint16_t tab_reg[64]; + uint16_t tab_reg[4]; + uint8_t byte[8]; int rc; int i; @@ -131,16 +131,16 @@ int main(int argc, char *argv[]) { //int unixtime = (int) t; //printf("%d\n", unixtime); - config_setting_t *registers; + config_setting_t *readings; - registers = config_lookup(&cfg, "register"); + readings = config_lookup(&cfg, "reading"); - unsigned int num_registers = config_setting_length(registers); + unsigned int num_readings = config_setting_length(readings); int i; - for (i = 0; i < num_registers; ++i) { + for (i = 0; i < num_readings; ++i) { config_setting_t *register_element = config_setting_get_elem( - registers, i); + readings, i); int slaveid; config_setting_lookup_int(register_element, "slaveid", &slaveid); int intervalvalue; @@ -201,6 +201,10 @@ int main(int argc, char *argv[]) { numregisters, tab_reg); } + //MODBUS_GET_HIGH_BYTE(data); + //MODBUS_GET_LOW_BYTE(data); + //MODBUS_SET_INT16_TO_INT8(tab_int8, index, value); + //<mac address>_YYYY_MM_DD_HH_MM_SS.log time_t unixtime_day = ((((int) t) / 86400 * 86400) + 86400); @@ -229,11 +233,28 @@ int main(int argc, char *argv[]) { FILE *filehandle = fopen(filename, "a+"); - uint16_t registervalue = 0; + int registervalue = 0; int p; - for (p = 0; p < numregisters; p = p + 2) { - registervalue += tab_reg[i] + tab_reg[i + 1]; + for (p = 0; p < numregisters; p++) { + MODBUS_SET_INT16_TO_INT8(byte, p * 2, tab_reg[p]); + //registervalue += tab_reg[i] + tab_reg[i + 1]; + } + + switch (numregisters) { + case 1: + registervalue = byte[0]; + break; + case 2: + registervalue = ((byte[0]) << 8) + byte[1]; + break; + case 4: + registervalue = ((byte[0]) << 24) + ((byte[1]) << 16) + + ((byte[2]) << 8) + byte[3]; + break; + default: + registervalue = byte[0]; + break; } struct tm utc = *gmtime(&unixtime_min); |