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.
 
 
 
 

159 lines
2.8 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 <string.h>
  9. #include <syslog.h>
  10. #include <err.h>
  11. #include <linux/serial.h>
  12. #include <sys/signal.h>
  13. #include "serial-util.h"
  14. int zoom, gpib;
  15. void calibrate(void);
  16. #define info(x...) fprintf(stderr,x)
  17. int quit = 0;
  18. void handle_sig(int sig)
  19. {
  20. quit = 1;
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. char *zoomdev=strdup("/dev/ttyUSB1");
  25. char *gpibdev=strdup("/dev/ttyUSB0");
  26. int getopt_index;
  27. static struct option long_opts[] = {
  28. { "zoom-device", required_argument, NULL, 'z' },
  29. { "gpib-device", required_argument, NULL, 'g' },
  30. { "help", no_argument, NULL, 'h' },
  31. { 0, 0, 0, 0 }
  32. };
  33. int help=0;
  34. char c;
  35. {
  36. char buf[1024];
  37. drain(0);
  38. fdprintf(1,"type a string in 5 sec: ");
  39. fdgets(buf, 1024, 0, 5000);
  40. chomp(buf);
  41. fdprintf(1,"you typed: '%s' len %d\n", buf, strlen(buf));
  42. abort();
  43. }
  44. while ((c = getopt_long(argc, argv, "z:g:h?",
  45. long_opts, &getopt_index)) != -1) {
  46. switch(c)
  47. {
  48. case 'z':
  49. free(zoomdev);
  50. zoomdev = strdup(optarg);
  51. break;
  52. case 'g':
  53. free(gpibdev);
  54. gpibdev = strdup(optarg);
  55. break;
  56. case 'h':
  57. case '?':
  58. default:
  59. help = 1;
  60. break;
  61. }
  62. }
  63. if (help) {
  64. fprintf(stderr, "Zoom Nilm Calibration Tool\n");
  65. fprintf(stderr, "usage: %s [options]\n\n", *argv);
  66. fprintf(stderr, " -z, --zoom-device %-14s zoom NILM serial port\n", zoomdev);
  67. fprintf(stderr, " -g, --gpib-device %-14s GPIB serial port\n", gpibdev);
  68. fprintf(stderr, " -h, --help this help\n");
  69. return 1;
  70. }
  71. if ((zoom = serial_open(zoomdev, 115200)) == -1)
  72. errx(1, "zoom open failed");
  73. if ((gpib = serial_open(gpibdev, 9600)) == -1)
  74. errx(1, "gpib open failed");
  75. signal(SIGINT, handle_sig);
  76. calibrate();
  77. close(zoom);
  78. close(gpib);
  79. return 0;
  80. }
  81. void gputs(char *s)
  82. {
  83. info("%s\n", s);
  84. if (write(gpib, s, strlen(s)) != strlen(s) ||
  85. write(gpib, "\r", 1) != 1)
  86. err(1, "error writing GPIB string '%s'", s);
  87. }
  88. void keithley_on(void)
  89. {
  90. gputs("++mode 1");
  91. gputs("++auto 0");
  92. gputs("++eos 1");
  93. gputs("++eoi 1");
  94. gputs("++read_tmo_ms 4000");
  95. gputs("++eot_enable 1");
  96. gputs("++eot_char 13");
  97. gputs("++addr 24");
  98. gputs(":syst:beep:stat 0");
  99. gputs(":sour:func:mode curr");
  100. gputs(":sour:del 0");
  101. gputs(":sour:curr 0");
  102. gputs(":sour:curr:range:auto on");
  103. gputs(":outp on");
  104. }
  105. void keithley_current(int ma)
  106. {
  107. char s[128];
  108. sprintf(s, ":sour:curr %0.3f", ma / 1000.0);
  109. gputs(s);
  110. }
  111. void keithley_off(void)
  112. {
  113. gputs(":outp off");
  114. }
  115. void zputc(char ch)
  116. {
  117. if (write(zoom, &ch, 1) != 1)
  118. err(1, "zoom write");
  119. }
  120. void zero(void)
  121. {
  122. zputc('z');
  123. }
  124. void calibrate(void)
  125. {
  126. int ma;
  127. info("Initializing Keithley\n");
  128. keithley_on();
  129. info("Sweep\n");
  130. for (ma = -1000; ma <= 1000; ma += 10) {
  131. keithley_current(ma);
  132. usleep(100000);
  133. if (quit) break;
  134. }
  135. keithley_off();
  136. }