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.1 KiB

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