aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-11-14 01:09:14 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2011-11-14 01:09:14 +0000
commit882643c1377fb19a3f77dcb6ab250bc3a27dbb08 (patch)
tree7ebe0bac58651ade9f10092c0e4ddbd091b72bab
parentb784ec7d4d0b45c548b161ae83a3590a7fa91ff3 (diff)
downloadverteco-882643c1377fb19a3f77dcb6ab250bc3a27dbb08.tar.gz
Corrected byte readings.
-rw-r--r--modbuslog.c41
-rw-r--r--modbuslog.cfg46
2 files changed, 70 insertions, 17 deletions
diff --git a/modbuslog.c b/modbuslog.c
index f9c85e4..c2b34b7 100644
--- a/modbuslog.c
+++ b/modbuslog.c
@@ -18,7 +18,6 @@
*/
// gcc modbuslog.c -o modbuslog `pkg-config --libs --cflags libmodbus libconfig`
-
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
@@ -116,7 +115,8 @@ int main(int argc, char *argv[]) {
}
modbus_t *ctx;
- uint16_t tab_reg[64];
+ uint16_t tab_reg[4];
+ uint8_t byte[8];
int rc;
int i;
@@ -131,16 +131,16 @@ int main(int argc, char *argv[]) {
//int unixtime = (int) t;
//printf("%d\n", unixtime);
- config_setting_t *registers;
+ config_setting_t *readings;
- registers = config_lookup(&cfg, "register");
+ readings = config_lookup(&cfg, "reading");
- unsigned int num_registers = config_setting_length(registers);
+ unsigned int num_readings = config_setting_length(readings);
int i;
- for (i = 0; i < num_registers; ++i) {
+ for (i = 0; i < num_readings; ++i) {
config_setting_t *register_element = config_setting_get_elem(
- registers, i);
+ readings, i);
int slaveid;
config_setting_lookup_int(register_element, "slaveid", &slaveid);
int intervalvalue;
@@ -201,6 +201,10 @@ int main(int argc, char *argv[]) {
numregisters, tab_reg);
}
+ //MODBUS_GET_HIGH_BYTE(data);
+ //MODBUS_GET_LOW_BYTE(data);
+ //MODBUS_SET_INT16_TO_INT8(tab_int8, index, value);
+
//<mac address>_YYYY_MM_DD_HH_MM_SS.log
time_t unixtime_day = ((((int) t) / 86400 * 86400) + 86400);
@@ -229,11 +233,28 @@ int main(int argc, char *argv[]) {
FILE *filehandle = fopen(filename, "a+");
- uint16_t registervalue = 0;
+ int registervalue = 0;
int p;
- for (p = 0; p < numregisters; p = p + 2) {
- registervalue += tab_reg[i] + tab_reg[i + 1];
+ for (p = 0; p < numregisters; p++) {
+ MODBUS_SET_INT16_TO_INT8(byte, p * 2, tab_reg[p]);
+ //registervalue += tab_reg[i] + tab_reg[i + 1];
+ }
+
+ switch (numregisters) {
+ case 1:
+ registervalue = byte[0];
+ break;
+ case 2:
+ registervalue = ((byte[0]) << 8) + byte[1];
+ break;
+ case 4:
+ registervalue = ((byte[0]) << 24) + ((byte[1]) << 16)
+ + ((byte[2]) << 8) + byte[3];
+ break;
+ default:
+ registervalue = byte[0];
+ break;
}
struct tm utc = *gmtime(&unixtime_min);
diff --git a/modbuslog.cfg b/modbuslog.cfg
index 778b6f4..2451b3f 100644
--- a/modbuslog.cfg
+++ b/modbuslog.cfg
@@ -3,14 +3,14 @@
cfg_version = 0.99;
modbus = {
- device = "/dev/ttyUSB1";
+ device = "/dev/ttyUSB2";
baud = 19200;
data_bits = 8;
parity = "E"
stop_bits = 1;
};
-register = (
+reading= (
{
slaveid = 1;
registertype = 4;
@@ -22,17 +22,49 @@ register = (
{
slaveid = 1;
registertype = 4;
- startaddress = 8;
- numregisters = 4;
+ startaddress = 2;
+ numregisters = 2;
intervalvalue = 1;
intervalunit = "m";
},
{
slaveid = 1;
registertype = 4;
- startaddress = 13;
- numregisters = 1;
- intervalvalue = 2;
+ startaddress = 4;
+ numregisters = 2;
+ intervalvalue = 1;
+ intervalunit = "m";
+ },
+ {
+ slaveid = 1;
+ registertype = 4;
+ startaddress = 6;
+ numregisters = 2;
+ intervalvalue = 1;
+ intervalunit = "m";
+ },
+ {
+ slaveid = 1;
+ registertype = 4;
+ startaddress = 8;
+ numregisters = 2;
+ intervalvalue = 1;
+ intervalunit = "m";
+ },
+ {
+ slaveid = 1;
+ registertype = 4;
+ startaddress = 10;
+ numregisters = 2;
+ intervalvalue = 1;
+ intervalunit = "m";
+ },
+ {
+ slaveid = 1;
+ registertype = 4;
+ startaddress = 14;
+ numregisters = 2;
+ intervalvalue = 1;
intervalunit = "m";
}
);