/* ************************************************************** */ /* * Module LS450 * This module contains I/O functions for the Lake Shore 450 teslameter. * * Zachary Wolf * 1/26/00 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "ls450.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int ls450_open_dev(int gpib_board_addr, int gpib_dev_addr); void ls450_close_dev(int dev_ID); int ls450_check_dev_open(int dev_ID); int ls450_out(int dev_ID, char* buf); int ls450_in(int dev_ID, char* buf); int ls450_spoll(int dev_ID, int* spoll); void ls450_wait_until_not_busy(int dev_ID); void ls450_wait_until_data_ready(int dev_ID); int ls450_get_errors(int dev_ID); void ls450_message(char* msg); void ls450_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[LS450_MAX_NUM_DEV + 1]; static int dev_descr[LS450_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[LS450_MAX_CMD + 1]; static char msg[LS450_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * ls450_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/26/00 */ void ls450_init(int gpib_board_addr, int gpib_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int err; int i; /* Message */ ls450_message(""); ls450_message("Initializing the Lake Shore 450 Teslameter..."); /* Check input parameters */ if (gpib_board_addr < 0 || gpib_board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s LS450_MAX_NUM_DEV) { Fmt(msg, "%s LS450_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 = ls450_check_dev_open(dev_ID); if (dev_open != 1) { ls450_error("LS450 not open"); return; } /* Set the range to the requested value */ Fmt(cmd, "%s LS450_MAX_NUM_DEV) { Fmt(msg, "%s%i", &status); if (status & (4 + 8 + 16 + 32)) { printf("\nProblem zeroing the teslameter.\n"); printf("Try again..."); goto zero; } /* Check for errors */ err = ls450_get_errors(dev_ID); if (err != 0) { ls450_error("The teslameter had an error during zeroing."); ls450_close_dev(dev_ID); return; } /* Done */ return; } /* ************************************************************** */ /* * ls450_get_B * This function gets the field reading from the teslameter. * * Input: * dev_ID, device identifier * * Output: * B, field reading (T) * * Zachary Wolf * 1/26/00 */ void ls450_get_B(int dev_ID, double* B) { /* Declare variables */ int dev_open; int err; int num_scan; double field_reading; double multiplier; /* Check input parameters */ if (dev_ID < 1 || dev_ID > LS450_MAX_NUM_DEV) { Fmt(msg, "%s%f", &field_reading); if (num_scan != 1) { printf("LS450 problem getting field reading: %s\n", msg); printf("Try again...\n"); Delay(1.); goto try_again; } /* Get the multiplier */ ls450_out(dev_ID, "FIELDM?"); ls450_in(dev_ID, msg); if (msg[0] == 'u') multiplier = 1.e-6; else if (msg[0] == 'm') multiplier = 1.e-3; else if (msg[0] == 'k') multiplier = 1.e3; else if (strlen(msg) == 0) multiplier = 1.; else { printf("LS450 problem getting the multiplier: %s\n", msg); printf("Try again...\n"); Delay(1.); goto try_again; } /* Check the units */ ls450_out(dev_ID, "UNIT?"); ls450_in(dev_ID, msg); if (msg[0] != 'T') { printf("LS450 problem getting the units: %s\n", msg); printf("Try again...\n"); Delay(1.); goto try_again; } /* Check for errors */ err = ls450_get_errors(dev_ID); if (err != 0) { ls450_error("The teslameter has an error"); ls450_close_dev(dev_ID); *B = 0.; return; } /* Return the field strength */ *B = field_reading * multiplier; /* Done */ return; } /* ************************************************************** */ /* * ls450_exit * This function configures the LS450 for program exit and * closes the device. * * Input: * dev_ID, device identifier * * Zachary Wolf * 1/24/00 */ void ls450_exit(int dev_ID) { /* Declare variables */ int dev_open; int err; /* Check input parameters */ if (dev_ID < 1 || dev_ID > LS450_MAX_NUM_DEV) { Fmt(msg, "%s 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * ls450_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/26/00 */ int ls450_out(int dev_ID, char* buf) { /* Declare variables */ int nbytes; int err; char out_buf[LS450_MAX_CMD + 1]; /* Add LF to the message */ /* Add the wait-to-continue command */ Fmt(out_buf, "%s<%s;*WAI%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; } /* ************************************************************** */ /* * ls450_spoll * This function performs a serial poll on the device. * * Input: * dev_ID, device identifier * * Output: * spoll, integer encoding the serial poll information * err, 0 if ok, -1 otherwise * * Zachary Wolf * 1/26/00 */ int ls450_spoll(int dev_ID, int* spoll) { /* Declare variables */ int err; char spoll_byte; /* Perform the serial poll */ err = ibrsp(dev_descr[dev_ID], &spoll_byte); /* Check for errors */ if (err & 0x8000) { Fmt(msg, "%s%i", &status); if (num_scan != 1) { ls450_message("The LS450 teslameter has an error in ls450_wait_until_not_busy."); ls450_message(msg); return; } /* The teslameter is not busy if status bit 0 is set */ if (status & 1) busy = FALSE; else busy = TRUE; /* Check for errors */ if (status & 1); /* OK, operation complete */ if (status & 2); /* OK, not used */ if (status & 4) ls450_error("LS450: query error"); if (status & 8) ls450_error("LS450: DDE error"); if (status & 16) ls450_error("LS450: execution error"); if (status & 32) ls450_error("LS450: command error"); if (status & 64); /* OK, not used */ if (status & 128); /* OK, power on */ /* Allow time for the device to get ready */ Delay(.2); /* No endless loops */ counter++; //printf("%i\n", counter); if (counter > 100) busy = FALSE; /* End loop until device is ready */ } /* Done */ return; } /* ************************************************************** */ /* * ls450_wait_until_data_ready * This function performs checks the device until it determines * that the device is ready to send its data. * * Input: * dev_ID, device identifier * * Zachary Wolf * 1/26/00 */ void ls450_wait_until_data_ready(int dev_ID) { /* Declare variables */ int err; int status = 0; int num_scan; int counter = 0; enum {TRUE, FALSE} ready; /* Loop until the device has its data ready */ ready = FALSE; while (ready == FALSE) { /* Read the status byte register */ ls450_out(dev_ID, "*STB?"); ls450_in(dev_ID, msg); num_scan = Scan(msg, "%s>%i", &status); if (num_scan != 1) { ls450_message("The LS450 teslameter has an error in ls450_wait_until_data_ready."); ls450_message(msg); return; } /* The data is ready if status bit 0 is set */ if (status & 1) ready = TRUE; else ready = FALSE; /* Allow time for the device to get ready */ Delay(.2); /* No endless loops */ counter++; //printf("%i\n", counter); if (counter > 100) ready = TRUE; /* End loop until device is ready */ } /* Done */ return; } /* ************************************************************** */ /* * ls450_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 ls450_get_errors(int dev_ID) { /* Declare variables */ int status; int spoll; int num_scan; /* Read the standard event status register and clear it */ ls450_out(dev_ID, "*ESR?"); ls450_in(dev_ID, msg); num_scan = Scan(msg, "%s>%i", &status); if (num_scan != 1) { ls450_message("The LS450 teslameter has an error."); ls450_message(msg); return -1; } /* Debug */ //printf("ESR = %i\n", status); /* Check for errors */ if (status & 1); /* OK, operation complete */ if (status & 2); /* OK, not used */ if (status & 4) ls450_error("LS450: query error"); if (status & 8) ls450_error("LS450: DDE error"); if (status & 16) ls450_error("LS450: execution error"); if (status & 32) ls450_error("LS450: command error"); if (status & 64); /* OK, not used */ if (status & 128); /* OK, power on */ /* Perform a serial poll to read the status byte register and clear it */ ls450_spoll(dev_ID, &spoll); /* Debug */ //printf("SBR = %i\n", spoll); /* Check for errors */ /* No new errors, overload is handled in get_B function */ //if (spoll & 16) ls450_error("LS450: overload"); /* Done */ return 0; } /* ************************************************************** */ /* * ls450_message * This function handles messages about the LS450. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void ls450_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * ls450_error * This function handles error messages for the LS450. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void ls450_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nLS450 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); }