aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-12-05 01:41:41 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2011-12-05 01:41:41 +0000
commit38f7f2cf0fdda2971b244ce19162921c05c8c0b1 (patch)
tree0757ae8098c85e8477d1e6b234aa65692d2df676
parente061d097225ad8b855f8589913cd284b76e3b670 (diff)
downloadverteco-38f7f2cf0fdda2971b244ce19162921c05c8c0b1.tar.gz
fix interval counter, add support for multiple register types.
-rw-r--r--modbuslog.c57
1 files changed, 39 insertions, 18 deletions
diff --git a/modbuslog.c b/modbuslog.c
index db40244..248b8cc 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>
@@ -148,6 +149,10 @@ int main(int argc, char *argv[]) {
&intervalvalue);
//printf("%d", intervalvalue);
+ int registertype;
+ config_setting_lookup_int(register_element, "registertype",
+ &registertype);
+
int startaddress;
config_setting_lookup_int(register_element, "startaddress",
&startaddress);
@@ -191,15 +196,30 @@ int main(int argc, char *argv[]) {
//return -1;
}
- rc = modbus_read_input_registers(ctx, startaddress,
- numregisters, tab_reg);
-
- if (rc == -1) {
- fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno));
- //return -1;
- rc = modbus_read_input_registers(ctx, startaddress,
- numregisters, tab_reg);
- }
+ int retry = 0;
+ do {
+ switch (registertype) {
+
+ case 3:
+ rc = modbus_read_registers(ctx, startaddress,
+ numregisters, tab_reg);
+ break;
+
+ case 4:
+ rc = modbus_read_input_registers(ctx, startaddress,
+ numregisters, tab_reg);
+ break;
+
+ default:
+ rc = modbus_read_registers(ctx, startaddress,
+ numregisters, tab_reg);
+ break;
+ }
+ if (rc == -1) {
+ fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno));
+ }
+ retry++;
+ } while ((rc != -1) && (retry < 2));
//MODBUS_GET_HIGH_BYTE(data);
//MODBUS_GET_LOW_BYTE(data);
@@ -212,7 +232,8 @@ int main(int argc, char *argv[]) {
char filename[50];
- sprintf(filename, "/var/modbuslog/%s_%04i_%02i_%02i_%02i_%02i_%02i.log",
+ sprintf(filename,
+ "/var/modbuslog/%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);
@@ -260,22 +281,22 @@ int main(int argc, char *argv[]) {
struct tm utc = *gmtime(&unixtime_min);
struct tm lc = *localtime(&unixtime_min);
-
int intervalid;
- FILE *intervalfile = fopen("/var/modbuslog/interval.txt", "w+");
+ FILE *intervalfile = fopen("/var/modbuslog/interval.txt", "r+");
fscanf(intervalfile, "%d", &intervalid);
intervalid++;
+ rewind(intervalfile);
fprintf(intervalfile, "%d", intervalid);
fclose(intervalfile);
-
-
+
fprintf(
filehandle,
"%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%i\n",
- intervalid,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);
+ intervalid, 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);