]> sigrok.org Git - libsigrok.git/blob - src/hardware/siglent-sds/protocol.c
siglent-sds: Fixed issue with hard coded vdiv.
[libsigrok.git] / src / hardware / siglent-sds / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 mhooijboer <marchelh@gmail.com>
5  * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
6  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7  * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #define _GNU_SOURCE
24
25 #include <config.h>
26 #include <errno.h>
27 #include <glib.h>
28 #include <math.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <libsigrok/libsigrok.h>
35 #include "libsigrok-internal.h"
36 #include "scpi.h"
37 #include "protocol.h"
38
39 /* Set the next event to wait for in siglent_sds_receive(). */
40 static void siglent_sds_set_wait_event(struct dev_context *devc, enum wait_events event)
41 {
42         if (event == WAIT_STOP) {
43                 devc->wait_status = 2;
44         } else {
45                 devc->wait_status = 1;
46                 devc->wait_event = event;
47         }
48 }
49
50 /*
51  * Waiting for a event will return a timeout after 2 to 3 seconds in order
52  * to not block the application.
53  */
54 static int siglent_sds_event_wait(const struct sr_dev_inst *sdi)
55 {
56         char *buf;
57         long s;
58         int out;
59         struct dev_context *devc;
60         time_t start;
61
62         if (!(devc = sdi->priv))
63                 return SR_ERR;
64
65         start = time(NULL);
66
67         s = 10000; /* Sleep time for status refresh. */
68         if (devc->wait_status == 1) {
69                 do {
70                         if (time(NULL) - start >= 3) {
71                                 sr_dbg("Timeout waiting for trigger.");
72                                 return SR_ERR_TIMEOUT;
73                         }
74
75                         if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
76                                 return SR_ERR;
77                         sr_atoi(buf, &out);
78                         g_usleep(s);
79                 } while (out == 0);
80
81                 sr_dbg("Device triggered.");
82
83                 if ((devc->timebase < 0.51) && (devc->timebase > 0.99e-6)) {
84                         /*
85                          * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
86                          * -> 85 percent of sweep time
87                          */
88                         s = (devc->timebase * devc->model->series->num_horizontal_divs * 1000);
89                         sr_spew("Sleeping for %ld usecs after trigger, "
90                                 "to let the acq buffer in the device fill", s);
91                         g_usleep(s);
92                 }
93         }
94         if (devc->wait_status == 2) {
95                 do {
96                         if (time(NULL) - start >= 3) {
97                                 sr_dbg("Timeout waiting for trigger.");
98                                 return SR_ERR_TIMEOUT;
99                         }
100                         if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
101                                 return SR_ERR;
102                         sr_atoi(buf, &out);
103                         g_usleep(s);
104                 /* XXX
105                  * Now this loop condition looks suspicious! A bitwise
106                  * OR of a variable and a non-zero literal should be
107                  * non-zero. Logical AND of several non-zero values
108                  * should be non-zero. Are many parts of the condition
109                  * not taking effect? Was some different condition meant
110                  * to get encoded? This needs review, and adjustment.
111                  */
112                 } while ((out | DEVICE_STATE_TRIG_RDY && out | DEVICE_STATE_DATA_ACQ) && out | DEVICE_STATE_STOPPED);
113                 sr_dbg("Device triggerd 2.");
114                 siglent_sds_set_wait_event(devc, WAIT_NONE);
115         }
116
117         return SR_OK;
118 }
119
120 static int siglent_sds_trigger_wait(const struct sr_dev_inst *sdi)
121 {
122         struct dev_context *devc;
123
124         if (!(devc = sdi->priv))
125                 return SR_ERR;
126         return siglent_sds_event_wait(sdi);
127 }
128
129 /* Wait for scope to got to "Stop" in single shot mode. */
130 static int siglent_sds_stop_wait(const struct sr_dev_inst *sdi)
131 {
132         return siglent_sds_event_wait(sdi);
133 }
134
135 /* Send a configuration setting. */
136 SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi, const char *format, ...)
137 {
138         va_list args;
139         int ret;
140
141         va_start(args, format);
142         ret = sr_scpi_send_variadic(sdi->conn, format, args);
143         va_end(args);
144
145         return ret;
146 }
147
148 /* Start capturing a new frameset. */
149 SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
150 {
151         struct dev_context *devc;
152
153         if (!(devc = sdi->priv))
154                 return SR_ERR;
155
156         switch (devc->model->series->protocol) {
157         case SPO_MODEL:
158                 if (devc->data_source == DATA_SOURCE_SCREEN) {
159                         char *buf;
160                         int out;
161
162                         sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
163                                 devc->num_frames + 1, devc->limit_frames);
164                         if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
165                                 return SR_ERR;
166                         if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
167                                 return SR_ERR;
168                         sr_atoi(buf, &out);
169                         if (out == DEVICE_STATE_TRIG_RDY) {
170                                 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
171                         } else if (out == DEVICE_STATE_TRIG_RDY + 1) {
172                                 sr_spew("Device triggered.");
173                                 siglent_sds_set_wait_event(devc, WAIT_BLOCK);
174                                 return SR_OK;
175                         } else {
176                                 sr_spew("Device did not enter ARM mode.");
177                                 return SR_ERR;
178                         }
179                 } else { /* TODO: Implement history retrieval. */
180                         unsigned int framecount;
181                         char buf[200];
182                         int ret;
183
184                         sr_dbg("Starting data capture for history frameset.");
185                         if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
186                                 return SR_ERR;
187                         ret = sr_scpi_read_data(sdi->conn, buf, 200);
188                         if (ret < 0) {
189                                 sr_err("Read error while reading data header.");
190                                 return SR_ERR;
191                         }
192                         memcpy(&framecount, buf + 40, 4);
193                         if (devc->limit_frames > framecount)
194                                 sr_err("Frame limit higher than frames in buffer of device!");
195                         else if (devc->limit_frames == 0)
196                                 devc->limit_frames = framecount;
197                         sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
198                                 devc->num_frames + 1, devc->limit_frames);
199                         if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
200                                 return SR_ERR;
201                         if (siglent_sds_channel_start(sdi) != SR_OK)
202                                 return SR_ERR;
203                         siglent_sds_set_wait_event(devc, WAIT_STOP);
204                 }
205                 break;
206         case NON_SPO_MODEL:
207                 siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
208                 break;
209         }
210
211         return SR_OK;
212 }
213
214 /* Start reading data from the current channel. */
215 SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi)
216 {
217         struct dev_context *devc;
218         struct sr_channel *ch;
219
220         if (!(devc = sdi->priv))
221                 return SR_ERR;
222
223         ch = devc->channel_entry->data;
224
225         sr_dbg("Starting reading data from channel %d.", ch->index + 1);
226
227         switch (devc->model->series->protocol) {
228         case NON_SPO_MODEL:
229         case SPO_MODEL:
230                 if (ch->type == SR_CHANNEL_LOGIC) {
231                         if (sr_scpi_send(sdi->conn, "D%d:WF?",
232                                 ch->index + 1) != SR_OK)
233                                 return SR_ERR;
234                 } else {
235                         if (sr_scpi_send(sdi->conn, "C%d:WF? ALL",
236                                 ch->index + 1) != SR_OK)
237                                 return SR_ERR;
238                 }
239                 siglent_sds_set_wait_event(devc, WAIT_NONE);
240                 break;
241         }
242
243         siglent_sds_set_wait_event(devc, WAIT_BLOCK);
244
245         devc->num_channel_bytes = 0;
246         devc->num_header_bytes = 0;
247         devc->num_block_bytes = 0;
248
249         return SR_OK;
250 }
251
252 /* Read the header of a data block. */
253 static int siglent_sds_read_header(struct sr_dev_inst *sdi)
254 {
255         struct sr_scpi_dev_inst *scpi = sdi->conn;
256         struct dev_context *devc = sdi->priv;
257         char *buf = (char *)devc->buffer;
258         int ret, desc_length;
259         int block_offset = 15; /* Offset for descriptor block. */
260         long dataLength = 0;
261
262         /* Read header from device. */
263         ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes,
264                 devc->model->series->buffer_samples);
265         if (ret < 346) {
266                 sr_err("Read error while reading data header.");
267                 return SR_ERR;
268         }
269         sr_dbg("Device returned %i bytes.", ret);
270         devc->num_header_bytes += ret;
271         buf += block_offset; /* Skip to start descriptor block. */
272
273         /* Parse WaveDescriptor header. */
274         memcpy(&desc_length, buf + 36, 4); /* Descriptor block length */
275         memcpy(&dataLength, buf + 60, 4); /* Data block length */
276
277         devc->block_header_size = desc_length + 15;
278         ret = dataLength;
279
280         sr_dbg("Received data block header: '%s' -> block length %d.", buf, ret);
281
282         return ret;
283 }
284
285 SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
286 {
287         struct sr_dev_inst *sdi;
288         struct sr_scpi_dev_inst *scpi;
289         struct dev_context *devc;
290         struct sr_datafeed_packet packet;
291         struct sr_datafeed_analog analog;
292         struct sr_analog_encoding encoding;
293         struct sr_analog_meaning meaning;
294         struct sr_analog_spec spec;
295         struct sr_datafeed_logic logic;
296         int len, i;
297         struct sr_channel *ch;
298         gsize expected_data_bytes = 0;
299         char *memsize;
300
301         (void)fd;
302
303         if (!(sdi = cb_data))
304                 return TRUE;
305
306         if (!(devc = sdi->priv))
307                 return TRUE;
308
309         scpi = sdi->conn;
310
311         if (!(revents == G_IO_IN || revents == 0))
312                 return TRUE;
313
314         memsize = NULL;
315         if (sr_scpi_get_string(sdi->conn, "MSIZ?", &memsize) != SR_OK)
316                 return SR_ERR;
317
318         switch (devc->wait_event) {
319         case WAIT_NONE:
320                 break;
321         case WAIT_TRIGGER:
322                 if (siglent_sds_trigger_wait(sdi) != SR_OK)
323                         return TRUE;
324                 if (siglent_sds_channel_start(sdi) != SR_OK)
325                         return TRUE;
326                 return TRUE;
327         case WAIT_BLOCK:
328                 if (siglent_sds_channel_start(sdi) != SR_OK)
329                         return TRUE;
330                 break;
331         case WAIT_STOP:
332                 if (siglent_sds_stop_wait(sdi) != SR_OK)
333                         return TRUE;
334                 if (siglent_sds_channel_start(sdi) != SR_OK)
335                         return TRUE;
336                 return TRUE;
337         default:
338                 sr_err("BUG: Unknown event target encountered.");
339                 break;
340         }
341
342         ch = devc->channel_entry->data;
343
344         if (devc->num_block_bytes == 0) {
345
346                 if (g_ascii_strcasecmp(memsize, "14M") == 0) {
347                         sr_err("Device memory depth is set to 14Mpts, so please be patient.");
348                         g_usleep(4900000); /* Sleep for large memory set. */
349                 }
350                 sr_dbg("New block header expected.");
351                 len = siglent_sds_read_header(sdi);
352                 expected_data_bytes = len;
353                 if (len == 0)
354                         /* Still reading the header. */
355                         return TRUE;
356                 if (len == -1) {
357                         sr_err("Read error, aborting capture.");
358                         packet.type = SR_DF_FRAME_END;
359                         sr_session_send(sdi, &packet);
360                         sdi->driver->dev_acquisition_stop(sdi);
361                         return TRUE;
362                 }
363
364                 if (devc->data_source == DATA_SOURCE_SCREEN
365                         && (unsigned)len < expected_data_bytes) {
366                         sr_dbg("Discarding short data block.");
367                         sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1);
368                         return TRUE;
369                 }
370                 devc->num_block_bytes = len;
371                 devc->num_block_read = 0;
372         }
373
374         len = devc->num_block_bytes - devc->num_block_read;
375         if (len > ACQ_BUFFER_SIZE)
376                 len = ACQ_BUFFER_SIZE;
377
378         /* Offset the data block buffer past the IEEE header and description header. */
379         devc->buffer += devc->block_header_size;
380
381         if (len == -1) {
382                 sr_err("Read error, aborting capture.");
383                 packet.type = SR_DF_FRAME_END;
384                 sr_session_send(sdi, &packet);
385                 sdi->driver->dev_acquisition_stop(sdi);
386                 return TRUE;
387         }
388
389         sr_dbg("Received %d bytes.", len);
390
391         devc->num_block_read += len;
392
393         if (ch->type == SR_CHANNEL_ANALOG) {
394                 float vdiv = devc->vdiv[ch->index];
395                 float offset = devc->vert_offset[ch->index];
396                 GArray *float_data;
397                 static GArray *data;
398                 float voltage, vdivlog;
399                 int digits;
400
401                 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
402                 g_array_append_vals(data, devc->buffer, len);
403                 float_data = g_array_new(FALSE, FALSE, sizeof(float));
404                 for (i = 0; i < len; i++) {
405                         voltage = (float)g_array_index(data, int8_t, i) / 25;
406                         voltage = ((vdiv * voltage) - offset);
407                         g_array_append_val(float_data, voltage);
408                 }
409                 vdivlog = log10f(vdiv);
410                 digits = -(int) vdivlog + (vdivlog < 0.0);
411                 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
412                 analog.meaning->channels = g_slist_append(NULL, ch);
413                 analog.num_samples = float_data->len;
414                 analog.data = (float *)float_data->data;
415                 analog.meaning->mq = SR_MQ_VOLTAGE;
416                 analog.meaning->unit = SR_UNIT_VOLT;
417                 analog.meaning->mqflags = 0;
418                 packet.type = SR_DF_ANALOG;
419                 packet.payload = &analog;
420                 sr_session_send(sdi, &packet);
421                 g_slist_free(analog.meaning->channels);
422                 g_array_free(data, TRUE);
423         } else {
424                 logic.length = len;
425                 logic.unitsize = 1;
426                 logic.data = devc->buffer;
427                 packet.type = SR_DF_LOGIC;
428                 packet.payload = &logic;
429                 sr_session_send(sdi, &packet);
430         }
431
432         if (devc->num_block_read == devc->num_block_bytes) {
433                 sr_dbg("Block has been completed.");
434                 sr_dbg("Preparing for possible next block.");
435                 devc->num_header_bytes = 0;
436                 devc->num_block_bytes = 0;
437                 if (devc->data_source != DATA_SOURCE_SCREEN)
438                         siglent_sds_set_wait_event(devc, WAIT_BLOCK);
439                 if (!sr_scpi_read_complete(scpi)) {
440                         sr_err("Read should have been completed.");
441                         packet.type = SR_DF_FRAME_END;
442                         sr_session_send(sdi, &packet);
443                         sdi->driver->dev_acquisition_stop(sdi);
444                         return TRUE;
445                 }
446                 devc->num_block_read = 0;
447         } else {
448                 sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read.",
449                         devc->num_block_read, devc->num_block_bytes);
450         }
451         devc->num_channel_bytes += len;
452         if (devc->num_channel_bytes < expected_data_bytes) {
453                 /* Don't have the full data for this channel yet, re-run. */
454                 return TRUE;
455         }
456         if (devc->channel_entry->next) {
457                 /* We got the frame for this channel, now get the next channel. */
458                 devc->channel_entry = devc->channel_entry->next;
459                 siglent_sds_channel_start(sdi);
460         } else {
461                 /* Done with this frame. */
462                 packet.type = SR_DF_FRAME_END;
463                 sr_session_send(sdi, &packet);
464
465                 if (++devc->num_frames == devc->limit_frames) {
466                         /* Last frame, stop capture. */
467                         sdi->driver->dev_acquisition_stop(sdi);
468                 } else {
469                         /* Get the next frame, starting with the first channel. */
470                         devc->channel_entry = devc->enabled_channels;
471                         siglent_sds_capture_start(sdi);
472
473                         /* Start of next frame. */
474                         packet.type = SR_DF_FRAME_BEGIN;
475                         sr_session_send(sdi, &packet);
476                 }
477         }
478
479         return TRUE;
480 }
481
482 SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
483 {
484         struct dev_context *devc;
485         struct sr_channel *ch;
486         char *cmd, *response;
487         unsigned int i;
488         int res, num_tokens;
489         gchar **tokens;
490
491         devc = sdi->priv;
492
493         /* Analog channel state. */
494         for (i = 0; i < devc->model->analog_channels; i++) {
495                 cmd = g_strdup_printf("C%i:TRA?", i + 1);
496                 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->analog_channels[i]);
497                 g_free(cmd);
498                 if (res != SR_OK)
499                         return SR_ERR;
500                 ch = g_slist_nth_data(sdi->channels, i);
501                 ch->enabled = devc->analog_channels[i];
502         }
503         sr_dbg("Current analog channel state:");
504         for (i = 0; i < devc->model->analog_channels; i++)
505                 sr_dbg("CH%d %s", i + 1, devc->analog_channels[i] ? "On" : "Off");
506
507         /* Digital channel state. */
508         if (devc->model->has_digital) {
509                 gboolean status;
510
511                 sr_dbg("Check logic analyzer channel state.");
512                 devc->la_enabled = FALSE;
513                 cmd = g_strdup_printf("DGST?");
514                 res = sr_scpi_get_bool(sdi->conn, cmd, &status);
515                 g_free(cmd);
516                 if (res != SR_OK)
517                         return SR_ERR;
518                 sr_dbg("Logic analyzer status: %s", status ? "On" : "Off");
519                 if (status) {
520                         devc->la_enabled = TRUE;
521                         for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
522                                 cmd = g_strdup_printf("D%i:DGCH?", i);
523                                 res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]);
524                                 g_free(cmd);
525                                 if (res != SR_OK)
526                                         return SR_ERR;
527                                 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
528                                 ch->enabled = devc->digital_channels[i];
529                                 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
530                         }
531                 } else {
532                         for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) {
533                                 ch = g_slist_nth_data(sdi->channels, i + devc->model->analog_channels);
534                                 devc->digital_channels[i] = FALSE;
535                                 ch->enabled = devc->digital_channels[i];
536                                 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "On" : "Off");
537                         }
538                 }
539         }
540
541         /* Timebase. */
542         if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
543                 return SR_ERR;
544         sr_dbg("Current timebase: %g.", devc->timebase);
545
546         /* Probe attenuation. */
547         for (i = 0; i < devc->model->analog_channels; i++) {
548                 cmd = g_strdup_printf("C%d:ATTN?", i + 1);
549                 res = sr_scpi_get_float(sdi->conn, cmd, &devc->attenuation[i]);
550                 g_free(cmd);
551                 if (res != SR_OK)
552                         return SR_ERR;
553         }
554         sr_dbg("Current probe attenuation:");
555         for (i = 0; i < devc->model->analog_channels; i++)
556                 sr_dbg("CH%d %g", i + 1, devc->attenuation[i]);
557
558         /* Vertical gain and offset. */
559         if (siglent_sds_get_dev_cfg_vertical(sdi) != SR_OK)
560                 return SR_ERR;
561
562         /* Coupling. */
563         for (i = 0; i < devc->model->analog_channels; i++) {
564                 cmd = g_strdup_printf("C%d:CPL?", i + 1);
565                 res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
566                 g_free(cmd);
567                 if (res != SR_OK)
568                         return SR_ERR;
569         }
570
571         sr_dbg("Current coupling:");
572         for (i = 0; i < devc->model->analog_channels; i++)
573                 sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
574
575         /* Trigger source. */
576         response = NULL;
577         tokens = NULL;
578         if (sr_scpi_get_string(sdi->conn, "TRSE?", &response) != SR_OK)
579                 return SR_ERR;
580         tokens = g_strsplit(response, ",", 0);
581         for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
582         if (num_tokens < 4) {
583                 sr_dbg("IDN response not according to spec: %80.s.", response);
584                 g_strfreev(tokens);
585                 g_free(response);
586                 return SR_ERR_DATA;
587         }
588         g_free(response);
589         devc->trigger_source = g_strstrip(g_strdup(tokens[2]));
590         sr_dbg("Current trigger source: %s.", devc->trigger_source);
591
592         /* TODO: Horizontal trigger position. */
593
594         devc->horiz_triggerpos = 0;
595         sr_dbg("Current horizontal trigger position: %g.", devc->horiz_triggerpos);
596
597         /* Trigger slope. */
598         cmd = g_strdup_printf("%s:TRSL?", devc->trigger_source);
599         res = sr_scpi_get_string(sdi->conn, cmd, &devc->trigger_slope);
600         g_free(cmd);
601         if (res != SR_OK)
602                 return SR_ERR;
603         sr_dbg("Current trigger slope: %s.", devc->trigger_slope);
604
605         /* Trigger level. */
606         cmd = g_strdup_printf("%s:TRLV?", devc->trigger_source);
607         res = sr_scpi_get_float(sdi->conn, cmd, &devc->trigger_level);
608         g_free(cmd);
609         if (res != SR_OK)
610                 return SR_ERR;
611         sr_dbg("Current trigger level: %g.", devc->trigger_level);
612         return SR_OK;
613 }
614
615 SR_PRIV int siglent_sds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi)
616 {
617         struct dev_context *devc;
618         char *cmd;
619         unsigned int i;
620         int res;
621
622         devc = sdi->priv;
623
624         /* Vertical gain. */
625         for (i = 0; i < devc->model->analog_channels; i++) {
626                 cmd = g_strdup_printf("C%d:VDIV?", i + 1);
627                 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vdiv[i]);
628                 g_free(cmd);
629                 if (res != SR_OK)
630                         return SR_ERR;
631         }
632         sr_dbg("Current vertical gain:");
633         for (i = 0; i < devc->model->analog_channels; i++)
634                 sr_dbg("CH%d %g", i + 1, devc->vdiv[i]);
635
636         /* Vertical offset. */
637         for (i = 0; i < devc->model->analog_channels; i++) {
638                 cmd = g_strdup_printf("C%d:OFST?", i + 1);
639                 res = sr_scpi_get_float(sdi->conn, cmd, &devc->vert_offset[i]);
640                 g_free(cmd);
641                 if (res != SR_OK)
642                         return SR_ERR;
643         }
644         sr_dbg("Current vertical offset:");
645         for (i = 0; i < devc->model->analog_channels; i++)
646                 sr_dbg("CH%d %g", i + 1, devc->vert_offset[i]);
647
648         return SR_OK;
649 }
650
651 SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
652 {
653         struct dev_context *devc;
654         char *cmd;
655         int res;
656         char *sample_points_string;
657         float samplerate_scope, fvalue;
658         char *first, *concat;
659
660         devc = sdi->priv;
661         cmd = g_strdup_printf("SANU? C1");
662         res = sr_scpi_get_string(sdi->conn, cmd, &sample_points_string);
663         g_free(cmd);
664         if (res != SR_OK)
665                 return SR_ERR;
666         if (g_strstr_len(sample_points_string, -1, "Mpts") != NULL) {
667                 sample_points_string[strlen(sample_points_string) - 4] = '\0';
668
669                 if (g_strstr_len(sample_points_string, -1, ".") != NULL) {
670                         first = strtok(sample_points_string, ".");
671                         concat = strcat(first, strtok(NULL, "."));
672                         if (sr_atof_ascii(concat, &fvalue) != SR_OK || fvalue == 0.0) {
673                                 sr_dbg("Invalid float converted from scope response.");
674                                 return SR_ERR;
675                         }
676                 } else {
677                         if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK || fvalue == 0.0) {
678                                 sr_dbg("Invalid float converted from scope response.");
679                                 return SR_ERR;
680                         }
681                 }
682                 samplerate_scope = fvalue * 100000;
683         } else {
684                 sample_points_string[strlen(sample_points_string) - 4] = '\0';
685                 if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK || fvalue == 0.0) {
686                         sr_dbg("Invalid float converted from scope response.");
687                         return SR_ERR;
688                 }
689                 samplerate_scope = fvalue * 1000;
690         }
691         /* Get the timebase. */
692         if (sr_scpi_get_float(sdi->conn, ":TDIV?", &devc->timebase) != SR_OK)
693                 return SR_ERR;
694         sr_dbg("Current timebase: %g.", devc->timebase);
695         devc->samplerate = samplerate_scope / (devc->timebase * devc->model->series->num_horizontal_divs);
696
697         return SR_OK;
698 }