From e60a7cc135af7ee738d67ecc7bd02ce18963ace6 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Thu, 13 Oct 2011 02:00:41 +0100 Subject: Integrate libmodbus and libconfig --- libconfig/example1.c | 2 ++ libconfig/jmccrohan-example.c | 2 ++ libconfig/myprogram | Bin 9968 -> 0 bytes verteco | Bin 9589 -> 0 bytes verteco.c | 60 +++++++++++++++++++++++++++++++++--------- 5 files changed, 51 insertions(+), 13 deletions(-) delete mode 100755 libconfig/myprogram delete mode 100755 verteco diff --git a/libconfig/example1.c b/libconfig/example1.c index 56661f3..8909884 100644 --- a/libconfig/example1.c +++ b/libconfig/example1.c @@ -38,6 +38,8 @@ int main(int argc, char **argv) config_init(&cfg); +fprintf(stderr, "__FILE__"); + /* Read the file. If there is an error, report it and exit. */ if(! config_read_file(&cfg, "example.cfg")) { diff --git a/libconfig/jmccrohan-example.c b/libconfig/jmccrohan-example.c index 1dbb6da..73fd3e8 100644 --- a/libconfig/jmccrohan-example.c +++ b/libconfig/jmccrohan-example.c @@ -14,6 +14,8 @@ int main(int argc, char **argv) config_init(&cfg); +fprintf(stderr, "__FILE__"); + /* Read the file. If there is an error, report it and exit. */ if(! config_read_file(&cfg, "CONFIG_FILE")) { diff --git a/libconfig/myprogram b/libconfig/myprogram deleted file mode 100755 index 8e28bdd..0000000 Binary files a/libconfig/myprogram and /dev/null differ diff --git a/verteco b/verteco deleted file mode 100755 index 4223811..0000000 Binary files a/verteco and /dev/null differ diff --git a/verteco.c b/verteco.c index a039952..91391dd 100644 --- a/verteco.c +++ b/verteco.c @@ -1,33 +1,67 @@ +/ gcc verteco.c -o verteco `pkg-config --libs --cflags libmodbus libconfig` + #include #include #include #include +#include #include -#define SLAVE_ID 1 -#define START_ADDRESS 0 -#define NUMBER_REGISTERS 14 - -#define DEBUG_MODE 0 +#define SLAVE_ID 1 +#define START_ADDRESS 0 +#define NUMBER_REGISTERS 14 +#define DEBUG 1 int main(int argc, char *argv[]) { -if ( argc != 2 ){ +const char *CONFIG_FILE = "verteco.cfg"; + + config_t cfg; + //config_setting_t *setting; + + const char *modbus_device_address; + int modbus_baud_rate; + int modbus_data_bits; + const char *modbus_parity; + int modbus_stop_bits; + + config_init(&cfg); + + + if(!config_read_file(&cfg, CONFIG_FILE)) + { + fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), + config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + fprintf(stderr, "Unable to find CONFIG_FILE.\n"); return -1; + } + + modbus_t *ctx; + uint16_t tab_reg[64]; + int rc; + int i; + + if(!(config_lookup_string(&cfg, "modbus.device", &modbus_device_address) + && config_lookup_int(&cfg, "modbus.baud", &modbus_baud_rate) + && config_lookup_int(&cfg, "modbus.data_bits", &modbus_data_bits) + && config_lookup_string(&cfg, "modbus.parity", &modbus_parity) + && config_lookup_int(&cfg, "modbus.stop_bits", &modbus_stop_bits)) +){ + fprintf(stderr, "Check modbus configuration in configuration file.\n"); + return -1; } + if(DEBUG){ + printf("%s %d %c %d %d\n",modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits); + } -modbus_t *ctx; -uint16_t tab_reg[64]; -int rc; -int i; +ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits); -ctx = modbus_new_rtu(argv[1], 19200, 'E', 8, 1); modbus_set_slave(ctx, SLAVE_ID); - -modbus_set_debug(ctx, DEBUG_MODE); +modbus_set_debug(ctx, DEBUG); if (ctx == NULL) { fprintf(stderr, "Unable to create the libmodbus context\n"); -- cgit v1.2.3