]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/korad-kaxxxxp/protocol.c
korad-kaxxxxp: Add two channels "V" and "I", remove channel "CH1"
[libsigrok.git] / src / hardware / korad-kaxxxxp / protocol.c
index c01d3ca636089ba4b846e4703f16ddf224fdc524..ff5759623a4746d762203e20381f4b1d1b9ddc94 100644 (file)
@@ -88,7 +88,7 @@ SR_PRIV int korad_kaxxxxp_set_value(struct sr_serial_dev_inst *serial,
        give_device_time_to_process(devc);
 
        msg[20] = 0;
-       switch(devc->target){
+       switch (devc->target) {
        case KAXXXXP_CURRENT:
        case KAXXXXP_VOLTAGE:
        case KAXXXXP_STATUS:
@@ -158,7 +158,7 @@ SR_PRIV int korad_kaxxxxp_query_value(struct sr_serial_dev_inst *serial,
 
        give_device_time_to_process(devc);
 
-       switch(devc->target){
+       switch (devc->target) {
        case KAXXXXP_CURRENT:
                /* Read current from device. */
                ret = korad_kaxxxxp_send_cmd(serial, "IOUT1?");
@@ -211,7 +211,7 @@ SR_PRIV int korad_kaxxxxp_get_reply(struct sr_serial_dev_inst *serial,
                                struct dev_context *devc)
 {
        double value;
-       int count, ret, i;
+       int count, ret;
        float *target;
        char status_byte;
 
@@ -250,15 +250,6 @@ SR_PRIV int korad_kaxxxxp_get_reply(struct sr_serial_dev_inst *serial,
        devc->reply[count] = 0;
 
        if (target) {
-               /* Handle the strange 'M'. */
-               if (devc->reply[0] == 'M') {
-                       for (i = 1; i < count; i++)
-                               devc->reply[i - 1] = devc->reply[i];
-                       /* Get the last character. */
-                       if ((i = korad_kaxxxxp_read_chars(serial, 1,
-                                               &(devc->reply[count]))) < 0)
-                               return i;
-               }
                value = g_ascii_strtod(devc->reply, NULL);
                *target = (float)value;
                sr_dbg("value: %f",value);
@@ -293,7 +284,9 @@ SR_PRIV int korad_kaxxxxp_get_reply(struct sr_serial_dev_inst *serial,
                        (status_byte & (1 << 6)) ? "enabled" : "disabled",
                        (status_byte & (1 << 7)) ? "true" : "false");
        }
-
+       /* Read the sixth byte from ISET? BUG workaround. */
+       if (devc->target == KAXXXXP_CURRENT_MAX)
+               serial_read_blocking(serial, &status_byte, 1, 10);
        devc->reply_pending = FALSE;
 
        return ret;
@@ -335,8 +328,12 @@ SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data)
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_analog_old analog;
-       int64_t t, elapsed_us;
+       struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
+       uint64_t elapsed_us;
+       GSList *l;
 
        (void)fd;
 
@@ -352,25 +349,36 @@ SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data)
                /* Get the value. */
                korad_kaxxxxp_get_reply(serial, devc);
 
+               /* Note: digits/spec_digits will be overridden later. */
+               sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+
                /* Send the value forward. */
-               packet.type = SR_DF_ANALOG_OLD;
+               packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
-               analog.channels = sdi->channels;
                analog.num_samples = 1;
+               l = g_slist_copy(sdi->channels);
                if (devc->target == KAXXXXP_CURRENT) {
-                       analog.mq = SR_MQ_CURRENT;
-                       analog.unit = SR_UNIT_AMPERE;
-                       analog.mqflags = 0;
+                       l = g_slist_remove_link(l, g_slist_nth(l, 0));
+                       analog.meaning->channels = l;
+                       analog.meaning->mq = SR_MQ_CURRENT;
+                       analog.meaning->unit = SR_UNIT_AMPERE;
+                       analog.meaning->mqflags = 0;
+                       analog.encoding->digits = 3;
+                       analog.spec->spec_digits = 3;
                        analog.data = &devc->current;
                        sr_session_send(sdi, &packet);
                }
-               if (devc->target == KAXXXXP_VOLTAGE) {
-                       analog.mq = SR_MQ_VOLTAGE;
-                       analog.unit = SR_UNIT_VOLT;
-                       analog.mqflags = SR_MQFLAG_DC;
+               else if (devc->target == KAXXXXP_VOLTAGE) {
+                       l = g_slist_remove_link(l, g_slist_nth(l, 1));
+                       analog.meaning->channels = l;
+                       analog.meaning->mq = SR_MQ_VOLTAGE;
+                       analog.meaning->unit = SR_UNIT_VOLT;
+                       analog.meaning->mqflags = SR_MQFLAG_DC;
+                       analog.encoding->digits = 2;
+                       analog.spec->spec_digits = 2;
                        analog.data = &devc->voltage;
                        sr_session_send(sdi, &packet);
-                       devc->num_samples++;
+                       sr_sw_limits_update_samples_read(&devc->limits, 1);
                }
                next_measurement(devc);
        } else {
@@ -383,21 +391,11 @@ SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data)
                }
        }
 
-       if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
-               sr_info("Requested number of samples reached.");
-               sdi->driver->dev_acquisition_stop(sdi, cb_data);
+       if (sr_sw_limits_check(&devc->limits)) {
+               sr_dev_acquisition_stop(sdi);
                return TRUE;
        }
 
-       if (devc->limit_msec) {
-               t = (g_get_monotonic_time() - devc->starttime) / 1000;
-               if (t > (int64_t)devc->limit_msec) {
-                       sr_info("Requested time limit reached.");
-                       sdi->driver->dev_acquisition_stop(sdi, cb_data);
-                       return TRUE;
-               }
-       }
-
        /* Request next packet, if required. */
        if (sdi->status == SR_ST_ACTIVE) {
                if (devc->reply_pending) {