sched.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2012 Bert Vermeulen <bert@biot.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. #include <glib.h>
  20. #include "libsigrok.h"
  21. #include "libsigrok-internal.h"
  22. #include "agilent-dmm.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <math.h>
  27. static void dispatch(const struct sr_dev_inst *sdi)
  28. {
  29. struct dev_context *devc;
  30. const struct agdmm_job *jobs;
  31. int64_t now;
  32. int i;
  33. devc = sdi->priv;
  34. jobs = devc->profile->jobs;
  35. now = g_get_monotonic_time() / 1000;
  36. for (i = 0; (&jobs[i])->interval; i++) {
  37. if (now - devc->jobqueue[i] > (&jobs[i])->interval) {
  38. sr_spew("Running job %d.", i);
  39. (&jobs[i])->send(sdi);
  40. devc->jobqueue[i] = now;
  41. }
  42. }
  43. }
  44. static void receive_line(const struct sr_dev_inst *sdi)
  45. {
  46. struct dev_context *devc;
  47. const struct agdmm_recv *recvs, *recv;
  48. GRegex *reg;
  49. GMatchInfo *match;
  50. int i;
  51. devc = sdi->priv;
  52. /* Strip CRLF */
  53. while (devc->buflen) {
  54. if (*(devc->buf + devc->buflen - 1) == '\r'
  55. || *(devc->buf + devc->buflen - 1) == '\n')
  56. *(devc->buf + --devc->buflen) = '\0';
  57. else
  58. break;
  59. }
  60. sr_spew("Received '%s'.", devc->buf);
  61. recv = NULL;
  62. recvs = devc->profile->recvs;
  63. for (i = 0; (&recvs[i])->recv_regex; i++) {
  64. reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL);
  65. if (g_regex_match(reg, (char *)devc->buf, 0, &match)) {
  66. recv = &recvs[i];
  67. break;
  68. }
  69. g_match_info_unref(match);
  70. g_regex_unref(reg);
  71. }
  72. if (recv) {
  73. recv->recv(sdi, match);
  74. g_match_info_unref(match);
  75. g_regex_unref(reg);
  76. } else
  77. sr_dbg("Unknown line '%s'.", devc->buf);
  78. /* Done with this. */
  79. devc->buflen = 0;
  80. }
  81. SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
  82. {
  83. struct sr_dev_inst *sdi;
  84. struct dev_context *devc;
  85. struct sr_serial_dev_inst *serial;
  86. int len;
  87. (void)fd;
  88. if (!(sdi = cb_data))
  89. return TRUE;
  90. if (!(devc = sdi->priv))
  91. return TRUE;
  92. serial = sdi->conn;
  93. if (revents == G_IO_IN) {
  94. /* Serial data arrived. */
  95. while(AGDMM_BUFSIZE - devc->buflen - 1 > 0) {
  96. len = serial_read(serial, devc->buf + devc->buflen, 1);
  97. if (len < 1)
  98. break;
  99. devc->buflen += len;
  100. *(devc->buf + devc->buflen) = '\0';
  101. if (*(devc->buf + devc->buflen - 1) == '\n') {
  102. /* End of line */
  103. receive_line(sdi);
  104. break;
  105. }
  106. }
  107. }
  108. dispatch(sdi);
  109. if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
  110. sdi->driver->dev_acquisition_stop(sdi, cb_data);
  111. return TRUE;
  112. }
  113. static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
  114. {
  115. struct sr_serial_dev_inst *serial;
  116. char buf[32];
  117. serial = sdi->conn;
  118. sr_spew("Sending '%s'.", cmd);
  119. strncpy(buf, cmd, 28);
  120. if (!strncmp(buf, "*IDN?", 5))
  121. strncat(buf, "\r\n", 32);
  122. else
  123. strncat(buf, "\n\r\n", 32);
  124. if (serial_write(serial, buf, strlen(buf)) == -1) {
  125. sr_err("Failed to send: %s.", strerror(errno));
  126. return SR_ERR;
  127. }
  128. return SR_OK;
  129. }
  130. static int send_stat(const struct sr_dev_inst *sdi)
  131. {
  132. return agdmm_send(sdi, "STAT?");
  133. }
  134. static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
  135. {
  136. struct dev_context *devc;
  137. char *s;
  138. devc = sdi->priv;
  139. s = g_match_info_fetch(match, 1);
  140. sr_spew("STAT response '%s'.", s);
  141. /* Max, Min or Avg mode -- no way to tell which, so we'll
  142. * set both flags to denote it's not a normal measurement. */
  143. if (s[0] == '1')
  144. devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
  145. else
  146. devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
  147. if (s[1] == '1')
  148. devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
  149. else
  150. devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
  151. /* Triggered or auto hold modes. */
  152. if (s[2] == '1' || s[3] == '1')
  153. devc->cur_mqflags |= SR_MQFLAG_HOLD;
  154. else
  155. devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
  156. /* Temp/aux mode. */
  157. if (s[7] == '1')
  158. devc->mode_tempaux = TRUE;
  159. else
  160. devc->mode_tempaux = FALSE;
  161. /* Continuity mode. */
  162. if (s[16] == '1')
  163. devc->mode_continuity = TRUE;
  164. else
  165. devc->mode_continuity = FALSE;
  166. g_free(s);
  167. return SR_OK;
  168. }
  169. static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
  170. {
  171. struct dev_context *devc;
  172. char *s;
  173. devc = sdi->priv;
  174. s = g_match_info_fetch(match, 1);
  175. sr_spew("STAT response '%s'.", s);
  176. /* Peak hold mode. */
  177. if (s[4] == '1')
  178. devc->cur_mqflags |= SR_MQFLAG_MAX;
  179. else
  180. devc->cur_mqflags &= ~SR_MQFLAG_MAX;
  181. /* Triggered hold mode. */
  182. if (s[7] == '1')
  183. devc->cur_mqflags |= SR_MQFLAG_HOLD;
  184. else
  185. devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
  186. g_free(s);
  187. return SR_OK;
  188. }
  189. static int send_fetc(const struct sr_dev_inst *sdi)
  190. {
  191. return agdmm_send(sdi, "FETC?");
  192. }
  193. static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match)
  194. {
  195. struct dev_context *devc;
  196. struct sr_datafeed_packet packet;
  197. struct sr_datafeed_analog analog;
  198. float fvalue;
  199. char *mstr;
  200. sr_spew("FETC reply '%s'.", g_match_info_get_string(match));
  201. devc = sdi->priv;
  202. if (devc->cur_mq == -1)
  203. /* Haven't seen configuration yet, so can't know what
  204. * the fetched float means. Not really an error, we'll
  205. * get metadata soon enough. */
  206. return SR_OK;
  207. if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) {
  208. /* An invalid measurement shows up on the display as "O.L", but
  209. * comes through like this. Since comparing 38-digit floats
  210. * is rather problematic, we'll cut through this here. */
  211. fvalue = NAN;
  212. } else {
  213. mstr = g_match_info_fetch(match, 1);
  214. if (sr_atof_ascii(mstr, &fvalue) != SR_OK || fvalue == 0.0) {
  215. g_free(mstr);
  216. sr_err("Invalid float.");
  217. return SR_ERR;
  218. }
  219. g_free(mstr);
  220. if (devc->cur_divider > 0)
  221. fvalue /= devc->cur_divider;
  222. }
  223. memset(&analog, 0, sizeof(struct sr_datafeed_analog));
  224. analog.mq = devc->cur_mq;
  225. analog.unit = devc->cur_unit;
  226. analog.mqflags = devc->cur_mqflags;
  227. analog.channels = sdi->channels;
  228. analog.num_samples = 1;
  229. analog.data = &fvalue;
  230. packet.type = SR_DF_ANALOG;
  231. packet.payload = &analog;
  232. sr_session_send(devc->cb_data, &packet);
  233. devc->num_samples++;
  234. return SR_OK;
  235. }
  236. static int send_conf(const struct sr_dev_inst *sdi)
  237. {
  238. return agdmm_send(sdi, "CONF?");
  239. }
  240. static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
  241. {
  242. struct dev_context *devc;
  243. char *mstr;
  244. sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
  245. devc = sdi->priv;
  246. mstr = g_match_info_fetch(match, 1);
  247. if (!strcmp(mstr, "V")) {
  248. devc->cur_mq = SR_MQ_VOLTAGE;
  249. devc->cur_unit = SR_UNIT_VOLT;
  250. devc->cur_mqflags = 0;
  251. devc->cur_divider = 0;
  252. } else if(!strcmp(mstr, "MV")) {
  253. if (devc->mode_tempaux) {
  254. devc->cur_mq = SR_MQ_TEMPERATURE;
  255. /* No way to detect whether Fahrenheit or Celcius
  256. * is used, so we'll just default to Celcius. */
  257. devc->cur_unit = SR_UNIT_CELSIUS;
  258. devc->cur_mqflags = 0;
  259. devc->cur_divider = 0;
  260. } else {
  261. devc->cur_mq = SR_MQ_VOLTAGE;
  262. devc->cur_unit = SR_UNIT_VOLT;
  263. devc->cur_mqflags = 0;
  264. devc->cur_divider = 1000;
  265. }
  266. } else if(!strcmp(mstr, "A")) {
  267. devc->cur_mq = SR_MQ_CURRENT;
  268. devc->cur_unit = SR_UNIT_AMPERE;
  269. devc->cur_mqflags = 0;
  270. devc->cur_divider = 0;
  271. } else if(!strcmp(mstr, "UA")) {
  272. devc->cur_mq = SR_MQ_CURRENT;
  273. devc->cur_unit = SR_UNIT_AMPERE;
  274. devc->cur_mqflags = 0;
  275. devc->cur_divider = 1000000;
  276. } else if(!strcmp(mstr, "FREQ")) {
  277. devc->cur_mq = SR_MQ_FREQUENCY;
  278. devc->cur_unit = SR_UNIT_HERTZ;
  279. devc->cur_mqflags = 0;
  280. devc->cur_divider = 0;
  281. } else if(!strcmp(mstr, "RES")) {
  282. if (devc->mode_continuity) {
  283. devc->cur_mq = SR_MQ_CONTINUITY;
  284. devc->cur_unit = SR_UNIT_BOOLEAN;
  285. } else {
  286. devc->cur_mq = SR_MQ_RESISTANCE;
  287. devc->cur_unit = SR_UNIT_OHM;
  288. }
  289. devc->cur_mqflags = 0;
  290. devc->cur_divider = 0;
  291. } else if(!strcmp(mstr, "CAP")) {
  292. devc->cur_mq = SR_MQ_CAPACITANCE;
  293. devc->cur_unit = SR_UNIT_FARAD;
  294. devc->cur_mqflags = 0;
  295. devc->cur_divider = 0;
  296. } else
  297. sr_dbg("Unknown first argument.");
  298. g_free(mstr);
  299. if (g_match_info_get_match_count(match) == 4) {
  300. mstr = g_match_info_fetch(match, 3);
  301. /* Third value, if present, is always AC or DC. */
  302. if (!strcmp(mstr, "AC"))
  303. devc->cur_mqflags |= SR_MQFLAG_AC;
  304. else if (!strcmp(mstr, "DC"))
  305. devc->cur_mqflags |= SR_MQFLAG_DC;
  306. else
  307. sr_dbg("Unknown third argument.");
  308. g_free(mstr);
  309. } else
  310. devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
  311. return SR_OK;
  312. }
  313. static int recv_conf_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
  314. {
  315. struct dev_context *devc;
  316. char *mstr;
  317. sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
  318. devc = sdi->priv;
  319. mstr = g_match_info_fetch(match, 1);
  320. if (!strncmp(mstr, "VOLT", 4)) {
  321. devc->cur_mq = SR_MQ_VOLTAGE;
  322. devc->cur_unit = SR_UNIT_VOLT;
  323. devc->cur_mqflags = 0;
  324. devc->cur_divider = 0;
  325. if (mstr[4] == ':') {
  326. if (!strcmp(mstr + 4, "AC"))
  327. devc->cur_mqflags |= SR_MQFLAG_AC;
  328. else if (!strcmp(mstr + 4, "DC"))
  329. devc->cur_mqflags |= SR_MQFLAG_DC;
  330. else
  331. /* "ACDC" appears as well, no idea what it means. */
  332. devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
  333. } else
  334. devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
  335. } else if(!strcmp(mstr, "CURR")) {
  336. devc->cur_mq = SR_MQ_CURRENT;
  337. devc->cur_unit = SR_UNIT_AMPERE;
  338. devc->cur_mqflags = 0;
  339. devc->cur_divider = 0;
  340. } else if(!strcmp(mstr, "RES")) {
  341. if (devc->mode_continuity) {
  342. devc->cur_mq = SR_MQ_CONTINUITY;
  343. devc->cur_unit = SR_UNIT_BOOLEAN;
  344. } else {
  345. devc->cur_mq = SR_MQ_RESISTANCE;
  346. devc->cur_unit = SR_UNIT_OHM;
  347. }
  348. devc->cur_mqflags = 0;
  349. devc->cur_divider = 0;
  350. } else
  351. sr_dbg("Unknown first argument.");
  352. g_free(mstr);
  353. return SR_OK;
  354. }
  355. /* At least the 123x and 125x appear to have this. */
  356. static int recv_conf(const struct sr_dev_inst *sdi, GMatchInfo *match)
  357. {
  358. struct dev_context *devc;
  359. char *mstr;
  360. sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
  361. devc = sdi->priv;
  362. mstr = g_match_info_fetch(match, 1);
  363. if(!strcmp(mstr, "DIOD")) {
  364. devc->cur_mq = SR_MQ_VOLTAGE;
  365. devc->cur_unit = SR_UNIT_VOLT;
  366. devc->cur_mqflags = SR_MQFLAG_DIODE;
  367. devc->cur_divider = 0;
  368. } else
  369. sr_dbg("Unknown single argument.");
  370. g_free(mstr);
  371. return SR_OK;
  372. }
  373. /* This comes in whenever the rotary switch is changed to a new position.
  374. * We could use it to determine the major measurement mode, but we already
  375. * have the output of CONF? for that, which is more detailed. However
  376. * we do need to catch this here, or it'll show up in some other output. */
  377. static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match)
  378. {
  379. (void)sdi;
  380. sr_spew("Switch '%s'.", g_match_info_get_string(match));
  381. return SR_OK;
  382. }
  383. SR_PRIV const struct agdmm_job agdmm_jobs_u123x[] = {
  384. { 143, send_stat },
  385. { 1000, send_conf },
  386. { 143, send_fetc },
  387. { 0, NULL }
  388. };
  389. SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = {
  390. { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x },
  391. { "^\\*([0-9])$", recv_switch },
  392. { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
  393. { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x },
  394. { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x},
  395. { "^\"(DIOD)\"$", recv_conf },
  396. { NULL, NULL }
  397. };
  398. SR_PRIV const struct agdmm_job agdmm_jobs_u125x[] = {
  399. { 143, send_stat },
  400. { 1000, send_conf },
  401. { 143, send_fetc },
  402. { 0, NULL }
  403. };
  404. SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = {
  405. { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x },
  406. { "^\\*([0-9])$", recv_switch },
  407. { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
  408. { "^(VOLT|CURR|RES|CAP) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)$", recv_conf_u125x },
  409. { "^(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)$", recv_conf_u125x },
  410. { "^\"(DIOD)\"$", recv_conf },
  411. { NULL, NULL }
  412. };