You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

read.c 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. #include <stdint.h>
  8. #include <syslog.h>
  9. #include <err.h>
  10. #include <linux/serial.h>
  11. #include "serial-util.h"
  12. #include <string.h>
  13. #include <ctype.h>
  14. int hex = 0;
  15. int dec = 0;
  16. int screen = 0;
  17. int unprocessed = 0;
  18. int process(const uint8_t *buf, int len);
  19. int main(int argc, char *argv[])
  20. {
  21. char *device=strdup("/dev/serial/by-id/usb-FTDI_FT232R_"
  22. "USB_UART_A6007wc5-if00-port0");
  23. int rate=500000;
  24. int fd;
  25. int getopt_index;
  26. char buf[1024];
  27. int len;
  28. int calibrate = 1;
  29. static struct option long_opts[] = {
  30. { "device", required_argument, NULL, 'd' },
  31. { "rate", required_argument, NULL, 'r' },
  32. { "hex", no_argument, NULL, 'x' },
  33. { "dec", no_argument, NULL, 'D' },
  34. { "unprocessed", no_argument, NULL, 'u' },
  35. { "no-calibrate", no_argument, NULL, 'n' },
  36. { "screen", no_argument, NULL, 's' },
  37. { "help", no_argument, NULL, 'h' },
  38. { 0, 0, 0, 0 }
  39. };
  40. int help=0;
  41. char c;
  42. while ((c = getopt_long(argc, argv, "d:r:xDunsh?",
  43. long_opts, &getopt_index)) != -1) {
  44. switch(c)
  45. {
  46. case 'd':
  47. free(device);
  48. device = strdup(optarg);
  49. break;
  50. case 'r':
  51. rate = atoi(optarg);
  52. if(rate == 0)
  53. errx(1, "invalid rate: %s",optarg);
  54. break;
  55. case 'x':
  56. hex = 1;
  57. break;
  58. case 'D':
  59. dec = 1;
  60. break;
  61. case 'u':
  62. unprocessed = 1;
  63. break;
  64. case 'n':
  65. calibrate = 0;
  66. break;
  67. case 's':
  68. screen = 1;
  69. break;
  70. case 'h':
  71. case '?':
  72. default:
  73. help = 1;
  74. break;
  75. }
  76. }
  77. if (help) {
  78. fprintf(stderr, "Zoom Nilm Client\n");
  79. fprintf(stderr, "usage: %s [options]\n\n", *argv);
  80. fprintf(stderr, " -d, --device %-14s serial port\n", device);
  81. fprintf(stderr, " -r, --rate %-16d baud rate\n", rate);
  82. fprintf(stderr, " -x, --hex hex out\n");
  83. fprintf(stderr, " -D, --dec dec out\n");
  84. fprintf(stderr, " -u, --unprocessed "
  85. "dump raw unprocessed data\n");
  86. fprintf(stderr, " -n, --no-calibrate "
  87. "skip calibration routine\n");
  88. fprintf(stderr, " -s, --screen "
  89. "send \\r instead of \\n\n");
  90. fprintf(stderr, " -h, --help this cruft\n");
  91. return 1;
  92. }
  93. if ((fd = serial_open(device, rate)) == -1)
  94. err(1, "serial_open failed for %s", device);
  95. if (calibrate) {
  96. fprintf(stderr, "performing calibration\n");
  97. char c = 'c';
  98. write(fd, &c, 1);
  99. drain(fd);
  100. }
  101. len = 0;
  102. while (1) {
  103. int processed, n;
  104. n = read(fd, buf + len, sizeof(buf) - len);
  105. if (n <= 0)
  106. err(1, "read");
  107. len += n;
  108. processed = process((uint8_t *) buf, len);
  109. memmove(buf, buf + processed, len - processed);
  110. len -= processed;
  111. }
  112. return 1;
  113. }
  114. int process_adc_dac(const uint8_t *buf)
  115. {
  116. uint16_t dac, tmp;
  117. int16_t adc;
  118. int overflow;
  119. /* data OK? */
  120. if ((buf[0] & 0xC0) != 0)
  121. return 0;
  122. /* extract */
  123. if (buf[0] & 0x10)
  124. overflow = 1;
  125. else
  126. overflow = 0;
  127. tmp = ((buf[0] & 0x0F) << 8) | buf[1];
  128. /* sign-extend ADC value */
  129. if (tmp & 0x0800)
  130. tmp |= 0xF000;
  131. else
  132. tmp &= ~0xF000;
  133. adc = (int16_t)tmp;
  134. dac = (buf[2] << 8) | buf[3];
  135. /* send it out */
  136. if (hex) {
  137. printf("%04x %03x", dac, adc & 0x0FFF);
  138. } else if (dec) {
  139. printf("%d %d", dac, adc);
  140. } else {
  141. printf("DAC: %5d ADC: % 5d Total: xxx", dac, adc);
  142. if (overflow)
  143. printf(" **** adc may have clamped");
  144. }
  145. if (screen)
  146. printf("\033[K\r");
  147. else
  148. printf("\n");
  149. return 1;
  150. }
  151. int process_calibration(const uint8_t *buf)
  152. {
  153. float f = *(float *)buf;
  154. fprintf(stderr, "got calibration value: %f\n", f);
  155. return 1;
  156. }
  157. int process(const uint8_t *buf, int len)
  158. {
  159. int n = 0;
  160. if (unprocessed) {
  161. n = write(fileno(stdout), buf, len);
  162. if (n >= 0)
  163. return n;
  164. return 0;
  165. }
  166. /* Process blocks */
  167. retry:
  168. for (; (n + 5) <= len; buf += 5, n += 5) {
  169. int ok = 0;
  170. switch (buf[0]) {
  171. case 0xA0:
  172. if (process_adc_dac(buf + 1)) ok = 1;
  173. break;
  174. case 0xA1:
  175. if (process_calibration(buf + 1)) ok = 1;
  176. break;
  177. default:
  178. break;
  179. }
  180. if (!ok) {
  181. /* badly formed data; eat one byte and retry */
  182. fprintf(stderr,"throwing away 0x%02x '%c'\n",
  183. buf[0], isprint(buf[0]) ? buf[0] : '.');
  184. buf++;
  185. n++;
  186. goto retry;
  187. }
  188. }
  189. return n;
  190. }