aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2012-05-20 19:15:37 +0100
committerJonathan McCrohan <jmccrohan@gmail.com>2012-05-20 23:25:03 +0100
commit96117e05c387c930c9619e3ae5c0ad0b4da196d2 (patch)
tree5a64ad590d0e7d28d0469136bdca8034332c9699
parent41c3d98e8951107970a35f6104e1cbf15cd87ada (diff)
downloadverteco-96117e05c387c930c9619e3ae5c0ad0b4da196d2.tar.gz
modbuslog: Add support for signed registers
-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);