]>
Commit | Line | Data |
---|---|---|
23f43dff ML |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com> | |
5 | * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | ||
24 | #include <glib.h> | |
25 | #include <string.h> | |
26 | ||
27 | /* Message logging helpers with subsystem-specific prefix string. */ | |
28 | #define LOG_PREFIX "scpi_serial: " | |
29 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) | |
30 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) | |
31 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) | |
32 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) | |
33 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) | |
34 | ||
35 | #define SCPI_READ_RETRIES 100 | |
36 | #define SCPI_READ_RETRY_TIMEOUT 10000 | |
37 | ||
38 | SR_PRIV int scpi_serial_open(void *priv) | |
39 | { | |
40 | struct sr_serial_dev_inst *serial = priv; | |
41 | ||
9dfeb81b ML |
42 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
43 | return SR_ERR; | |
44 | ||
45 | if (serial_flush(serial) != SR_OK) | |
46 | return SR_ERR; | |
47 | ||
48 | return SR_OK; | |
23f43dff ML |
49 | } |
50 | ||
51 | SR_PRIV int scpi_serial_source_add(void *priv, int events, int timeout, | |
52 | sr_receive_data_callback_t cb, void *cb_data) | |
53 | { | |
54 | struct sr_serial_dev_inst *serial = priv; | |
55 | ||
abc4b335 | 56 | return serial_source_add(serial, events, timeout, cb, cb_data); |
23f43dff ML |
57 | } |
58 | ||
59 | SR_PRIV int scpi_serial_source_remove(void *priv) | |
60 | { | |
61 | struct sr_serial_dev_inst *serial = priv; | |
62 | ||
7faa3e88 | 63 | return serial_source_remove(serial); |
23f43dff ML |
64 | } |
65 | ||
66 | SR_PRIV int scpi_serial_send(void *priv, const char *command) | |
67 | { | |
721fc227 | 68 | int len, result, written; |
23f43dff ML |
69 | gchar *terminated_command; |
70 | struct sr_serial_dev_inst *serial = priv; | |
71 | ||
72 | terminated_command = g_strconcat(command, "\n", NULL); | |
73 | len = strlen(terminated_command); | |
721fc227 ML |
74 | written = 0; |
75 | while (written < len) { | |
76 | result = serial_write(serial, terminated_command + written, len - written); | |
77 | if (result < 0) { | |
78 | sr_err("Error while sending SCPI command: '%s'.", command); | |
79 | g_free(terminated_command); | |
80 | return SR_ERR; | |
81 | } | |
82 | written += result; | |
23f43dff ML |
83 | } |
84 | ||
721fc227 ML |
85 | g_free(terminated_command); |
86 | ||
23f43dff ML |
87 | sr_spew("Successfully sent SCPI command: '%s'.", command); |
88 | ||
89 | return SR_OK; | |
90 | } | |
91 | ||
92 | SR_PRIV int scpi_serial_receive(void *priv, char **scpi_response) | |
93 | { | |
94 | int len, ret; | |
95 | char buf[256]; | |
96 | unsigned int i; | |
97 | GString *response; | |
98 | struct sr_serial_dev_inst *serial = priv; | |
99 | ||
100 | response = g_string_sized_new(1024); | |
101 | ||
102 | for (i = 0; i <= SCPI_READ_RETRIES; i++) { | |
103 | while ((len = serial_read(serial, buf, sizeof(buf))) > 0) | |
104 | response = g_string_append_len(response, buf, len); | |
105 | ||
106 | if (response->len > 0 && | |
107 | response->str[response->len-1] == '\n') { | |
108 | sr_spew("Fetched full SCPI response."); | |
109 | break; | |
110 | } | |
111 | ||
112 | g_usleep(SCPI_READ_RETRY_TIMEOUT); | |
113 | } | |
114 | ||
115 | if (response->len == 0) { | |
116 | sr_dbg("No SCPI response received."); | |
117 | g_string_free(response, TRUE); | |
118 | *scpi_response = NULL; | |
119 | return SR_ERR; | |
120 | } else if (response->str[response->len - 1] == '\n') { | |
121 | /* | |
122 | * The SCPI response contains a LF ('\n') at the end and we | |
123 | * don't need this so replace it with a '\0' and decrement | |
124 | * the length. | |
125 | */ | |
126 | response->str[--response->len] = '\0'; | |
127 | ret = SR_OK; | |
128 | } else { | |
129 | sr_warn("Incomplete SCPI response received!"); | |
130 | ret = SR_ERR; | |
131 | } | |
132 | ||
133 | /* Minor optimization: steal the string instead of copying. */ | |
134 | *scpi_response = response->str; | |
135 | ||
136 | /* A SCPI response can be quite large, print at most 50 characters. */ | |
137 | sr_dbg("SCPI response received (length %d): '%.50s'", | |
138 | response->len, response->str); | |
139 | ||
140 | g_string_free(response, FALSE); | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
bc03a7cc BV |
145 | /* Some stubs to keep the compiler from whining. */ |
146 | static int scpi_serial_read(void *priv, char *buf, int maxlen) | |
147 | { | |
148 | return serial_read(priv, buf, maxlen); | |
149 | } | |
150 | static int scpi_serial_close(void *priv) | |
151 | { | |
152 | return serial_close(priv); | |
153 | } | |
154 | static void scpi_serial_free(void *priv) | |
155 | { | |
156 | return sr_serial_dev_inst_free(priv); | |
157 | } | |
158 | ||
23f43dff ML |
159 | SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port, |
160 | const char *serialcomm) | |
161 | { | |
162 | struct sr_scpi_dev_inst *scpi; | |
163 | struct sr_serial_dev_inst *serial; | |
164 | ||
165 | scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst)); | |
166 | ||
167 | if (!(serial = sr_serial_dev_inst_new(port, serialcomm))) | |
168 | { | |
169 | g_free(scpi); | |
170 | return NULL; | |
171 | } | |
172 | ||
173 | scpi->open = scpi_serial_open; | |
174 | scpi->source_add = scpi_serial_source_add; | |
175 | scpi->source_remove = scpi_serial_source_remove; | |
176 | scpi->send = scpi_serial_send; | |
177 | scpi->receive = scpi_serial_receive; | |
bc03a7cc BV |
178 | scpi->read = scpi_serial_read; |
179 | scpi->close = scpi_serial_close; | |
180 | scpi->free = scpi_serial_free; | |
23f43dff ML |
181 | scpi->priv = serial; |
182 | ||
183 | return scpi; | |
184 | } |