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.
 
 
 
 

135 lines
3.0 KiB

  1. /* Test DAC for stability etc */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <getopt.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <syslog.h>
  11. #include <err.h>
  12. #include <linux/serial.h>
  13. #include <sys/signal.h>
  14. #include "serial-util.h"
  15. #include "gpib.h"
  16. #include "zoom.h"
  17. #include <math.h>
  18. #include "mt19937ar.h"
  19. void write_random(int zoom, int gpib);
  20. #define info(x...) fprintf(stderr,x)
  21. int g_quit = 0;
  22. void handle_sig(int sig) { g_quit = 1; }
  23. int main(int argc, char *argv[])
  24. {
  25. char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wc5-if00-port0");
  26. char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
  27. int getopt_index;
  28. int zoom, gpib;
  29. unsigned long seed = 1337;
  30. static struct option long_opts[] = {
  31. { "zoom-device", required_argument, NULL, 'Z' },
  32. { "gpib-device", required_argument, NULL, 'G' },
  33. { "seed", required_argument, NULL, 's' },
  34. { "help", no_argument, NULL, 'h' },
  35. { 0, 0, 0, 0}
  36. };
  37. int help=0;
  38. char c;
  39. while ((c = getopt_long(argc, argv, "Z:G:s:h?",
  40. long_opts, &getopt_index)) != -1) {
  41. switch(c)
  42. {
  43. case 'Z':
  44. free(zoomdev);
  45. zoomdev = strdup(optarg);
  46. break;
  47. case 'G':
  48. free(gpibdev);
  49. gpibdev = strdup(optarg);
  50. break;
  51. case 's':
  52. seed = atol(optarg);
  53. if(seed == 0)
  54. errx(1, "invalid seed: %s", 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 DC Test\n");
  65. fprintf(stderr, "usage: %s [options]\n\n", *argv);
  66. fprintf(stderr, " -Z, --zoom-device %-14s zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
  67. fprintf(stderr, " -G, --gpib-device %-14s GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
  68. fprintf(stderr, " -s, --seed %-16ld random seed\n", seed);
  69. fprintf(stderr, " -h, --help this help\n");
  70. return 1;
  71. }
  72. signal(SIGINT, handle_sig);
  73. info("Initializing twister with seed %ld\n", seed);
  74. init_genrand(seed);
  75. info("Opening Zoom NILM device %s\n", zoomdev);
  76. if ((zoom = serial_open(zoomdev, 115200)) == -1)
  77. err(1, "failed to open zoom device %s", zoomdev);
  78. info("Opening GPIB device %s\n", gpibdev);
  79. if ((gpib = serial_open(gpibdev, 9600)) == -1)
  80. err(1, "failed to open gpib device %s", gpibdev);
  81. /* do it */
  82. write_random(zoom, gpib);
  83. close(zoom);
  84. close(gpib);
  85. return 0;
  86. }
  87. void write_random(int zoom, int gpib)
  88. {
  89. int dac;
  90. double meas;
  91. info("Initializing Zoom NILM\n");
  92. if (zoom_init_nozero(zoom) < 0) goto fail;
  93. info("Initializing GPIB\n");
  94. if (gpib_init(gpib) < 0) goto fail;
  95. info("Initializing Keithley\n");
  96. if (gpib_addr(gpib, 23) < 0) goto fail;
  97. if (keithley2002_init_volts(gpib) < 0) goto fail;
  98. if (isnan(keithley2002_read(gpib))) goto fail;
  99. info("Running\n");
  100. while (!g_quit) {
  101. dac = genrand_int32() & 0x3ff;
  102. meas = keithley2002_read(gpib);
  103. if (isnan(meas))
  104. goto fail;
  105. printf("%d %.12f\n", dac, meas);
  106. }
  107. safecleanup:
  108. return;
  109. fail:
  110. info("Failed\n");
  111. goto safecleanup;
  112. }