aboutsummaryrefslogtreecommitdiffstats
path: root/src/modbuslog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/modbuslog.c')
-rw-r--r--src/modbuslog.c87
1 files changed, 63 insertions, 24 deletions
diff --git a/src/modbuslog.c b/src/modbuslog.c
index 3f9b658..da03a11 100644
--- a/src/modbuslog.c
+++ b/src/modbuslog.c
@@ -197,6 +197,10 @@ int main(int argc, char *argv[]) {
config_setting_lookup_int(register_element, "numbytes",
&numbytes);
+ int signedvalue;
+ config_setting_lookup_int(register_element, "signedvalue",
+ &signedvalue);
+
const char *intervalunit;
config_setting_lookup_string(register_element, "intervalunit",
&intervalunit);
@@ -340,7 +344,7 @@ int main(int argc, char *argv[]) {
syslog(LOG_DEBUG, "opening file for append: [%s]",
log_filename);
- int32_t registervalue = 0;
+ uint32_t registervalue;
int p;
@@ -353,20 +357,32 @@ int main(int argc, char *argv[]) {
//registervalue += tab_reg[i] + tab_reg[i + 1];
}
+ // initialise register value
+ // need to check if 2s complement
+ if (signedvalue && (byte[0]>>7)){
+ registervalue = 0xffffffff;
+ } else{
+ registervalue = 0;
+ }
+
// bitshifting magic
switch (numbytes) {
case 1:
- registervalue = (startbyte % 2 == 0) ? byte[0] : byte[1];
+ registervalue <<= 8;
+ registervalue |= ((startbyte % 2 == 0) ? byte[0] : byte[1]);
break;
case 2:
- registervalue = ((byte[0]) << 8) + byte[1];
+ registervalue <<= 16;
+ registervalue |= (((byte[0]) << 8) + byte[1]);
break;
case 4:
- registervalue = ((byte[0]) << 24) + ((byte[1]) << 16)
- + ((byte[2]) << 8) + byte[3];
+ registervalue <<= 24;
+ registervalue |= (((byte[0]) << 24) + ((byte[1]) << 16)
+ + ((byte[2]) << 8) + byte[3]);
break;
default:
- registervalue = ((byte[0]) << 8) + byte[1];
+ registervalue <<= 16;
+ registervalue |= (((byte[0]) << 8) + byte[1]);
break;
}
@@ -409,24 +425,47 @@ int main(int argc, char *argv[]) {
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, startbyte,
- registervalue);
- fclose(filehandle);
-
- syslog(
- LOG_DEBUG,
- "[%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, startbyte,
- registervalue);
+ if (signedvalue){
+
+ 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, startbyte,
+ registervalue);
+ fclose(filehandle);
+
+ syslog(
+ LOG_DEBUG,
+ "[%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, startbyte,
+ registervalue);
+ } else{
+ fprintf(
+ filehandle,
+ "%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%u\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, startbyte,
+ registervalue);
+ fclose(filehandle);
+
+ syslog(
+ LOG_DEBUG,
+ "[%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%u]\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, startbyte,
+ registervalue);
+ }
+
modbus_close(ctx);
modbus_free(ctx);