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)
43 int retry = 3; /* sometimes we receive garbadge */
47 struct timeval time_end;
49 tcflush(fd, TCIFLUSH);
51 if (gettimeofday (&time_end, NULL) < 0)
54 ERROR ("multimeter plugin: gettimeofday failed: %s",
55 sstrerror (errno, errbuf,
63 char buf[LINE_LENGTH];
67 struct timeval timeout;
68 struct timeval time_now;
70 status = swrite (fd, "D", 1);
73 ERROR ("multimeter plugin: swrite failed.");
80 if (gettimeofday (&time_now, NULL) < 0)
83 ERROR ("multimeter plugin: "
84 "gettimeofday failed: %s",
85 sstrerror (errno, errbuf,
89 if (timeval_cmp (time_end, time_now, &timeout) < 0)
92 status = select(fd+1, &rfds, NULL, NULL, &timeout);
94 if (status > 0) /* usually we succeed */
96 status = read(fd, buf, LINE_LENGTH);
98 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
101 /* Format: "DC 00.000mV \r" */
102 if (status > 0 && status == LINE_LENGTH)
104 *value = strtod(buf + 2, &range);
106 if ( range > (buf + 6) )
112 case 'p': *value *= 1.0E-12; break;
113 case 'n': *value *= 1.0E-9; break;
114 case 'u': *value *= 1.0E-6; break;
115 case 'm': *value *= 1.0E-3; break;
116 case 'k': *value *= 1.0E3; break;
117 case 'M': *value *= 1.0E6; break;
118 case 'G': *value *= 1.0E9; break;
122 return (-1); /* Overflow */
124 return (0); /* value received */
128 else if (!status) /* Timeout */
132 else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
136 else /* status == -1 */
139 ERROR ("multimeter plugin: "
141 sstrerror (errno, errbuf, sizeof (errbuf)));
147 return (-2); /* no value received */
148 } /* int multimeter_read_value */
150 static int multimeter_init (void)
152 char device[] = "/dev/ttyS ";
154 for (int i = 0; i < 10; i++)
156 device[strlen(device)-1] = i + '0';
158 if ((fd = open(device, O_RDWR | O_NOCTTY)) != -1)
160 struct termios tios = { 0 };
164 tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
165 tios.c_iflag = IGNBRK | IGNPAR;
168 tios.c_cc[VTIME] = 3;
169 tios.c_cc[VMIN] = LINE_LENGTH;
171 tcflush(fd, TCIFLUSH);
172 tcsetattr(fd, TCSANOW, &tios);
173 ioctl(fd, TIOCMBIC, &rts);
175 if (multimeter_read_value (&value) < -1)
182 INFO ("multimeter plugin: Device "
183 "found at %s", device);
189 ERROR ("multimeter plugin: No device found");
194 static void multimeter_submit (double value)
196 value_list_t vl = VALUE_LIST_INIT;
198 vl.values = &(value_t) { .gauge = value };
200 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
201 sstrncpy (vl.plugin, "multimeter", sizeof (vl.plugin));
202 sstrncpy (vl.type, "multimeter", sizeof (vl.type));
204 plugin_dispatch_values (&vl);
207 static int multimeter_read (void)
214 if (multimeter_read_value (&value) != 0)
217 multimeter_submit (value);
219 } /* int multimeter_read */
221 static int multimeter_shutdown (void)
232 void module_register (void)
234 plugin_register_init ("multimeter", multimeter_init);
235 plugin_register_read ("multimeter", multimeter_read);
236 plugin_register_shutdown ("multimeter", multimeter_shutdown);
237 } /* void module_register */