]> sigrok.org Git - libsigrok.git/blobdiff - src/dmm/metex14.c
serial-dmm, metex14: add support for multiple channels per DMM
[libsigrok.git] / src / dmm / metex14.c
index 2677484a743608e87451fc0db7d2da266f726d50..2d0a70d640475d9778a92c03973e974d0c7c8d2e 100644 (file)
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /**
@@ -87,7 +86,7 @@ static int parse_value(const uint8_t *buf, struct metex14_info *info,
                return SR_OK;
 
        /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
-       sscanf((const char *)&valstr, "%f", result);
+       sr_atof_ascii((const char *)&valstr, result);
 
        dot_pos = strcspn(valstr, ".");
        if (dot_pos < cnt)
@@ -153,17 +152,17 @@ static void parse_flags(const char *buf, struct metex14_info *info)
                info->is_unitless = TRUE;
 
        /* Bytes 0-1: Measurement mode, except AC/DC */
-       info->is_resistance  = !strncmp(buf, "OH", 2) ||
+       info->is_resistance = !strncmp(buf, "OH", 2) ||
                (!strncmp(buf, "  ", 2) && info->is_ohm);
-       info->is_capacity    = !strncmp(buf, "CA", 2) ||
+       info->is_capacity = !strncmp(buf, "CA", 2) ||
                (!strncmp(buf, "  ", 2) && info->is_farad);
        info->is_temperature = !strncmp(buf, "TE", 2);
-       info->is_diode       = !strncmp(buf, "DI", 2) ||
+       info->is_diode = !strncmp(buf, "DI", 2) ||
                (!strncmp(buf, "  ", 2) && info->is_volt && info->is_milli);
-       info->is_frequency   = !strncmp(buf, "FR", 2) ||
+       info->is_frequency = !strncmp(buf, "FR", 2) ||
                (!strncmp(buf, "  ", 2) && info->is_hertz);
-       info->is_gain        = !strncmp(buf, "DB", 2);
-       info->is_hfe         = !strncmp(buf, "HF", 2) ||
+       info->is_gain = !strncmp(buf, "DB", 2);
+       info->is_hfe = !strncmp(buf, "HF", 2) ||
                (!strncmp(buf, "  ", 2) && !info->is_volt && !info->is_ohm &&
                 !info->is_logic && !info->is_farad && !info->is_hertz);
        /*
@@ -244,7 +243,7 @@ static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
        if (info->is_dc)
                analog->meaning->mqflags |= SR_MQFLAG_DC;
        if (info->is_diode)
-               analog->meaning->mqflags |= SR_MQFLAG_DIODE;
+               analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
 }
 
 static gboolean flags_valid(const struct metex14_info *info)
@@ -294,7 +293,7 @@ SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
 
        sr_spew("Requesting DMM packet.");
 
-       return (serial_write_nonblocking(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
+       return serial_write_blocking(serial, &wbuf, 1, 0);
 }
 #endif
 
@@ -314,6 +313,25 @@ SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
        return TRUE;
 }
 
+SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf)
+{
+       struct metex14_info info;
+       size_t ch_idx;
+       const uint8_t *ch_buf;
+
+       ch_buf = buf;
+       for (ch_idx = 0; ch_idx < 4; ch_idx++) {
+               if (ch_buf[13] != '\r')
+                       return FALSE;
+               memset(&info, 0x00, sizeof(info));
+               parse_flags((const char *)ch_buf, &info);
+               if (!flags_valid(&info))
+                       return FALSE;
+               ch_buf += METEX14_PACKET_SIZE;
+       }
+       return TRUE;
+}
+
 /**
  * Parse a protocol packet.
  *
@@ -350,8 +368,39 @@ SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
        parse_flags((const char *)buf, info_local);
        handle_flags(analog, floatval, &exponent, info_local);
 
-       analog->encoding->digits  = -exponent;
+       analog->encoding->digits = -exponent;
        analog->spec->spec_digits = -exponent;
 
        return SR_OK;
 }
+
+/**
+ * Parse one out of four values of a four-display Metex14 variant.
+ *
+ * The caller's 'info' parameter can be used to track the channel index,
+ * as long as the information is kept across calls to the 14-byte packet
+ * parse routine (which clears the 'info' container).
+ *
+ * Since analog values have further details in the 'analog' parameter,
+ * passing multiple values per parse routine call is problematic. So we
+ * prefer the approach of passing one value per call, which is most
+ * reliable and shall fit every similar device with multiple displays.
+ *
+ * The meters which use this parse routine send one 14-byte packet per
+ * display. Each packet has the regular Metex14 layout.
+ */
+SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
+       struct sr_datafeed_analog *analog, void *info)
+{
+       struct metex14_info *info_local;
+       size_t ch_idx;
+       const uint8_t *ch_buf;
+       int rc;
+
+       info_local = info;
+       ch_idx = info_local->ch_idx;
+       ch_buf = buf + ch_idx * METEX14_PACKET_SIZE;
+       rc = sr_metex14_parse(ch_buf, floatval, analog, info);
+       info_local->ch_idx = ch_idx + 1;
+       return rc;
+}