|
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <stdint.h>
- #include <string.h>
- #include <syslog.h>
- #include <err.h>
- #include <linux/serial.h>
- #include <sys/signal.h>
- #include "serial-util.h"
-
- int zoom, gpib;
-
- void calibrate(void);
-
- #define info(x...) fprintf(stderr,x)
-
- int quit = 0;
- void handle_sig(int sig)
- {
- quit = 1;
- }
-
- int main(int argc, char *argv[])
- {
- char *zoomdev=strdup("/dev/ttyUSB1");
- char *gpibdev=strdup("/dev/ttyUSB0");
- int getopt_index;
-
- static struct option long_opts[] = {
- { "zoom-device", required_argument, NULL, 'z' },
- { "gpib-device", required_argument, NULL, 'g' },
- { "help", no_argument, NULL, 'h' },
- { 0, 0, 0, 0 }
- };
- int help=0;
- char c;
-
- {
- char buf[1024];
- drain(0);
- fdprintf(1,"type a string in 5 sec: ");
- fdgets(buf, 1024, 0, 5000);
- chomp(buf);
- fdprintf(1,"you typed: '%s' len %d\n", buf, strlen(buf));
- abort();
- }
-
- while ((c = getopt_long(argc, argv, "z:g:h?",
- long_opts, &getopt_index)) != -1) {
- switch(c)
- {
- case 'z':
- free(zoomdev);
- zoomdev = strdup(optarg);
- break;
- case 'g':
- free(gpibdev);
- gpibdev = strdup(optarg);
- break;
- case 'h':
- case '?':
- default:
- help = 1;
- break;
- }
- }
-
- if (help) {
- fprintf(stderr, "Zoom Nilm Calibration Tool\n");
- fprintf(stderr, "usage: %s [options]\n\n", *argv);
- fprintf(stderr, " -z, --zoom-device %-14s zoom NILM serial port\n", zoomdev);
- fprintf(stderr, " -g, --gpib-device %-14s GPIB serial port\n", gpibdev);
- fprintf(stderr, " -h, --help this help\n");
- return 1;
- }
-
- if ((zoom = serial_open(zoomdev, 115200)) == -1)
- errx(1, "zoom open failed");
- if ((gpib = serial_open(gpibdev, 9600)) == -1)
- errx(1, "gpib open failed");
-
- signal(SIGINT, handle_sig);
- calibrate();
-
- close(zoom);
- close(gpib);
- return 0;
- }
-
- void gputs(char *s)
- {
- info("%s\n", s);
- if (write(gpib, s, strlen(s)) != strlen(s) ||
- write(gpib, "\r", 1) != 1)
- err(1, "error writing GPIB string '%s'", s);
- }
-
- void keithley_on(void)
- {
- gputs("++mode 1");
- gputs("++auto 0");
- gputs("++eos 1");
- gputs("++eoi 1");
- gputs("++read_tmo_ms 4000");
- gputs("++eot_enable 1");
- gputs("++eot_char 13");
- gputs("++addr 24");
- gputs(":syst:beep:stat 0");
- gputs(":sour:func:mode curr");
- gputs(":sour:del 0");
- gputs(":sour:curr 0");
- gputs(":sour:curr:range:auto on");
- gputs(":outp on");
- }
-
- void keithley_current(int ma)
- {
- char s[128];
- sprintf(s, ":sour:curr %0.3f", ma / 1000.0);
- gputs(s);
- }
-
- void keithley_off(void)
- {
- gputs(":outp off");
- }
-
- void zputc(char ch)
- {
- if (write(zoom, &ch, 1) != 1)
- err(1, "zoom write");
- }
-
- void zero(void)
- {
- zputc('z');
- }
-
- void calibrate(void)
- {
- int ma;
-
- info("Initializing Keithley\n");
- keithley_on();
-
- info("Sweep\n");
- for (ma = -1000; ma <= 1000; ma += 10) {
- keithley_current(ma);
- usleep(100000);
- if (quit) break;
- }
-
- keithley_off();
- }
|