aboutsummaryrefslogtreecommitdiffstats
path: root/modbuslog.c
diff options
context:
space:
mode:
Diffstat (limited to 'modbuslog.c')
-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);