diff options
author | Jonathan McCrohan <jmccrohan@gmail.com> | 2012-01-05 02:11:13 +0000 |
---|---|---|
committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2012-01-05 02:11:13 +0000 |
commit | 608d693c89f76e242e5491bfa6317d9774637e9f (patch) | |
tree | b8a6d912de95eb83ec2881b47928d356f2c7d3e0 | |
parent | 9bccaac7efbf7221704154149e5df45e7567c831 (diff) | |
download | verteco-608d693c89f76e242e5491bfa6317d9774637e9f.tar.gz |
Add comments
-rw-r--r-- | src/modbuslog.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/modbuslog.c b/src/modbuslog.c index be7a4d1..9424936 100644 --- a/src/modbuslog.c +++ b/src/modbuslog.c @@ -42,10 +42,13 @@ #define START_ADDRESS 0 #define NUMBER_REGISTERS 14 + +// handle SIGALRM by resetting it void minute_check(int signum) { alarm(60); } +// get mac address of primary interface eth0 char *mac_address() { int s; struct ifreq ifr; @@ -74,6 +77,8 @@ int main(int argc, char *argv[]) { int DEBUG = 0; int k; + + // check the argv array for strings matching -d for (k = 1; k < argc; k++) { if (strcmp(argv[k], "-d") == 0) { DEBUG = 1; @@ -92,6 +97,8 @@ int main(int argc, char *argv[]) { config_init(&cfg); + // attempt to read config + // loads entire file to memory and destroys file descriptor if (!config_read_file(&cfg, configfile)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); @@ -100,6 +107,7 @@ int main(int argc, char *argv[]) { return -1; } + // die if core config file options aren't there 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) @@ -123,10 +131,14 @@ int main(int argc, char *argv[]) { int rc; int i; + // SIGALRM used to wake for loop up every 60 secs + // sleep puts whole thread to sleep which isn't what we want + // other methods involve CPU spinlocks which are inefficient signal(SIGALRM, minute_check); alarm(60); for (;;) { + // block until SIGARLM select(0, NULL, NULL, NULL, NULL); time_t t = time(NULL); @@ -138,9 +150,12 @@ int main(int argc, char *argv[]) { readings = config_lookup(&cfg, "reading"); + // find number of required readings unsigned int num_readings = config_setting_length(readings); int i; + + // cycle through each reading and pull info from config file for (i = 0; i < num_readings; ++i) { config_setting_t *register_element = config_setting_get_elem( readings, i); @@ -176,10 +191,12 @@ int main(int argc, char *argv[]) { else intervalduration = intervalvalue * 60; + // if we match the required time for the reading if (unixtime_min % intervalduration == 0) { //printf("specified minute. %d %d\n", unixtime_min, // intervalduration); + // attempt to create new modbus connection ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits); @@ -199,6 +216,8 @@ int main(int argc, char *argv[]) { } int retry = 0; + + // handle timeouts and retries do { switch (registertype) { @@ -227,13 +246,14 @@ int main(int argc, char *argv[]) { //MODBUS_GET_LOW_BYTE(data); //MODBUS_SET_INT16_TO_INT8(tab_int8, index, value); - //<mac address>_YYYY_MM_DD_HH_MM_SS.log + // round forward to next midnight time_t unixtime_day = ((((int) t) / 86400 * 86400) + 86400); struct tm midnight = *localtime(&unixtime_day); char filename[50]; + //<mac address>_YYYY_MM_DD_HH_MM_SS.log sprintf(filename, "/var/modbuslog/%s_%04i_%02i_%02i_%02i_%02i_%02i.log", mac_address(), midnight.tm_year + 1900, @@ -259,11 +279,14 @@ int main(int argc, char *argv[]) { int16_t registervalue = 0; int p; + + // data arrives in words, split into bytes 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]; } + // bitshifting magic switch (numregisters) { case 1: registervalue = byte[0]; |