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/>.
21 * Gossen Metrawatt Metrahit 1x/2x drivers
29 /* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */
30 #define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */
31 #define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0"
32 #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0"
33 #define VENDOR_GMC "Gossen Metrawatt"
35 static const uint32_t scanopts[] = {
40 /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */
41 static const uint32_t devopts_sm[] = {
43 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
45 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
49 /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */
50 static const uint32_t devopts_bd[] = {
52 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
54 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
55 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
56 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
60 * - For the 29S SR_CONF_ENERGYMETER, too.
61 * - SR_CONF_PATTERN_MODE for some 2x devices
62 * - SR_CONF_DATALOG for 22M, 26M, 29S and storage adaptors.
63 * Need to implement device-specific lists.
67 * Read single byte from serial port.
69 * @retval -1 Timeout or error.
72 static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
78 rc = serial_read_nonblocking(serial, &result, 1);
80 sr_spew("read: 0x%02x/%d", result, result);
83 if (g_get_monotonic_time() > timeout)
90 * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
92 * @param serial Configured, open serial port.
94 * @retval NULL Detection failed.
95 * @retval other Model code.
97 static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
99 int byte, bytecnt, cnt;
103 model = METRAHIT_NONE;
104 timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000);
107 * Try to find message consisting of device code and several
108 * (at least 4) data bytes.
110 for (bytecnt = 0; bytecnt < 100; bytecnt++) {
111 byte = read_byte(serial, timeout_us);
112 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
114 if ((byte & MSGID_MASK) == MSGID_INF) {
115 if (!(model = gmc_decode_model_sm(byte & MSGC_MASK)))
117 /* Now expect (at least) 4 data bytes. */
118 for (cnt = 0; cnt < 4; cnt++) {
119 byte = read_byte(serial, timeout_us);
121 ((byte & MSGID_MASK) != MSGID_DATA))
123 model = METRAHIT_NONE;
136 * Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
139 * The older 1x models use 8192 baud and the newer 2x 9600 baud.
140 * The DMM usually sends up to about 20 messages per second. However, depending
141 * on configuration and measurement mode the intervals can be much larger and
142 * then the detection might not work.
144 static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
146 struct sr_dev_inst *sdi;
147 struct dev_context *devc;
148 struct sr_config *src;
149 struct sr_serial_dev_inst *serial;
151 const char *conn, *serialcomm;
153 gboolean serialcomm_given;
156 conn = serialcomm = NULL;
157 serialcomm_given = FALSE;
159 sr_spew("scan_1x_2x_rs232() called!");
161 for (l = options; l; l = l->next) {
165 conn = g_variant_get_string(src->data, NULL);
167 case SR_CONF_SERIALCOMM:
168 serialcomm = g_variant_get_string(src->data, NULL);
169 serialcomm_given = TRUE;
176 serialcomm = SERIALCOMM_2X_RS232;
178 serial = sr_serial_dev_inst_new(conn, serialcomm);
180 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
181 sr_serial_dev_inst_free(serial);
185 serial_flush(serial);
187 model = scan_model_sm(serial);
190 * If detection failed and no user-supplied parameters,
191 * try second baud rate.
193 if ((model == METRAHIT_NONE) && !serialcomm_given) {
194 serialcomm = SERIALCOMM_1X_RS232;
195 g_free(serial->serialcomm);
196 serial->serialcomm = g_strdup(serialcomm);
197 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
198 serial_flush(serial);
199 model = scan_model_sm(serial);
203 if (model != METRAHIT_NONE) {
204 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
205 sdi = g_malloc0(sizeof(struct sr_dev_inst));
206 sdi->status = SR_ST_INACTIVE;
207 sdi->vendor = g_strdup(VENDOR_GMC);
208 sdi->model = g_strdup(gmc_model_str(model));
209 devc = g_malloc0(sizeof(struct dev_context));
210 sr_sw_limits_init(&devc->limits);
212 devc->settings_ok = FALSE;
215 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
216 devices = g_slist_append(devices, sdi);
219 return std_scan_complete(di, devices);
223 * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
224 * 'BD 232' interface.
226 static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
228 struct sr_dev_inst *sdi;
229 struct dev_context *devc;
230 struct sr_config *src;
231 struct sr_serial_dev_inst *serial;
233 const char *conn, *serialcomm;
239 conn = serialcomm = NULL;
242 sr_spew("scan_2x_bd232() called!");
244 for (l = options; l; l = l->next) {
248 conn = g_variant_get_string(src->data, NULL);
250 case SR_CONF_SERIALCOMM:
251 serialcomm = g_variant_get_string(src->data, NULL);
258 serialcomm = SERIALCOMM_2X;
260 serial = sr_serial_dev_inst_new(conn, serialcomm);
262 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
265 devc = g_malloc0(sizeof(struct dev_context));
267 sdi = g_malloc0(sizeof(struct sr_dev_inst));
268 sdi->status = SR_ST_INACTIVE;
269 sdi->vendor = g_strdup(VENDOR_GMC);
272 /* Send message 03 "Query multimeter version and status" */
274 if (req_stat14(sdi, TRUE) != SR_OK)
277 /* Wait for reply from device(s) for up to 2s. */
278 timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
280 while (timeout_us > g_get_monotonic_time()) {
281 /* Receive reply (14 bytes) */
283 for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
284 byte = read_byte(serial, timeout_us);
286 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
289 if (devc->buflen != GMC_REPLY_SIZE)
292 devc->addr = devc->buf[0];
296 if (devc->model != METRAHIT_NONE) {
297 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
298 sr_sw_limits_init(&devc->limits);
299 sdi->model = g_strdup(gmc_model_str(devc->model));
300 sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
303 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
304 devices = g_slist_append(devices, sdi);
305 devc = g_malloc0(sizeof(struct dev_context));
306 sdi = g_malloc0(sizeof(struct sr_dev_inst));
307 sdi->status = SR_ST_INACTIVE;
308 sdi->vendor = g_strdup(VENDOR_GMC);
312 /* Free last alloc if no device found */
313 if (devc->model == METRAHIT_NONE) {
315 sr_dev_inst_free(sdi);
318 return std_scan_complete(di, devices);
321 sr_info("scan_2x_bd232(): Error!");
324 sr_serial_dev_inst_free(serial);
327 sr_dev_inst_free(sdi);
332 static int dev_close(struct sr_dev_inst *sdi)
334 struct dev_context *devc;
336 std_serial_dev_close(sdi);
338 sdi->status = SR_ST_INACTIVE;
339 if ((devc = sdi->priv))
340 devc->model = METRAHIT_NONE;
345 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
346 const struct sr_channel_group *cg)
349 struct dev_context *devc;
360 case SR_CONF_LIMIT_SAMPLES:
361 case SR_CONF_LIMIT_MSEC:
362 return sr_sw_limits_config_get(&devc->limits, key, data);
363 case SR_CONF_POWER_OFF:
364 *data = g_variant_new_boolean(FALSE);
373 /** Implementation of config_list, auxiliary function for common parts. */
374 static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
375 const struct sr_channel_group *cg)
381 case SR_CONF_SCAN_OPTIONS:
382 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
383 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
392 /** Implementation of config_list for Metrahit 1x/2x send mode */
393 static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
394 const struct sr_channel_group *cg)
397 case SR_CONF_DEVICE_OPTIONS:
398 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
399 devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t));
402 return config_list_common(key, data, sdi, cg);
408 /** Implementation of config_list for Metrahit 2x bidirectional mode */
409 static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
410 const struct sr_channel_group *cg)
413 case SR_CONF_DEVICE_OPTIONS:
414 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
415 devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t));
418 return config_list_common(key, data, sdi, cg);
424 static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
426 struct dev_context *devc;
427 struct sr_serial_dev_inst *serial;
429 if (sdi->status != SR_ST_ACTIVE)
430 return SR_ERR_DEV_CLOSED;
433 devc->settings_ok = FALSE;
436 sr_sw_limits_acquisition_start(&devc->limits);
437 std_session_send_df_header(sdi);
439 /* Poll every 40ms, or whenever some data comes in. */
441 serial_source_add(sdi->session, serial, G_IO_IN, 40,
442 gmc_mh_1x_2x_receive_data, (void *)sdi);
447 static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
449 struct dev_context *devc;
450 struct sr_serial_dev_inst *serial;
452 if (sdi->status != SR_ST_ACTIVE)
453 return SR_ERR_DEV_CLOSED;
456 devc->settings_ok = FALSE;
459 sr_sw_limits_acquisition_start(&devc->limits);
460 std_session_send_df_header(sdi);
462 /* Poll every 40ms, or whenever some data comes in. */
464 serial_source_add(sdi->session, serial, G_IO_IN, 40,
465 gmc_mh_2x_receive_data, (void *)sdi);
467 /* Send start message */
468 return req_meas14(sdi);
471 static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
472 .name = "gmc-mh-1x-2x-rs232",
473 .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
476 .cleanup = std_cleanup,
477 .scan = scan_1x_2x_rs232,
478 .dev_list = std_dev_list,
480 .config_get = config_get,
481 .config_set = config_set,
482 .config_list = config_list_sm,
483 .dev_open = std_serial_dev_open,
484 .dev_close = dev_close,
485 .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
486 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
489 SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
491 static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
492 .name = "gmc-mh-2x-bd232",
493 .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
496 .cleanup = std_cleanup,
497 .scan = scan_2x_bd232,
498 .dev_list = std_dev_list,
500 .config_get = config_get,
501 .config_set = config_set,
502 .config_list = config_list_bd,
503 .dev_open = std_serial_dev_open,
504 .dev_close = dev_close,
505 .dev_acquisition_start = dev_acquisition_start_2x_bd232,
506 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
509 SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);