diff options
author | Jonathan McCrohan <jmccrohan@gmail.com> | 2011-11-03 01:05:06 +0000 |
---|---|---|
committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2011-11-03 01:05:06 +0000 |
commit | d747c60873804a8e58edeb681cde117853bf20a1 (patch) | |
tree | 0ece0ab2a26fb23962a17736d1b01b01400f7915 | |
parent | c9d1e4e78a1a13d396806e9b2b71bb8561f2ae3e (diff) | |
download | verteco-d747c60873804a8e58edeb681cde117853bf20a1.tar.gz |
Logging to file as per spec.v1.0
-rw-r--r-- | modbuslog.c | 71 | ||||
-rw-r--r-- | modbuslog.cfg | 21 |
2 files changed, 72 insertions, 20 deletions
diff --git a/modbuslog.c b/modbuslog.c index 6d14ce1..e9346eb 100644 --- a/modbuslog.c +++ b/modbuslog.c @@ -18,6 +18,7 @@ */ // gcc modbuslog.c -o modbuslog `pkg-config --libs --cflags libmodbus libconfig` + #include <stdio.h> #include <unistd.h> #include <stdlib.h> @@ -26,6 +27,7 @@ #include <unistd.h> #include <sys/select.h> #include <signal.h> +#include <fcntl.h> #include <libconfig.h> #include <modbus.h> @@ -39,7 +41,6 @@ #define SLAVE_ID 1 #define START_ADDRESS 0 #define NUMBER_REGISTERS 14 -#define DEBUG 1 void minute_check(int signum) { alarm(60); @@ -69,7 +70,15 @@ void find_mac(unsigned char* mac_address) { int main(int argc, char *argv[]) { - const char *CONFIG_FILE = "modbuslog.cfg"; + const char *configfile = "modbuslog.cfg"; + + int DEBUG = 0; + int k; + for (k = 1; k < argc; k++) { + if (strcmp(argv[k], "-d") == 0) { + DEBUG = 1; + } + } config_t cfg; //config_setting_t *setting; @@ -86,11 +95,11 @@ int main(int argc, char *argv[]) { find_mac(mac_address); - if (!config_read_file(&cfg, CONFIG_FILE)) { + if (!config_read_file(&cfg, configfile)) { 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"); + fprintf(stderr, "Unable to find configfile.\n"); return -1; } @@ -122,7 +131,7 @@ int main(int argc, char *argv[]) { select(0, NULL, NULL, NULL, NULL); time_t t = time(NULL); - int unixtime_min = (((int) t) / 60 * 60); + time_t unixtime_min = (((int) t) / 60 * 60); //int unixtime = (int) t; //printf("%d\n", unixtime); @@ -159,7 +168,7 @@ int main(int argc, char *argv[]) { int intervalduration; - if (intervalunit[0] == 'H') + if (intervalunit[0] == 'h') intervalduration = intervalvalue * 3600; else intervalduration = intervalvalue * 60; @@ -192,15 +201,61 @@ int main(int argc, char *argv[]) { if (rc == -1) { fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno)); //return -1; + rc = modbus_read_input_registers(ctx, startaddress, + numregisters, tab_reg); } - for (i = 0; i < rc; i = i + 2) { - printf("reg[%d]=%d\n", i, tab_reg[i] + tab_reg[i + 1]); + //<mac address>_YYYY_MM_DD_HH_MM_SS.log + time_t unixtime_day = ((((int) t) / 86400 * 86400) + 86400); + + struct tm midnight = *localtime(&unixtime_day); + + char filename[50]; + + sprintf(filename, "%s_%04i_%02i_%02i_%02i_%02i_%02i.log", + mac_address, midnight.tm_year + 1900, + midnight.tm_mon + 1, midnight.tm_mday, midnight.tm_hour, + midnight.tm_min, midnight.tm_sec); + + //printf("%s\n",filename); + + FILE *fp = fopen(filename, "r"); + if (fp) { + fclose(fp); + } else { + // file doesn't exist. create it. + FILE *fp = fopen(filename, "w"); + fprintf( + fp, + "IntervalID|UTCDate|UTCTime|LOCALDate|LOCALTime|SensorID|RegisterID|Reading\n"); + fclose(fp); } + FILE *filehandle = fopen(filename, "a+"); + + uint16_t registervalue = 0; + + int p; + for (p = 0; p < numregisters; p = p + 2) { + registervalue += tab_reg[i] + tab_reg[i + 1]; + } + + struct tm utc = *gmtime(&unixtime_min); + struct tm lc = *localtime(&unixtime_min); + + fprintf( + filehandle, + "1|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%i\n", + utc.tm_year + 1900, utc.tm_mon + 1, utc.tm_mday, + utc.tm_hour, utc.tm_min, utc.tm_sec, lc.tm_year + 1900, + lc.tm_mon + 1, lc.tm_mday, lc.tm_hour, lc.tm_min, + lc.tm_sec, slaveid, startaddress, registervalue); + fclose(filehandle); + modbus_close(ctx); modbus_free(ctx); + sleep(1); //return 0; } diff --git a/modbuslog.cfg b/modbuslog.cfg index f724e43..778b6f4 100644 --- a/modbuslog.cfg +++ b/modbuslog.cfg @@ -3,7 +3,7 @@ cfg_version = 0.99; modbus = { - device = "/dev/ttyUSB2"; + device = "/dev/ttyUSB1"; baud = 19200; data_bits = 8; parity = "E" @@ -16,27 +16,24 @@ register = ( registertype = 4; startaddress = 0; numregisters = 2; - datatype = "byte"; - intervalvalue = 2; - intervalunit = "M"; + intervalvalue = 1; + intervalunit = "m"; }, { slaveid = 1; registertype = 4; - startaddress = 4; + startaddress = 8; numregisters = 4; - datatype = "word"; intervalvalue = 1; - intervalunit = "M"; + intervalunit = "m"; }, { slaveid = 1; registertype = 4; - startaddress = 10; - numregisters = 2; - datatype = "byte"; - intervalvalue = 12; - intervalunit = "H"; + startaddress = 13; + numregisters = 1; + intervalvalue = 2; + intervalunit = "m"; } ); |