Browse Source

add better gpib routines

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@7605 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 12 years ago
parent
commit
3b3cf31f51
5 changed files with 105 additions and 75 deletions
  1. +5
    -3
      pc/Makefile
  2. +15
    -70
      pc/calibrate.c
  3. +70
    -0
      pc/gpib.c
  4. +13
    -0
      pc/gpib.h
  5. +2
    -2
      pc/serial-util.h

+ 5
- 3
pc/Makefile View File

@@ -1,13 +1,15 @@
CFLAGS=-Wall -Werror
CFLAGS=-std=c99 -Wall -Werror

all: read calibrate

serial-util.o: serial-util.h

gpib.o: serial-util.h

read.o: serial-util.h

calibrate.o: serial-util.h
calibrate.o: serial-util.h gpib.h

read: read.o serial-util.o

calibrate: calibrate.o serial-util.o
calibrate: calibrate.o serial-util.o gpib.o

+ 15
- 70
pc/calibrate.c View File

@@ -11,11 +11,9 @@
#include <linux/serial.h>
#include <sys/signal.h>
#include "serial-util.h"
#include "gpib.h"

int zoom, gpib;

void calibrate(void);

void calibrate(int zoom, int gpib);
#define info(x...) fprintf(stderr,x)

int quit = 0;
@@ -29,6 +27,7 @@ int main(int argc, char *argv[])
char *zoomdev=strdup("/dev/ttyUSB1");
char *gpibdev=strdup("/dev/ttyUSB0");
int getopt_index;
int zoom, gpib;

static struct option long_opts[] = {
{ "zoom-device", required_argument, NULL, 'z' },
@@ -39,16 +38,6 @@ int main(int argc, char *argv[])
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)
@@ -84,75 +73,31 @@ int main(int argc, char *argv[])
errx(1, "gpib open failed");

signal(SIGINT, handle_sig);
calibrate();
calibrate(zoom, gpib);

close(zoom);
close(gpib);
return 0;
}

void gputs(char *s)
void calibrate(int zoom, int gpib)
{
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);
}
float i;

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 GPIB\n");
gpib_init(gpib);

info("Initializing Keithley\n");
keithley_on();
gpib_addr(gpib, 24);
keithley_init(gpib);

info("Sweep\n");
for (ma = -1000; ma <= 1000; ma += 10) {
keithley_current(ma);
usleep(100000);
for (i = -1.0; i <= 1.0; i += 0.01) {
keithley_current(gpib, i);
printf("requested %f, actual %f\n", i, keithley_read(gpib));
usleep(10000);
if (quit) break;
}

keithley_off();
keithley_off(gpib);
}

+ 70
- 0
pc/gpib.c View File

@@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "gpib.h"
#include "serial-util.h"

#define gputv(string, rv) do { \
int ____l = strlen(string); \
if (safewrite(fd, string, ____l) != ____l) return rv; \
if (safewrite(fd, "\r", 1) != 1) return rv; } while(0)
#define gput(string) gputv(string, -1)
int gpib_init(int fd)
{
gput("++mode 1");
gput("++auto 0");
gput("++eos 1");
gput("++eoi 1");
gput("++read_tmo_ms 4000");
gput("++eot_enable 1");
gput("++eot_char 13");
return 0;
}

int gpib_addr(int fd, int addr)
{
char s[128];
sprintf(s, "++addr %d", addr);
gput(s);
return 0;
}

int keithley_init(int fd)
{
gput(":syst:beep:stat 1");
gput(":sour:func:mode curr");
gput(":sour:del 0");
gput(":sour:curr 0");
gput(":sour:curr:range:auto on");
gput(":outp on");
return 0;
}

int keithley_current(int fd, float amps)
{
char s[128];
sprintf(s, ":sour:curr %0.3f", amps);
gput(s);
return 0;
}

float keithley_read(int fd)
{
char s[128];
float i, dummy;
gputv("read?", NAN);
gputv("++read", NAN);
if (fdgets(s, 128, fd, 1000) == NULL)
return NAN;
if (sscanf(s, "%f,%f,", &dummy, &i) != 2)
return NAN;
return i;
}

int keithley_off(int fd)
{
gput(":outp off");
return 0;
}


+ 13
- 0
pc/gpib.h View File

@@ -0,0 +1,13 @@
#ifndef GPIB_H
#define GPIB_H

int gpib_init(int fd);
int gpib_addr(int fd, int addr);

int keithley_init(int fd);
int keithley_current(int fd, float amps);
float keithley_read(int fd);
int keithley_off(int fd);

#endif


+ 2
- 2
pc/serial-util.h View File

@@ -4,8 +4,8 @@
* Jim Paris <jim@jtan.com>
* $Id$
*/
#ifndef UTIL_H
#define UTIL_H
#ifndef SERIAL_UTIL_H
#define SERIAL_UTIL_H

#include <unistd.h>



Loading…
Cancel
Save