diff options
Diffstat (limited to 'modbuslog.c')
-rw-r--r-- | modbuslog.c | 100 |
1 files changed, 49 insertions, 51 deletions
diff --git a/modbuslog.c b/modbuslog.c index 6a29b9d..6d14ce1 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> @@ -46,7 +45,7 @@ void minute_check(int signum) { alarm(60); } -void getmac(unsigned char* mac_address) { +void find_mac(unsigned char* mac_address) { int s; struct ifreq ifr; @@ -75,7 +74,6 @@ int main(int argc, char *argv[]) { config_t cfg; //config_setting_t *setting; - //unsigned char* mac_address; unsigned char mac_address[6]; const char *modbus_device_address; @@ -86,11 +84,7 @@ int main(int argc, char *argv[]) { config_init(&cfg); - getmac(mac_address); - - if (DEBUG) { - printf("%s\n", mac_address); - } + find_mac(mac_address); if (!config_read_file(&cfg, CONFIG_FILE)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), @@ -111,8 +105,9 @@ int main(int argc, char *argv[]) { } if (DEBUG) { - printf("%s %d %c %d %d\n", modbus_device_address, modbus_baud_rate, - modbus_parity[0], modbus_data_bits, modbus_stop_bits); + printf("%s %s %d %c %d %d\n", mac_address, modbus_device_address, + modbus_baud_rate, modbus_parity[0], modbus_data_bits, + modbus_stop_bits); } modbus_t *ctx; @@ -146,13 +141,21 @@ int main(int argc, char *argv[]) { int intervalvalue; config_setting_lookup_int(register_element, "intervalvalue", &intervalvalue); - printf("%d", intervalvalue); + //printf("%d", intervalvalue); + + int startaddress; + config_setting_lookup_int(register_element, "startaddress", + &startaddress); + + int numregisters; + config_setting_lookup_int(register_element, "numregisters", + &numregisters); const char *intervalunit; config_setting_lookup_string(register_element, "intervalunit", &intervalunit); - printf("%c", intervalunit[0]); + //printf("%c", intervalunit[0]); int intervalduration; @@ -162,53 +165,48 @@ int main(int argc, char *argv[]) { intervalduration = intervalvalue * 60; if (unixtime_min % intervalduration == 0) { - printf("specified minute. %d %d\n", unixtime_min, - intervalduration); - } - - printf("%d ", slaveid); - } - printf("%d\n", unixtime_min); - } -} + //printf("specified minute. %d %d\n", unixtime_min, + // intervalduration); -/* + ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, + modbus_parity[0], modbus_data_bits, modbus_stop_bits); - ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits); + modbus_set_slave(ctx, slaveid); + modbus_set_debug(ctx, DEBUG); - modbus_set_slave(ctx, SLAVE_ID); - modbus_set_debug(ctx, DEBUG); + if (ctx == NULL) { + fprintf(stderr, "Unable to create libmodbus object.\n"); + //return -1; + } - if (ctx == NULL) { - fprintf(stderr, "Unable to create libmodbus object.\n"); - return -1; - } + if (modbus_connect(ctx) == -1) { + fprintf(stderr, "Connection failed: %s\n", + modbus_strerror(errno)); + modbus_free(ctx); + //return -1; + } + rc = modbus_read_input_registers(ctx, startaddress, + numregisters, tab_reg); - if (modbus_connect(ctx) == -1) { - fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); - modbus_free(ctx); - return -1; - } + if (rc == -1) { + fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno)); + //return -1; + } - rc = modbus_read_input_registers(ctx, START_ADDRESS, NUMBER_REGISTERS, tab_reg); + for (i = 0; i < rc; i = i + 2) { + printf("reg[%d]=%d\n", i, tab_reg[i] + tab_reg[i + 1]); + } + modbus_close(ctx); + modbus_free(ctx); - if (rc == -1) { - fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno)); - return -1; - } + //return 0; + } - for (i=0; i < rc; i=i+2) { - printf("reg[%d]=%d\n", i, tab_reg[i]+tab_reg[i+1]); - } - - - modbus_close(ctx); - modbus_free(ctx); - - return 0; - } - - */ + //printf("%d ", slaveid); + } + //printf("%d\n", unixtime_min); + } +} |