|
|
@@ -0,0 +1,170 @@ |
|
|
|
#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" |
|
|
|
#include "gpib.h" |
|
|
|
#include "zoom.h" |
|
|
|
#include "math.h" |
|
|
|
#include <pthread.h> |
|
|
|
|
|
|
|
#define info(x...) fprintf(stderr,x) |
|
|
|
|
|
|
|
void dctest(int zoom, int gpib); |
|
|
|
|
|
|
|
int g_quit = 0; |
|
|
|
void handle_sig(int sig) { g_quit = 1; } |
|
|
|
|
|
|
|
int main(int argc, char *argv[]) |
|
|
|
{ |
|
|
|
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wc5-if00-port0"); |
|
|
|
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0"); |
|
|
|
int rate=500000; |
|
|
|
int getopt_index; |
|
|
|
int zoom, gpib; |
|
|
|
|
|
|
|
static struct option long_opts[] = { |
|
|
|
{ "zoom-device", required_argument, NULL, 'Z' }, |
|
|
|
{ "gpib-device", required_argument, NULL, 'G' }, |
|
|
|
{ "rate", required_argument, NULL, 'r' }, |
|
|
|
{ "help", no_argument, NULL, 'h' }, |
|
|
|
{ 0, 0, 0, 0} |
|
|
|
}; |
|
|
|
int help=0; |
|
|
|
char c; |
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "Z:G:r: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 'r': |
|
|
|
rate = atoi(optarg); |
|
|
|
if(rate == 0) |
|
|
|
errx(1, "invalid rate: %s",optarg); |
|
|
|
break; |
|
|
|
case 'h': |
|
|
|
case '?': |
|
|
|
default: |
|
|
|
help = 1; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (help) { |
|
|
|
fprintf(stderr, "Zoom Nilm DC Test Tool\n"); |
|
|
|
fprintf(stderr, "usage: %s [options]\n\n", *argv); |
|
|
|
fprintf(stderr, " -Z, --zoom-device %-9s zoom NILM serial port\n", "/dev/xxx"); |
|
|
|
fprintf(stderr, " -G, --gpib-device %-9s GPIB serial port\n", "/dev/xxx"); |
|
|
|
fprintf(stderr, " -r, --rate %-16d baud rate\n", rate); |
|
|
|
fprintf(stderr, " -h, --help this help\n"); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
signal(SIGINT, handle_sig); |
|
|
|
|
|
|
|
/* open devices */ |
|
|
|
info("Opening Zoom NILM device %s\n", zoomdev); |
|
|
|
if ((zoom = serial_open(zoomdev, rate)) == -1) |
|
|
|
err(1, "failed to open zoom device %s", zoomdev); |
|
|
|
|
|
|
|
info("Opening GPIB device %s\n", gpibdev); |
|
|
|
if ((gpib = serial_open(gpibdev, 9600)) == -1) |
|
|
|
err(1, "failed to open gpib device %s", gpibdev); |
|
|
|
|
|
|
|
/* do the dc test */ |
|
|
|
dctest(zoom, gpib); |
|
|
|
|
|
|
|
close(zoom); |
|
|
|
close(gpib); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
struct threadinfo_t { |
|
|
|
int quit_flag; |
|
|
|
int fd; |
|
|
|
}; |
|
|
|
|
|
|
|
void *read_data(void *arg) |
|
|
|
{ |
|
|
|
struct threadinfo_t *ti = (struct threadinfo_t *)arg; |
|
|
|
|
|
|
|
printf("ti->fd=%d\n", ti->fd); |
|
|
|
while (!ti->quit_flag) { |
|
|
|
printf("thread sleeping\n"); |
|
|
|
sleep(1); |
|
|
|
} |
|
|
|
printf("thread quitting\n"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
void dctest(int zoom, int gpib) |
|
|
|
{ |
|
|
|
// double idesired, iactual; |
|
|
|
// int i; |
|
|
|
// int zero; |
|
|
|
// int dac[ZOOM_SWEEP_COUNT]; |
|
|
|
// int adc[ZOOM_SWEEP_COUNT]; |
|
|
|
pthread_t thread; |
|
|
|
struct threadinfo_t threadinfo; |
|
|
|
|
|
|
|
/* Do a calibration with Keithley off */ |
|
|
|
info("Triggering calibration\n"); |
|
|
|
zoomrun_trigger_calibrate(zoom); |
|
|
|
usleep(500000); |
|
|
|
|
|
|
|
printf("zoom=%d gpib=%d\n", zoom,gpib); |
|
|
|
|
|
|
|
/* Init Keithley */ |
|
|
|
info("Initializing GPIB\n"); |
|
|
|
// if (gpib_init(gpib) < 0) { info("failed\n"); goto out1; } |
|
|
|
|
|
|
|
info("Initializing Keithley\n"); |
|
|
|
// if (gpib_addr(gpib, 24) < 0) { info("failed\n"); goto out1; } |
|
|
|
// if (keithley_init(gpib) < 0) { info("failed\n"); goto out2; } |
|
|
|
// if (keithley_current(gpib, 0) < 0) { info("failed\n"); goto out2; } |
|
|
|
// if (isnan(keithley_read(gpib))) { info("failed\n"); goto out2; } |
|
|
|
|
|
|
|
/* Start the thread that reads and dumps data */ |
|
|
|
info("Spawning thread\n"); |
|
|
|
threadinfo.quit_flag = 0; |
|
|
|
threadinfo.fd = zoom; |
|
|
|
if (pthread_create(&thread, NULL, read_data, &threadinfo) != 0) { |
|
|
|
info("failed\n"); |
|
|
|
goto out2; |
|
|
|
} |
|
|
|
|
|
|
|
/* Do another calibration now */ |
|
|
|
info("Triggering calibration\n"); |
|
|
|
zoomrun_trigger_calibrate(zoom); |
|
|
|
|
|
|
|
/* Change Keithley values */ |
|
|
|
while (!g_quit) { |
|
|
|
info("sleeping\n"); |
|
|
|
sleep(1); |
|
|
|
} |
|
|
|
printf("quitting\n"); |
|
|
|
|
|
|
|
threadinfo.quit_flag = 1; |
|
|
|
pthread_join(thread, NULL); |
|
|
|
out2: |
|
|
|
keithley_off(gpib); |
|
|
|
out1: |
|
|
|
return; |
|
|
|
} |
|
|
|
|