2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 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 VENDOR_GMC "Gossen Metrawatt"
29 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
31 static const int32_t hwopts[] = {
36 static const int32_t hwcaps[] = {
38 SR_CONF_LIMIT_SAMPLES,
43 /** Driver init function */
44 static int init_1x_2x_rs232(struct sr_context *sr_ctx)
46 return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX);
50 /** Read single byte from serial port.
51 * \retval -1 Timeout or error.
54 static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
60 rc = serial_read(serial, &result, 1);
62 sr_spew("read: 0x%02x/%d", result, result);
65 if (g_get_monotonic_time() > timeout)
71 /** Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
72 * \param serial Configured, open serial port.
74 * \retval NULL Detection failed.
75 * \retval other Model code.
77 static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
79 int byte, bytecnt, cnt;
83 model = SR_METRAHIT_NONE;
84 timeout_us = g_get_monotonic_time() + 1*1000*1000;
86 /* Try to find message consisting of device code and several
87 * (at least 4) data bytes. */
88 for (bytecnt = 0; bytecnt < 100; bytecnt++) {
89 byte = read_byte(serial, timeout_us);
90 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
92 if ((byte & MSGID_MASK) == MSGID_INF) {
93 if (!(model = sr_gmc_decode_model_sm(byte & MSGC_MASK)))
95 /* Now expect (at least) 4 data bytes */
96 for (cnt = 0; cnt < 4; cnt++) {
97 byte = read_byte(serial, timeout_us);
99 ((byte & MSGID_MASK) != MSGID_DATA))
101 model = SR_METRAHIT_NONE;
113 /** Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
114 * 'RS232' interface. The older 1x models use 8192 and the newer 2x 9600 baud.
115 * The DMM usually sends up to about 20 messages per second. However, depending
116 * on configuration and measurement mode the intervals can be much larger and
117 * then the detection might not work.
119 static GSList *scan_1x_2x_rs232(GSList *options)
121 struct sr_dev_inst *sdi;
122 struct drv_context *drvc;
123 struct dev_context *devc;
124 struct sr_config *src;
125 struct sr_probe *probe;
126 struct sr_serial_dev_inst *serial;
128 const char *conn, *serialcomm;
130 gboolean serialcomm_given;
134 drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv;
135 drvc->instances = NULL;
136 conn = serialcomm = NULL;
137 model = SR_METRAHIT_NONE;
138 serialcomm_given = FALSE;
140 sr_spew("scan_1x_2x_rs232() called!");
142 for (l = options; l; l = l->next) {
146 conn = g_variant_get_string(src->data, NULL);
148 case SR_CONF_SERIALCOMM:
149 serialcomm = g_variant_get_string(src->data, NULL);
150 serialcomm_given = TRUE;
157 serialcomm = SERIALCOMM_2X_RS232;
159 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
162 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) {
163 sr_serial_dev_inst_free(serial);
167 serial_flush(serial);
169 model = scan_model_sm(serial);
171 /* If detection failed and no user-supplied parameters,
172 * try second baud rate. */
173 if ((model == SR_METRAHIT_NONE) && !serialcomm_given) {
174 serialcomm = SERIALCOMM_1X_RS232;
175 g_free(serial->serialcomm);
176 serial->serialcomm = g_strdup(serialcomm);
177 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
178 serial_flush(serial);
179 model = scan_model_sm(serial);
183 if (model != SR_METRAHIT_NONE) {
184 sr_spew("%s %s detected!", VENDOR_GMC, sr_gmc_model_str(model));
185 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC,
186 sr_gmc_model_str(model), "")))
188 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
189 sr_err("Device context malloc failed.");
193 devc->limit_samples = 0;
194 devc->limit_msec = 0;
195 devc->num_samples = 0;
196 devc->elapsed_msec = g_timer_new();
197 devc->settings_ok = FALSE;
201 sdi->driver = &gmc_mh_1x_2x_rs232_driver_info;
202 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
204 sdi->probes = g_slist_append(sdi->probes, probe);
205 drvc->instances = g_slist_append(drvc->instances, sdi);
206 devices = g_slist_append(devices, sdi);
212 /** Driver device list function */
213 static GSList *dev_list_1x_2x_rs232(void)
215 return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))
219 static int dev_clear_1x_2x_rs232(void)
221 return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL);
224 static int dev_open(struct sr_dev_inst *sdi)
226 struct sr_serial_dev_inst *serial = sdi->conn;
227 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
230 sdi->status = SR_ST_ACTIVE;
235 static int dev_close(struct sr_dev_inst *sdi)
237 struct sr_serial_dev_inst *serial;
238 struct dev_context *devc;
241 if (serial && serial->fd != -1) {
242 serial_close(serial);
243 sdi->status = SR_ST_INACTIVE;
246 sdi->status = SR_ST_INACTIVE;
248 /* Free dynamically allocated resources. */
249 if ((devc = sdi->priv) && devc->elapsed_msec) {
250 g_timer_destroy(devc->elapsed_msec);
251 devc->elapsed_msec = NULL;
252 devc->model = SR_METRAHIT_NONE;
258 static int cleanup_sm_rs232(void)
260 return dev_clear_1x_2x_rs232();
264 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
265 const struct sr_probe_group *probe_group)
283 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
284 const struct sr_probe_group *probe_group)
286 struct dev_context *devc;
290 if (sdi->status != SR_ST_ACTIVE)
291 return SR_ERR_DEV_CLOSED;
293 if (!(devc = sdi->priv)) {
294 sr_err("sdi->priv was NULL.");
299 case SR_CONF_LIMIT_MSEC:
300 if (g_variant_get_uint64(data) == 0) {
301 sr_err("LIMIT_MSEC can't be 0.");
304 devc->limit_msec = g_variant_get_uint64(data);
305 sr_dbg("Setting time limit to %" PRIu64 "ms.",
308 case SR_CONF_LIMIT_SAMPLES:
309 devc->limit_samples = g_variant_get_uint64(data);
310 sr_dbg("Setting sample limit to %" PRIu64 ".",
311 devc->limit_samples);
320 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
321 const struct sr_probe_group *probe_group)
326 case SR_CONF_SCAN_OPTIONS:
327 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
328 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
330 case SR_CONF_DEVICE_OPTIONS:
331 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
332 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
341 static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
344 struct dev_context *devc;
345 struct sr_serial_dev_inst *serial;
347 if (!sdi || !cb_data || !(devc = sdi->priv))
350 if (sdi->status != SR_ST_ACTIVE)
351 return SR_ERR_DEV_CLOSED;
353 devc->cb_data = cb_data;
354 devc->settings_ok = FALSE;
357 /* Send header packet to the session bus. */
358 std_session_send_df_header(cb_data, LOG_PREFIX);
360 /* Start timer, if required. */
361 if (devc->limit_msec)
362 g_timer_start(devc->elapsed_msec);
364 /* Poll every 40ms, or whenever some data comes in. */
366 sr_source_add(serial->fd, G_IO_IN, 40, gmc_mh_1x_2x_receive_data,
372 static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data)
374 struct dev_context *devc;
376 /* Stop timer, if required. */
377 if (sdi && (devc = sdi->priv) && devc->limit_msec)
378 g_timer_stop(devc->elapsed_msec);
380 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
381 sdi->conn, LOG_PREFIX);
384 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
385 .name = "gmc-mh-1x-2x-rs232",
387 "Gossen Metrawatt Metrahit 1x/2x DMMs, 'RS232' Interface",
389 .init = init_1x_2x_rs232,
390 .cleanup = cleanup_sm_rs232,
391 .scan = scan_1x_2x_rs232,
392 .dev_list = dev_list_1x_2x_rs232,
393 .dev_clear = dev_clear_1x_2x_rs232,
394 .config_get = config_get,
395 .config_set = config_set,
396 .config_list = config_list,
397 .dev_open = dev_open,
398 .dev_close = dev_close,
399 .dev_acquisition_start = dev_acq_start_1x_2x_rs232,
400 .dev_acquisition_stop = dev_acq_stop,