2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013, 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */
25 #define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */
26 #define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0"
27 #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0"
28 #define VENDOR_GMC "Gossen Metrawatt"
30 static const uint32_t scanopts[] = {
35 static const uint32_t drvopts[] = {
37 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
40 /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */
41 static const uint32_t devopts_sm[] = {
43 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
47 /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */
48 static const uint32_t devopts_bd[] = {
50 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
51 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
52 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
56 * - For the 29S SR_CONF_ENERGYMETER, too.
57 * - SR_CONF_PATTERN_MODE for some 2x devices
58 * - SR_CONF_DATALOG for 22M, 26M, 29S and storage adaptors.
59 * Need to implement device-specific lists.
63 * Read single byte from serial port.
65 * @retval -1 Timeout or error.
68 static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
74 rc = serial_read_nonblocking(serial, &result, 1);
76 sr_spew("read: 0x%02x/%d", result, result);
79 if (g_get_monotonic_time() > timeout)
86 * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
88 * @param serial Configured, open serial port.
90 * @retval NULL Detection failed.
91 * @retval other Model code.
93 static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
95 int byte, bytecnt, cnt;
99 model = METRAHIT_NONE;
100 timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000);
103 * Try to find message consisting of device code and several
104 * (at least 4) data bytes.
106 for (bytecnt = 0; bytecnt < 100; bytecnt++) {
107 byte = read_byte(serial, timeout_us);
108 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
110 if ((byte & MSGID_MASK) == MSGID_INF) {
111 if (!(model = gmc_decode_model_sm(byte & MSGC_MASK)))
113 /* Now expect (at least) 4 data bytes. */
114 for (cnt = 0; cnt < 4; cnt++) {
115 byte = read_byte(serial, timeout_us);
117 ((byte & MSGID_MASK) != MSGID_DATA))
119 model = METRAHIT_NONE;
132 * Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
135 * The older 1x models use 8192 baud and the newer 2x 9600 baud.
136 * The DMM usually sends up to about 20 messages per second. However, depending
137 * on configuration and measurement mode the intervals can be much larger and
138 * then the detection might not work.
140 static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
142 struct sr_dev_inst *sdi;
143 struct dev_context *devc;
144 struct sr_config *src;
145 struct sr_serial_dev_inst *serial;
147 const char *conn, *serialcomm;
149 gboolean serialcomm_given;
152 conn = serialcomm = NULL;
153 serialcomm_given = FALSE;
155 for (l = options; l; l = l->next) {
159 conn = g_variant_get_string(src->data, NULL);
161 case SR_CONF_SERIALCOMM:
162 serialcomm = g_variant_get_string(src->data, NULL);
163 serialcomm_given = TRUE;
170 serialcomm = SERIALCOMM_2X_RS232;
172 serial = sr_serial_dev_inst_new(conn, serialcomm);
174 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
175 sr_serial_dev_inst_free(serial);
179 serial_flush(serial);
181 model = scan_model_sm(serial);
184 * If detection failed and no user-supplied parameters,
185 * try second baud rate.
187 if ((model == METRAHIT_NONE) && !serialcomm_given) {
188 serialcomm = SERIALCOMM_1X_RS232;
189 g_free(serial->serialcomm);
190 serial->serialcomm = g_strdup(serialcomm);
191 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
192 serial_flush(serial);
193 model = scan_model_sm(serial);
197 if (model != METRAHIT_NONE) {
198 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
199 sdi = g_malloc0(sizeof(struct sr_dev_inst));
200 sdi->status = SR_ST_INACTIVE;
201 sdi->vendor = g_strdup(VENDOR_GMC);
202 sdi->model = g_strdup(gmc_model_str(model));
203 devc = g_malloc0(sizeof(struct dev_context));
204 sr_sw_limits_init(&devc->limits);
206 devc->settings_ok = FALSE;
209 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
210 devices = g_slist_append(devices, sdi);
213 return std_scan_complete(di, devices);
217 * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
218 * 'BD 232' interface.
220 static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
222 struct sr_dev_inst *sdi;
223 struct dev_context *devc;
224 struct sr_config *src;
225 struct sr_serial_dev_inst *serial;
227 const char *conn, *serialcomm;
233 conn = serialcomm = NULL;
236 for (l = options; l; l = l->next) {
240 conn = g_variant_get_string(src->data, NULL);
242 case SR_CONF_SERIALCOMM:
243 serialcomm = g_variant_get_string(src->data, NULL);
250 serialcomm = SERIALCOMM_2X;
252 serial = sr_serial_dev_inst_new(conn, serialcomm);
254 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
257 devc = g_malloc0(sizeof(struct dev_context));
259 sdi = g_malloc0(sizeof(struct sr_dev_inst));
260 sdi->status = SR_ST_INACTIVE;
261 sdi->vendor = g_strdup(VENDOR_GMC);
264 /* Send message 03 "Query multimeter version and status" */
266 if (req_stat14(sdi, TRUE) != SR_OK)
269 /* Wait for reply from device(s) for up to 2s. */
270 timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
272 while (timeout_us > g_get_monotonic_time()) {
273 /* Receive reply (14 bytes) */
275 for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
276 byte = read_byte(serial, timeout_us);
278 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
281 if (devc->buflen != GMC_REPLY_SIZE)
284 devc->addr = devc->buf[0];
288 if (devc->model != METRAHIT_NONE) {
289 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
290 sr_sw_limits_init(&devc->limits);
291 sdi->model = g_strdup(gmc_model_str(devc->model));
292 sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
295 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
296 devices = g_slist_append(devices, sdi);
297 devc = g_malloc0(sizeof(struct dev_context));
298 sdi = g_malloc0(sizeof(struct sr_dev_inst));
299 sdi->status = SR_ST_INACTIVE;
300 sdi->vendor = g_strdup(VENDOR_GMC);
304 /* Free last alloc if no device found */
305 if (devc->model == METRAHIT_NONE) {
307 sr_dev_inst_free(sdi);
310 return std_scan_complete(di, devices);
313 sr_serial_dev_inst_free(serial);
315 sr_dev_inst_free(sdi);
320 static int dev_close(struct sr_dev_inst *sdi)
322 struct dev_context *devc;
326 devc->model = METRAHIT_NONE;
328 return std_serial_dev_close(sdi);
331 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
332 const struct sr_channel_group *cg)
334 struct dev_context *devc;
344 case SR_CONF_LIMIT_SAMPLES:
345 case SR_CONF_LIMIT_MSEC:
346 return sr_sw_limits_config_get(&devc->limits, key, data);
347 case SR_CONF_POWER_OFF:
348 *data = g_variant_new_boolean(FALSE);
357 /** Implementation of config_list for Metrahit 1x/2x send mode */
358 static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
359 const struct sr_channel_group *cg)
361 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_sm);
364 /** Implementation of config_list for Metrahit 2x bidirectional mode */
365 static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
366 const struct sr_channel_group *cg)
368 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_bd);
371 static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
373 struct dev_context *devc;
374 struct sr_serial_dev_inst *serial;
377 devc->settings_ok = FALSE;
380 sr_sw_limits_acquisition_start(&devc->limits);
381 std_session_send_df_header(sdi);
384 serial_source_add(sdi->session, serial, G_IO_IN, 40,
385 gmc_mh_1x_2x_receive_data, (void *)sdi);
390 static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
392 struct dev_context *devc;
393 struct sr_serial_dev_inst *serial;
396 devc->settings_ok = FALSE;
399 sr_sw_limits_acquisition_start(&devc->limits);
400 std_session_send_df_header(sdi);
403 serial_source_add(sdi->session, serial, G_IO_IN, 40,
404 gmc_mh_2x_receive_data, (void *)sdi);
406 /* Send start message */
407 return req_meas14(sdi);
410 static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
411 .name = "gmc-mh-1x-2x-rs232",
412 .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
415 .cleanup = std_cleanup,
416 .scan = scan_1x_2x_rs232,
417 .dev_list = std_dev_list,
418 .dev_clear = std_dev_clear,
419 .config_get = config_get,
420 .config_set = config_set,
421 .config_list = config_list_sm,
422 .dev_open = std_serial_dev_open,
423 .dev_close = dev_close,
424 .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
425 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
428 SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
430 static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
431 .name = "gmc-mh-2x-bd232",
432 .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
435 .cleanup = std_cleanup,
436 .scan = scan_2x_bd232,
437 .dev_list = std_dev_list,
438 .dev_clear = std_dev_clear,
439 .config_get = config_get,
440 .config_set = config_set,
441 .config_list = config_list_bd,
442 .dev_open = std_serial_dev_open,
443 .dev_close = dev_close,
444 .dev_acquisition_start = dev_acquisition_start_2x_bd232,
445 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
448 SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);