aboutsummaryrefslogtreecommitdiffstats
path: root/modbuslog.c
blob: 6d14ce16174a8750e90b9018d37d809c413e768f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* 
 * modbuslog
 *
 * Copyright (C) 2011 Jonathan McCrohan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// gcc modbuslog.c -o modbuslog `pkg-config --libs --cflags libmodbus libconfig`
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/select.h>
#include <signal.h>

#include <libconfig.h>
#include <modbus.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <string.h>

#define SLAVE_ID 1
#define START_ADDRESS 0
#define NUMBER_REGISTERS 14
#define DEBUG 1

void minute_check(int signum) {
	alarm(60);
}

void find_mac(unsigned char* mac_address) {
	int s;
	struct ifreq ifr;

	s = socket(PF_INET, SOCK_DGRAM, 0);
	memset(&ifr, 0x00, sizeof(ifr));
	strcpy(ifr.ifr_name, "eth0");
	ioctl(s, SIOCGIFHWADDR, &ifr);
	close(s);

	unsigned char mac_address_internal[6];
	sprintf(mac_address_internal, "%.2X%.2X%.2X%.2X%.2X%.2X",
			(unsigned char) ifr.ifr_hwaddr.sa_data[0],
			(unsigned char) ifr.ifr_hwaddr.sa_data[1],
			(unsigned char) ifr.ifr_hwaddr.sa_data[2],
			(unsigned char) ifr.ifr_hwaddr.sa_data[3],
			(unsigned char) ifr.ifr_hwaddr.sa_data[4],
			(unsigned char) ifr.ifr_hwaddr.sa_data[5]);

	strcpy(mac_address, mac_address_internal);
}

int main(int argc, char *argv[]) {

	const char *CONFIG_FILE = "modbuslog.cfg";

	config_t cfg;
	//config_setting_t *setting;

	unsigned char mac_address[6];

	const char *modbus_device_address;
	int modbus_baud_rate;
	int modbus_data_bits;
	const char *modbus_parity;
	int modbus_stop_bits;

	config_init(&cfg);

	find_mac(mac_address);

	if (!config_read_file(&cfg, CONFIG_FILE)) {
		fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
				config_error_line(&cfg), config_error_text(&cfg));
		config_destroy(&cfg);
		fprintf(stderr, "Unable to find CONFIG_FILE.\n");
		return -1;
	}

	if (!(config_lookup_string(&cfg, "modbus.device", &modbus_device_address)
			&& config_lookup_int(&cfg, "modbus.baud", &modbus_baud_rate)
			&& config_lookup_int(&cfg, "modbus.data_bits", &modbus_data_bits)
			&& config_lookup_string(&cfg, "modbus.parity", &modbus_parity)
			&& config_lookup_int(&cfg, "modbus.stop_bits", &modbus_stop_bits))) {
		fprintf(stderr,
				"Incomplete modbus configuration. Check configuration file.\n");
		return -1;
	}

	if (DEBUG) {
		printf("%s %s %d %c %d %d\n", mac_address, modbus_device_address,
				modbus_baud_rate, modbus_parity[0], modbus_data_bits,
				modbus_stop_bits);
	}

	modbus_t *ctx;
	uint16_t tab_reg[64];
	int rc;
	int i;

	signal(SIGALRM, minute_check);
	alarm(60);

	for (;;) {
		select(0, NULL, NULL, NULL, NULL);

		time_t t = time(NULL);
		int unixtime_min = (((int) t) / 60 * 60);
		//int unixtime = (int) t;
		//printf("%d\n", unixtime);

		config_setting_t *registers;

		registers = config_lookup(&cfg, "register");

		unsigned int num_registers = config_setting_length(registers);

		int i;
		for (i = 0; i < num_registers; ++i) {
			config_setting_t *register_element = config_setting_get_elem(
					registers, i);
			int slaveid;
			config_setting_lookup_int(register_element, "slaveid", &slaveid);
			int intervalvalue;
			config_setting_lookup_int(register_element, "intervalvalue",
					&intervalvalue);
			//printf("%d", intervalvalue);

			int startaddress;
			config_setting_lookup_int(register_element, "startaddress",
					&startaddress);

			int numregisters;
			config_setting_lookup_int(register_element, "numregisters",
					&numregisters);

			const char *intervalunit;
			config_setting_lookup_string(register_element, "intervalunit",
					&intervalunit);

			//printf("%c", intervalunit[0]);

			int intervalduration;

			if (intervalunit[0] == 'H')
				intervalduration = intervalvalue * 3600;
			else
				intervalduration = intervalvalue * 60;

			if (unixtime_min % intervalduration == 0) {
				//printf("specified minute.  %d %d\n", unixtime_min,
				//		intervalduration);

				ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate,
						modbus_parity[0], modbus_data_bits, modbus_stop_bits);

				modbus_set_slave(ctx, slaveid);
				modbus_set_debug(ctx, DEBUG);

				if (ctx == NULL) {
					fprintf(stderr, "Unable to create libmodbus object.\n");
					//return -1;
				}

				if (modbus_connect(ctx) == -1) {
					fprintf(stderr, "Connection failed: %s\n",
							modbus_strerror(errno));
					modbus_free(ctx);
					//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;
				}

				for (i = 0; i < rc; i = i + 2) {
					printf("reg[%d]=%d\n", i, tab_reg[i] + tab_reg[i + 1]);
				}

				modbus_close(ctx);
				modbus_free(ctx);

				//return 0;

			}

			//printf("%d ", slaveid);
		}
		//printf("%d\n", unixtime_min);
	}
}