/* ************************************************************** */ /* * Module GP3 * This module contains I/O functions for the Group3 teslameter. * * Zachary Wolf * 1/24/00 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "gp3.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int gp3_open_dev(int gpib_board_addr, int gpib_dev_addr); void gp3_close_dev(int dev_ID); int gp3_check_dev_open(int dev_ID); int gp3_out(int dev_ID, char* buf); int gp3_in(int dev_ID, char* buf); int gp3_get_errors(int dev_ID); void gp3_message(char* msg); void gp3_error(char* msg); /* ************************************************************** */ /* PRIVATE DEVICE TABLE */ /* * This table allows several devices of the same type to be * used in the system. * dev_addr, contains the GPIB addresses of opened devices * dev_descr, contains the device descriptors of opened devices * dev_count, contains the number of devices open of this type */ static int dev_addr[GP3_MAX_NUM_DEV + 1]; static int dev_descr[GP3_MAX_NUM_DEV + 1]; static int dev_count; /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ /* * cmd, buffer for GPIB I/O strings * msg, buffer for message strings to Standard I/O */ static char cmd[GP3_MAX_CMD + 1]; static char msg[GP3_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * gp3_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Input: * gpib_board_addr, address of the GPIB board the device is connected to (0, 1, ...) * gpib_dev_addr, GPIB address of the device (1 to 30, 0 is reserved) * * Output: * dev_ID, identifier for future references to the device * * Zachary Wolf * 1/24/00 */ void gp3_init(int gpib_board_addr, int gpib_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int err; int i; /* Message */ gp3_message(""); gp3_message("Initializing the Group3 Teslameter..."); /* Check input parameters */ if (gpib_board_addr < 0 || gpib_board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s GP3_MAX_NUM_DEV) { Fmt(msg, "%s GP3_MAX_NUM_DEV) { Fmt(msg, "%s 3) { Fmt(msg, "%s 3) range = 3; } /* Exit here if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = gp3_check_dev_open(dev_ID); if (dev_open != 1) { gp3_error("GP3 not open"); return; } /* Set the range to the requested value */ Fmt(cmd, "%s GP3_MAX_NUM_DEV) { Fmt(msg, "%s%i", &init_range); if (num_scan != 1) { gp3_message("The Group3 teslameter has an error during zeroing."); gp3_message("The range may change."); gp3_message(msg); init_range = 3; } /* Zero the teslameter on each range */ for (range = 0; range <= 3; range++) { gp3_set_range(dev_ID, range); gp3_out(dev_ID, "Z"); Delay(2.); } /* Leave the teslameter with its original range setting */ gp3_set_range(dev_ID, init_range); /* Check for errors */ err = gp3_get_errors(dev_ID); if (err != 0) { gp3_error("The teslameter has an error"); gp3_close_dev(dev_ID); return; } /* Done */ return; } /* ************************************************************** */ /* * gp3_get_B * This function gets the field reading from the teslameter. * * Input: * dev_ID, device identifier * * Output: * B, field reading (T) * * Zachary Wolf * 1/24/00 */ void gp3_get_B(int dev_ID, double* B) { /* Declare variables */ int dev_open; int err; int num_scan; /* Check input parameters */ if (dev_ID < 1 || dev_ID > GP3_MAX_NUM_DEV) { Fmt(msg, "%s%f", B); if (num_scan != 1) { printf("GP3 problem getting field reading: %s\n", msg); printf("Returning B = 0. T\n"); printf("%c", 7); *B = 0.; } /* Check for errors */ err = gp3_get_errors(dev_ID); if (err != 0) { gp3_error("The teslameter has an error"); gp3_close_dev(dev_ID); return; } /* Done */ return; } /* ************************************************************** */ /* * gp3_exit * This function configures the GP3 for program exit and * closes the device. * * Input: * dev_ID, device identifier * * Zachary Wolf * 1/24/00 */ void gp3_exit(int dev_ID) { /* Declare variables */ int dev_open; int err; /* Check input parameters */ if (dev_ID < 1 || dev_ID > GP3_MAX_NUM_DEV) { Fmt(msg, "%s 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * gp3_out * This function writes a buffer of data to the device. * * Input: * dev_ID, device identifier * buf, null terminated string, the contents up to \0 are sent * * Output: * err, 0 if ok, -1 otherwise * * Zachary Wolf * 1/24/00 */ int gp3_out(int dev_ID, char* buf) { /* Declare variables */ int nbytes; int err; char out_buf[GP3_MAX_CMD + 1]; /* Add LF to the message */ Fmt(out_buf, "%s<%s%c", buf, 10); /* Send the message */ nbytes = StringLength(out_buf); err = ibwrt(dev_descr[dev_ID], out_buf, (long)nbytes); /* Check for errors */ if (err & 0x8000) { Fmt(msg, "%s%s[t-]", buf); /* Done */ return 0; } /* ************************************************************** */ /* * gp3_get_errors * This function obtains device errors. * The operator is alerted if there is an error. * * Input: * dev_ID, device identifier * * Output: * err, 0 if no errors, -1 otherwise * * Zachary Wolf * 12/20/99 */ int gp3_get_errors(int dev_ID) { /* Declare variables */ int status; int num_scan; /* Check the response to a query */ gp3_out(dev_ID, "IR"); gp3_in(dev_ID, msg); /* Check the response */ num_scan = Scan(msg, "%s>%i", &status); if (num_scan != 1) { gp3_message("The Group3 teslameter has an error."); gp3_message(msg); return -1; } /* Done */ return 0; } /* ************************************************************** */ /* * gp3_message * This function handles messages about the GP3. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void gp3_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * gp3_error * This function handles error messages for the GP3. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void gp3_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nGP3 ERROR: %s\n", message); Beep(); Delay(.5); Beep(); /* Terminate the program if the operator desires */ printf("Press ENTER to continue.\nPress any key then ENTER to terminate program.\n"); fgets(buf, 80, stdin); if (buf[0] == '\n') return; else exit(0); }