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
31 #include <sys/ioctl.h>
34 #error "No applicable input method."
39 #define LINE_LENGTH 14
40 static int multimeter_read_value(double *value) {
41 int retry = 3; /* sometimes we receive garbadge */
44 struct timeval time_end;
46 tcflush(fd, TCIFLUSH);
48 if (gettimeofday(&time_end, NULL) < 0) {
50 ERROR("multimeter plugin: gettimeofday failed: %s",
51 sstrerror(errno, errbuf, sizeof(errbuf)));
57 char buf[LINE_LENGTH];
61 struct timeval timeout;
62 struct timeval time_now;
64 status = swrite(fd, "D", 1);
66 ERROR("multimeter plugin: swrite failed.");
73 if (gettimeofday(&time_now, NULL) < 0) {
75 ERROR("multimeter plugin: "
76 "gettimeofday failed: %s",
77 sstrerror(errno, errbuf, sizeof(errbuf)));
80 if (timeval_cmp(time_end, time_now, &timeout) < 0)
83 status = select(fd + 1, &rfds, NULL, NULL, &timeout);
85 if (status > 0) /* usually we succeed */
87 status = read(fd, buf, LINE_LENGTH);
89 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
92 /* Format: "DC 00.000mV \r" */
93 if (status > 0 && status == LINE_LENGTH) {
94 *value = strtod(buf + 2, &range);
96 if (range > (buf + 6)) {
123 return (-1); /* Overflow */
125 return (0); /* value received */
128 } else if (!status) /* Timeout */
131 } else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR))) {
133 } else /* status == -1 */
136 ERROR("multimeter plugin: "
138 sstrerror(errno, errbuf, sizeof(errbuf)));
144 return (-2); /* no value received */
145 } /* int multimeter_read_value */
147 static int multimeter_init(void) {
148 char device[] = "/dev/ttyS ";
150 for (int i = 0; i < 10; i++) {
151 device[strlen(device) - 1] = i + '0';
153 if ((fd = open(device, O_RDWR | O_NOCTTY)) != -1) {
154 struct termios tios = {0};
158 tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
159 tios.c_iflag = IGNBRK | IGNPAR;
162 tios.c_cc[VTIME] = 3;
163 tios.c_cc[VMIN] = LINE_LENGTH;
165 tcflush(fd, TCIFLUSH);
166 tcsetattr(fd, TCSANOW, &tios);
167 ioctl(fd, TIOCMBIC, &rts);
169 if (multimeter_read_value(&value) < -1) {
173 INFO("multimeter plugin: Device "
181 ERROR("multimeter plugin: No device found");
186 static void multimeter_submit(double value) {
187 value_list_t vl = VALUE_LIST_INIT;
189 vl.values = &(value_t){.gauge = value};
191 sstrncpy(vl.plugin, "multimeter", sizeof(vl.plugin));
192 sstrncpy(vl.type, "multimeter", sizeof(vl.type));
194 plugin_dispatch_values(&vl);
197 static int multimeter_read(void) {
203 if (multimeter_read_value(&value) != 0)
206 multimeter_submit(value);
208 } /* int multimeter_read */
210 static int multimeter_shutdown(void) {
219 void module_register(void) {
220 plugin_register_init("multimeter", multimeter_init);
221 plugin_register_read("multimeter", multimeter_read);
222 plugin_register_shutdown("multimeter", multimeter_shutdown);
223 } /* void module_register */