2 * collectd - src/multimeter.c
3 * Copyright (C) 2005,2006 Peter Holik
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Peter Holik <peter at holik.at>
22 * Used multimeter: Metex M-4650CR
30 #if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
32 #include <sys/ioctl.h>
35 #error "No applicable input method."
40 #define LINE_LENGTH 14
41 static int multimeter_read_value(double *value) {
42 int retry = 3; /* sometimes we receive garbadge */
45 struct timeval time_end;
47 tcflush(fd, TCIFLUSH);
49 if (gettimeofday(&time_end, NULL) < 0) {
51 ERROR("multimeter plugin: gettimeofday failed: %s",
52 sstrerror(errno, errbuf, sizeof(errbuf)));
58 char buf[LINE_LENGTH];
62 struct timeval timeout;
63 struct timeval time_now;
65 status = swrite(fd, "D", 1);
67 ERROR("multimeter plugin: swrite failed.");
74 if (gettimeofday(&time_now, NULL) < 0) {
76 ERROR("multimeter plugin: "
77 "gettimeofday failed: %s",
78 sstrerror(errno, errbuf, sizeof(errbuf)));
81 if (timeval_cmp(time_end, time_now, &timeout) < 0)
84 status = select(fd + 1, &rfds, NULL, NULL, &timeout);
86 if (status > 0) /* usually we succeed */
88 status = read(fd, buf, LINE_LENGTH);
90 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
93 /* Format: "DC 00.000mV \r" */
94 if (status > 0 && status == LINE_LENGTH) {
95 *value = strtod(buf + 2, &range);
97 if (range > (buf + 6)) {
124 return (-1); /* Overflow */
126 return (0); /* value received */
129 } else if (!status) /* Timeout */
132 } else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR))) {
134 } else /* status == -1 */
137 ERROR("multimeter plugin: "
139 sstrerror(errno, errbuf, sizeof(errbuf)));
145 return (-2); /* no value received */
146 } /* int multimeter_read_value */
148 static int multimeter_init(void) {
149 char device[] = "/dev/ttyS ";
151 for (int i = 0; i < 10; i++) {
152 device[strlen(device) - 1] = i + '0';
154 if ((fd = open(device, O_RDWR | O_NOCTTY)) != -1) {
155 struct termios tios = {0};
159 tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
160 tios.c_iflag = IGNBRK | IGNPAR;
163 tios.c_cc[VTIME] = 3;
164 tios.c_cc[VMIN] = LINE_LENGTH;
166 tcflush(fd, TCIFLUSH);
167 tcsetattr(fd, TCSANOW, &tios);
168 ioctl(fd, TIOCMBIC, &rts);
170 if (multimeter_read_value(&value) < -1) {
174 INFO("multimeter plugin: Device "
182 ERROR("multimeter plugin: No device found");
187 static void multimeter_submit(double value) {
188 value_list_t vl = VALUE_LIST_INIT;
190 vl.values = &(value_t){.gauge = value};
192 sstrncpy(vl.plugin, "multimeter", sizeof(vl.plugin));
193 sstrncpy(vl.type, "multimeter", sizeof(vl.type));
195 plugin_dispatch_values(&vl);
198 static int multimeter_read(void) {
204 if (multimeter_read_value(&value) != 0)
207 multimeter_submit(value);
209 } /* int multimeter_read */
211 static int multimeter_shutdown(void) {
220 void module_register(void) {
221 plugin_register_init("multimeter", multimeter_init);
222 plugin_register_read("multimeter", multimeter_read);
223 plugin_register_shutdown("multimeter", multimeter_shutdown);
224 } /* void module_register */