- serial_flush(fd);
- /* There's no way to get an ID from the multimeter. It just sends data
- * periodically, so the best we can do is check if the packets match the
- * expected format. */
- while (!devices && retry < 3)
- {
- size_t i;
- size_t good_packets = 0;
- retry++;
-
- /* Let's get a bit of data and see if we can find a packet */
- len = sizeof(buf);
- serial_readline(fd, &b, &len, 500);
- if( (len == 0) || (len < LCD14_PACKET_SIZE) ) {
- /* Not enough data received, is the DMM connected ? */
- continue;
- }
-
- /* Let's treat our buffer like a stream, and find any
- * valid packets */
- for( i = 0; i < len - LCD14_PACKET_SIZE + 1;
- /* don't increment i here */ )
- {
- const lcd14_packet *packet = (void *)(&buf[i]);
- if( !lcd14_is_packet_valid(packet, NULL) ){
- i++;
- continue;
- }
- good_packets++;
- i += LCD14_PACKET_SIZE;
- }
-
- /* If we dropped more than two packets worth of data, something
- * is wrong */
- size_t dropped = len - (good_packets * LCD14_PACKET_SIZE);
- if(dropped > 2 * LCD14_PACKET_SIZE)
- continue;
-
- /* Let's see if we have anything good */
- if (good_packets == 0)
- continue;
+ serial_flush(serial);
+
+ /*
+ * There's no way to get an ID from the multimeter. It just sends data
+ * periodically, so the best we can do is check if the packets match
+ * the expected format.
+ */
+
+ /* Let's get a bit of data and see if we can find a packet. */
+ len = sizeof(buf);
+
+ ret = serial_stream_detect(serial, buf, &len, FS9721_PACKET_SIZE,
+ sr_fs9721_packet_valid, 1000, 2400);
+ if (ret != SR_OK)
+ goto scan_cleanup;
+
+ /*
+ * If we dropped more than two packets worth of data, something is
+ * wrong. We shouldn't quit however, since the dropped bytes might be
+ * just zeroes at the beginning of the stream. Those can occur as a
+ * combination of the nonstandard cable that ships with this device and
+ * the serial port or USB to serial adapter.
+ */
+ dropped = len - FS9721_PACKET_SIZE;
+ if (dropped > 2 * FS9721_PACKET_SIZE)
+ sr_warn("Had to drop too much data.");
+
+ sr_info("Found device on port %s.", conn);
+
+ if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
+ "TP4000ZC", "")))
+ goto scan_cleanup;
+
+ if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
+ sr_err("Device context malloc failed.");
+ goto scan_cleanup;
+ }