diff options
author | Jonathan McCrohan <jmccrohan@gmail.com> | 2011-11-02 01:32:31 +0000 |
---|---|---|
committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2011-11-02 01:32:31 +0000 |
commit | c9d1e4e78a1a13d396806e9b2b71bb8561f2ae3e (patch) | |
tree | 33d63c1077529d289c5741fcb3d765a591687342 | |
parent | b8216bcb5698ca1aa2d74416c5103d407e846351 (diff) | |
download | verteco-c9d1e4e78a1a13d396806e9b2b71bb8561f2ae3e.tar.gz |
Modbus logging working.
-rw-r--r-- | modbuslog.c | 100 | ||||
-rw-r--r-- | modbuslog.cfg | 20 |
2 files changed, 59 insertions, 61 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); + } +} diff --git a/modbuslog.cfg b/modbuslog.cfg index 1157a3d..f724e43 100644 --- a/modbuslog.cfg +++ b/modbuslog.cfg @@ -3,7 +3,7 @@ cfg_version = 0.99; modbus = { - device = "/dev/ttyUSB4"; + device = "/dev/ttyUSB2"; baud = 19200; data_bits = 8; parity = "E" @@ -14,25 +14,25 @@ register = ( { slaveid = 1; registertype = 4; - startaddress = 2; - numregisters = 6; + startaddress = 0; + numregisters = 2; datatype = "byte"; intervalvalue = 2; intervalunit = "M"; }, { - slaveid = 99; - registertype = 5; - startaddress = 7; - numregisters = 8; + slaveid = 1; + registertype = 4; + startaddress = 4; + numregisters = 4; datatype = "word"; intervalvalue = 1; intervalunit = "M"; }, { - slaveid = 2; - registertype = 3; - startaddress = 4; + slaveid = 1; + registertype = 4; + startaddress = 10; numregisters = 2; datatype = "byte"; intervalvalue = 12; |