aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-11-01 14:50:24 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2011-11-01 14:50:24 +0000
commitb8216bcb5698ca1aa2d74416c5103d407e846351 (patch)
treeab68fff5108da701b2ccda2bc98a87ae98e4e4c8
parent78dce59594ebe24123889d0b5a4e7f224c811ac2 (diff)
downloadverteco-b8216bcb5698ca1aa2d74416c5103d407e846351.tar.gz
Fix indentation.
Diffstat (limited to '')
-rw-r--r--modbuslog.c294
-rw-r--r--modbuslog.cfg23
2 files changed, 165 insertions, 152 deletions
diff --git a/modbuslog.c b/modbuslog.c
index e46c9a7..6a29b9d 100644
--- a/modbuslog.c
+++ b/modbuslog.c
@@ -19,7 +19,6 @@
// gcc modbuslog.c -o modbuslog `pkg-config --libs --cflags libmodbus libconfig`
-
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
@@ -43,168 +42,173 @@
#define NUMBER_REGISTERS 14
#define DEBUG 1
-void minute_check(int signum){
- alarm(60);
+void minute_check(int signum) {
+ alarm(60);
}
-void getmac(unsigned char* mac_address){
- int s;
- struct ifreq ifr;
-
- s = socket(PF_INET, SOCK_DGRAM, 0);
- memset(&ifr, 0x00, sizeof(ifr));
- strcpy(ifr.ifr_name, "eth0");
- ioctl(s, SIOCGIFHWADDR, &ifr);
- close(s);
-
- unsigned char mac_address_internal[6];
- sprintf(mac_address_internal,"%.2X%.2X%.2X%.2X%.2X%.2X", (unsigned char) ifr.ifr_hwaddr.sa_data[0], (unsigned char) ifr.ifr_hwaddr.sa_data[1], (unsigned char) ifr.ifr_hwaddr.sa_data[2], (unsigned char) ifr.ifr_hwaddr.sa_data[3], (unsigned char) ifr.ifr_hwaddr.sa_data[4], (unsigned char) ifr.ifr_hwaddr.sa_data[5]);
-
- strcpy(mac_address,mac_address_internal);
+void getmac(unsigned char* mac_address) {
+ int s;
+ struct ifreq ifr;
+
+ s = socket(PF_INET, SOCK_DGRAM, 0);
+ memset(&ifr, 0x00, sizeof(ifr));
+ strcpy(ifr.ifr_name, "eth0");
+ ioctl(s, SIOCGIFHWADDR, &ifr);
+ close(s);
+
+ unsigned char mac_address_internal[6];
+ sprintf(mac_address_internal, "%.2X%.2X%.2X%.2X%.2X%.2X",
+ (unsigned char) ifr.ifr_hwaddr.sa_data[0],
+ (unsigned char) ifr.ifr_hwaddr.sa_data[1],
+ (unsigned char) ifr.ifr_hwaddr.sa_data[2],
+ (unsigned char) ifr.ifr_hwaddr.sa_data[3],
+ (unsigned char) ifr.ifr_hwaddr.sa_data[4],
+ (unsigned char) ifr.ifr_hwaddr.sa_data[5]);
+
+ strcpy(mac_address, mac_address_internal);
}
-
-
-int main(int argc, char *argv[])
-{
-
- const char *CONFIG_FILE = "modbuslog.cfg";
-
- config_t cfg;
- //config_setting_t *setting;
-
- //unsigned char* mac_address;
- unsigned char mac_address[6];
-
- const char *modbus_device_address;
- int modbus_baud_rate;
- int modbus_data_bits;
- const char *modbus_parity;
- int modbus_stop_bits;
-
- config_init(&cfg);
-
- getmac(mac_address);
- if(DEBUG){
-printf("%s\n",mac_address);
+int main(int argc, char *argv[]) {
+
+ const char *CONFIG_FILE = "modbuslog.cfg";
+
+ config_t cfg;
+ //config_setting_t *setting;
+
+ //unsigned char* mac_address;
+ unsigned char mac_address[6];
+
+ const char *modbus_device_address;
+ int modbus_baud_rate;
+ int modbus_data_bits;
+ const char *modbus_parity;
+ int modbus_stop_bits;
+
+ config_init(&cfg);
+
+ getmac(mac_address);
+
+ if (DEBUG) {
+ printf("%s\n", mac_address);
+ }
+
+ 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;
+ }
+
+ 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,
+ "Incomplete modbus configuration. Check 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;
+
+ signal(SIGALRM, minute_check);
+ alarm(60);
+
+ for (;;) {
+ select(0, NULL, NULL, NULL, NULL);
+
+ time_t t = time(NULL);
+ int unixtime_min = (((int) t) / 60 * 60);
+ //int unixtime = (int) t;
+ //printf("%d\n", unixtime);
+
+ config_setting_t *registers;
+
+ registers = config_lookup(&cfg, "register");
+
+ unsigned int num_registers = config_setting_length(registers);
+
+ int i;
+ for (i = 0; i < num_registers; ++i) {
+ config_setting_t *register_element = config_setting_get_elem(
+ registers, i);
+ int slaveid;
+ config_setting_lookup_int(register_element, "slaveid", &slaveid);
+ int intervalvalue;
+ config_setting_lookup_int(register_element, "intervalvalue",
+ &intervalvalue);
+ printf("%d", intervalvalue);
+
+ const char *intervalunit;
+ config_setting_lookup_string(register_element, "intervalunit",
+ &intervalunit);
+
+ printf("%c", intervalunit[0]);
+
+ int intervalduration;
+
+ if (intervalunit[0] == 'H')
+ intervalduration = intervalvalue * 3600;
+ else
+ 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);
+ }
}
- 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;
- }
-
- 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, "Incomplete modbus configuration. Check 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;
-
-
- signal(SIGALRM, minute_check);
- alarm(60);
-
- for (;;){
- select(0, NULL, NULL, NULL, NULL);
-
- time_t t = time(NULL);
- int unixtime_min = (((int) t)/60*60);
- //int unixtime = (int) t;
- //printf("%d\n", unixtime);
-
- config_setting_t *registers;
-
- registers = config_lookup(&cfg, "register");
-
- unsigned int num_registers = config_setting_length(registers);
-
- int i;
- for(i = 0; i < num_registers; ++i){
- config_setting_t *register_element = config_setting_get_elem(registers, i);
- int slaveid;
- config_setting_lookup_int(register_element, "slaveid", &slaveid);
- int intervalvalue;
- config_setting_lookup_int(register_element, "intervalvalue", &intervalvalue);
- printf("%d", intervalvalue);
-
- const char *intervalunit;
- config_setting_lookup_string(register_element, "intervalunit", &intervalunit);
-
- printf("%c", intervalunit[0]);
-
- int intervalduration;
-
- if(intervalunit[0]=='H')
- intervalduration=intervalvalue*3600;
- else
- 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);
- }
-}
-
-
/*
-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, SLAVE_ID);
-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;
-}
+ 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, START_ADDRESS, NUMBER_REGISTERS, tab_reg);
+ rc = modbus_read_input_registers(ctx, START_ADDRESS, NUMBER_REGISTERS, tab_reg);
-if (rc == -1) {
- fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno));
- return -1;
-}
+ if (rc == -1) {
+ fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno));
+ return -1;
+ }
-for (i=0; i < rc; i=i+2) {
- printf("reg[%d]=%d\n", i, tab_reg[i]+tab_reg[i+1]);
-}
+ 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);
+ modbus_close(ctx);
+ modbus_free(ctx);
- return 0;
-}
+ return 0;
+ }
-*/
+ */
diff --git a/modbuslog.cfg b/modbuslog.cfg
index d9e41df..1157a3d 100644
--- a/modbuslog.cfg
+++ b/modbuslog.cfg
@@ -10,23 +10,32 @@ modbus = {
stop_bits = 1;
};
-device = (
+register = (
{
slaveid = 1;
registertype = 4;
startaddress = 2;
numregisters = 6;
datatype = "byte";
- intervalvalue = 15;
+ intervalvalue = 2;
intervalunit = "M";
},
{
- slaveid = 1;
- registertype = 4;
- startaddress = 1;
- numregisters = 1;
- datatype = "byte";
+ slaveid = 99;
+ registertype = 5;
+ startaddress = 7;
+ numregisters = 8;
+ datatype = "word";
intervalvalue = 1;
+ intervalunit = "M";
+ },
+ {
+ slaveid = 2;
+ registertype = 3;
+ startaddress = 4;
+ numregisters = 2;
+ datatype = "byte";
+ intervalvalue = 12;
intervalunit = "H";
}
);