]> sigrok.org Git - libsigrok.git/blame - hardware/tekpower-dmm/protocol.c
serial: Don't hardcode parity and stop bits on Windows.
[libsigrok.git] / hardware / tekpower-dmm / protocol.c
CommitLineData
7dc55d93
AG
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <glib.h>
7dc55d93
AG
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
24#include <errno.h>
bbabddbd
UH
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27#include "protocol.h"
7dc55d93 28
8c1adf37
UH
29/* User-defined FS9721_LP3 flag 'c2c1_10' means temperature on this DMM. */
30#define is_temperature info.is_c2c1_10
31
6bef68a7
UH
32static void log_dmm_packet(const uint8_t *buf)
33{
34 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
35 " %02x %02x %02x %02x %02x %02x %02x",
36 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
37 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
38}
39
bbabddbd 40/* Now see what the value means, and pass that on. */
8c1adf37 41static void fs9721_serial_handle_packet(const uint8_t *buf,
be5c1d3b 42 struct dev_context *devc)
7dc55d93 43{
8c1adf37 44 float floatval;
7dc55d93
AG
45 struct sr_datafeed_packet packet;
46 struct sr_datafeed_analog *analog;
8c1adf37 47 struct fs9721_info info;
7dc55d93 48
6bef68a7
UH
49 log_dmm_packet(buf);
50
bbabddbd 51 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
8c1adf37 52 sr_err("Analog packet malloc failed.");
7dc55d93
AG
53 return;
54 }
bbabddbd
UH
55
56 if (!(analog->data = g_try_malloc(sizeof(float)))) {
8c1adf37 57 sr_err("Analog value malloc failed.");
7dc55d93
AG
58 g_free(analog);
59 return;
60 }
bbabddbd 61
bbabddbd 62 analog->num_samples = 1;
7dc55d93
AG
63 analog->mq = -1;
64
8c1adf37
UH
65 sr_fs9721_parse(buf, &floatval, analog, &info);
66 *analog->data = floatval;
be5c1d3b 67
8c1adf37 68 if (is_temperature) {
7dc55d93 69 analog->mq = SR_MQ_TEMPERATURE;
bbabddbd 70 /* No Kelvin or Fahrenheit from the device, just Celsius. */
7dc55d93 71 analog->unit = SR_UNIT_CELSIUS;
7dc55d93
AG
72 }
73
7dc55d93
AG
74 if (analog->mq != -1) {
75 /* Got a measurement. */
7dc55d93
AG
76 packet.type = SR_DF_ANALOG;
77 packet.payload = analog;
78 sr_session_send(devc->cb_data, &packet);
79 devc->num_samples++;
80 }
bbabddbd 81
7dc55d93
AG
82 g_free(analog->data);
83 g_free(analog);
84}
85
d35afa87 86static void handle_new_data(struct dev_context *devc)
7dc55d93 87{
bbabddbd 88 int len, i, offset = 0;
bbabddbd
UH
89
90 /* Try to get as much data as the buffer can hold. */
7dc55d93 91 len = DMM_BUFSIZE - devc->buflen;
d35afa87 92 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
7dc55d93 93 if (len < 1) {
bbabddbd 94 sr_err("Serial port read error: %d.", len);
7dc55d93
AG
95 return;
96 }
97 devc->buflen += len;
98
bbabddbd 99 /* Now look for packets in that data. */
be5c1d3b 100 while ((devc->buflen - offset) >= FS9721_PACKET_SIZE) {
8c1adf37
UH
101 if (sr_fs9721_packet_valid(devc->buf + offset)) {
102 fs9721_serial_handle_packet(devc->buf + offset, devc);
be5c1d3b 103 offset += FS9721_PACKET_SIZE;
7dc55d93
AG
104 } else {
105 offset++;
106 }
107 }
108
bbabddbd
UH
109 /* If we have any data left, move it to the beginning of our buffer. */
110 for (i = 0; i < devc->buflen - offset; i++)
7dc55d93
AG
111 devc->buf[i] = devc->buf[offset + i];
112 devc->buflen -= offset;
113}
114
bbabddbd 115SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
7dc55d93 116{
642e9d62 117 struct sr_dev_inst *sdi;
7dc55d93
AG
118 struct dev_context *devc;
119
d35afa87
BV
120 (void)fd;
121
7dc55d93
AG
122 if (!(sdi = cb_data))
123 return TRUE;
124
125 if (!(devc = sdi->priv))
126 return TRUE;
127
bbabddbd 128 if (revents == G_IO_IN) {
7dc55d93 129 /* Serial data arrived. */
d35afa87 130 handle_new_data(devc);
7dc55d93
AG
131 }
132
133 if (devc->num_samples >= devc->limit_samples) {
6bef68a7 134 sr_info("Requested number of samples reached, stopping.");
7dc55d93
AG
135 sdi->driver->dev_acquisition_stop(sdi, cb_data);
136 return TRUE;
137 }
138
139 return TRUE;
140}