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.
 
 
 
 

209 lines
3.9 KiB

  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. int hex = 0;
  14. int dec = 0;
  15. int screen = 0;
  16. int unprocessed = 0;
  17. int process(const uint8_t *buf, int len);
  18. int main(int argc, char *argv[])
  19. {
  20. char *device=strdup("/dev/ttyUSB0");
  21. int rate=500000;
  22. int fd;
  23. int getopt_index;
  24. char buf[1024];
  25. int len;
  26. int calibrate = 1;
  27. static struct option long_opts[] = {
  28. { "device", required_argument, NULL, 'd' },
  29. { "rate", required_argument, NULL, 'r' },
  30. { "hex", no_argument, NULL, 'x' },
  31. { "dec", no_argument, NULL, 'D' },
  32. { "raw", no_argument, NULL, 'R' },
  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, " -u, --unprocessed dump raw unprocessed data\n");
  83. fprintf(stderr, " -n, --no-calibrate skip calibration routine\n");
  84. fprintf(stderr, " -s, --screen send \\r instead of \\n\n");
  85. fprintf(stderr, " -h, --help this cruft\n");
  86. return 1;
  87. }
  88. if ((fd = serial_open(device, rate)) == -1)
  89. err(1, "serial_open failed for %s", device);
  90. if (calibrate) {
  91. char c = 'c';
  92. write(fd, &c, 1);
  93. }
  94. len = 0;
  95. while (1) {
  96. int processed, n;
  97. n = read(fd, buf + len, sizeof(buf) - len);
  98. if (n <= 0)
  99. err(1, "read");
  100. len += n;
  101. processed = process((uint8_t *) buf, len);
  102. memmove(buf, buf + processed, len - processed);
  103. len -= processed;
  104. }
  105. return 1;
  106. }
  107. int process_adc_dac(const uint8_t *buf)
  108. {
  109. uint16_t dac, tmp;
  110. int16_t adc;
  111. int overflow;
  112. /* data OK? */
  113. if ((buf[0] & 0xC0) != 0)
  114. return 0;
  115. /* extract */
  116. if (buf[0] & 0x10)
  117. overflow = 1;
  118. else
  119. overflow = 0;
  120. tmp = ((buf[0] & 0x0F) << 8) | buf[1];
  121. /* sign-extend ADC value */
  122. if (tmp & 0x0800)
  123. tmp |= 0xF000;
  124. else
  125. tmp &= ~0xF000;
  126. adc = (int16_t)tmp;
  127. dac = (buf[2] << 8) | buf[3];
  128. /* send it out */
  129. if (hex) {
  130. printf("%04x %03x", dac, adc & 0x0FFF);
  131. } else if (dec) {
  132. printf("%d %d", dac, adc & 0x0FFF);
  133. } else {
  134. printf("DAC: %5d ADC: % 5d Total: xxx", dac, adc);
  135. if (overflow)
  136. printf(" **** adc may have clamped");
  137. }
  138. if (screen)
  139. printf("\033[K\r");
  140. else
  141. printf("\n");
  142. return 1;
  143. }
  144. int process_calibration(const uint8_t *buf)
  145. {
  146. float f = *(float *)buf;
  147. printf("got calibration value: %f\n", f);
  148. return 1;
  149. }
  150. int process(const uint8_t *buf, int len)
  151. {
  152. int n = 0;
  153. if (unprocessed) {
  154. n = write(fileno(stdout), buf, len);
  155. if (n >= 0)
  156. return n;
  157. return 0;
  158. }
  159. /* Process blocks */
  160. retry:
  161. for (; (n + 5) <= len; buf += 5, n += 5) {
  162. int ok = 0;
  163. switch (buf[0]) {
  164. case 0xA0:
  165. if (process_adc_dac(buf + 1)) ok = 1;
  166. break;
  167. case 0xA1:
  168. if (process_calibration(buf + 1)) ok = 1;
  169. break;
  170. default:
  171. break;
  172. }
  173. if (!ok) {
  174. /* badly formed data; eat one byte and retry */
  175. buf++;
  176. n++;
  177. goto retry;
  178. }
  179. }
  180. return n;
  181. }