/* ************************************************************** */ /* * Module B9900 * This module contains I/O functions for the Bell 9900. * * Zachary Wolf * 1/14/00 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "b9900.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int b9900_open_dev(int gpib_board_addr, int gpib_dev_addr); void b9900_close_dev(int dev_ID); int b9900_check_dev_open(int dev_ID); int b9900_out(int dev_ID, char* buf); int b9900_in(int dev_ID, char* buf); int b9900_spoll(int dev_ID, int* spoll); void b9900_wait_until_not_busy(int dev_ID); void b9900_wait_until_data_ready(int dev_ID); void b9900_message(char* message); void b9900_error(char* message); /* ************************************************************** */ /* 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[B9900_MAX_NUM_DEV + 1]; static int dev_descr[B9900_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 * range_settings, port range settings saved for quick access */ static char cmd[B9900_MAX_CMD + 1]; static char msg[B9900_MAX_CMD + 1]; static char reply[B9900_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * b9900_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/14/00 */ void b9900_init(int gpib_board_addr, int gpib_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int err; int i; /* Message */ b9900_message(""); b9900_message("Initializing the B9900..."); /* Check input parameters */ if (gpib_board_addr < 0 || gpib_board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s B9900_MAX_NUM_DEV) { Fmt(msg, "%s B9900_MAX_NUM_DEV) { Fmt(msg, "%s B9900_NUM_CHANNELS) { Fmt(msg, "%s 8) { Fmt(msg, "%s B9900_MAX_NUM_DEV) { Fmt(msg, "%s B9900_NUM_CHANNELS) { Fmt(msg, "%s B9900_MAX_NUM_DEV) { Fmt(msg, "%s B9900_NUM_CHANNELS) { Fmt(msg, "%s 5) /* Avoid infinite loops */ { printf("\nProblem reading the Hall probe, returning 0 T and exiting the function.\n"); *B = 0.; return; } /* Get the field reading from the Gaussmeter */ Fmt(cmd, "%sME%i[w1]0%i[w1]%c%f[w5]", &chan, &range, &sign, &reading); if (num_scan != 4) { printf("Problem 1 reading the Gaussmeter, reply = %s, try again...\n", reply); goto try_again; } /* Check */ if (chan != channel) { printf("Problem 2 reading the Gaussmeter, reply = %s, try again...\n", reply); goto try_again; } /* Check the range and determine the multiplier */ if (range == 1) multiplier = 1.e-8; else if (range == 2) multiplier = 1.e-7; else if (range == 3) multiplier = 1.e-6; else if (range == 4) multiplier = 1.e-5; else if (range == 5) multiplier = 1.e-4; else if (range == 6) multiplier = 1.e-3; else if (range == 7) multiplier = 1.e-2; else { printf("Problem 3 reading the Gaussmeter, reply = %s, try again...\n", reply); goto try_again; } /* Check the sign */ if (sign != '-' && sign != '+' && sign != ' ' && sign != '~') { printf("Problem 4 reading the Gaussmeter, reply = %s, try again...\n", reply); goto try_again; } /* Check the reading */ if (reading < 0 || reading > 32767) { printf("Problem 5 reading the Gaussmeter, reply = %s, try again...\n", reply); goto try_again; } /* Determine the field value */ if (sign == '-') *B = -1. * reading * multiplier; else *B = reading * multiplier; /* Done */ return; } /* ************************************************************** */ /* * b9900_exit * This function configures the B9900 for program exit and * closes the device. * * Input: * dev_ID, device identifier * * Zachary Wolf * 1/21/00 */ void b9900_exit(int dev_ID) { /* Declare variables */ int dev_open; /* Check input parameters */ if (dev_ID < 1 || dev_ID > B9900_MAX_NUM_DEV) { Fmt(msg, "%s 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * b9900_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/21/00 */ int b9900_out(int dev_ID, char* buf) { /* Declare variables */ int nbytes; int err; char out_buf[B9900_MAX_CMD + 1]; char expected_reply[B9900_MAX_CMD + 1]; /* Make sure the device is ready for a command */ b9900_wait_until_not_busy(dev_ID); /* Begin the command with ESC, end with CR */ Fmt(out_buf, "%s<%c%s%c", 27, buf, 13); /* 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", buf); /* Done */ return 0; } /* ************************************************************** */ /* * b9900_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/21/00 */ int b9900_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 100) busy = FALSE; /* End loop until device is ready */ } /* Done */ return; } /* ************************************************************** */ /* * b9900_wait_until_data_ready * This function performs serial polls on the device until it determines * that the device is ready to send its data. * * Input: * dev_ID, device identifier * * Zachary Wolf * 1/21/00 */ void b9900_wait_until_data_ready(int dev_ID) { /* Declare variables */ int err; int spoll = 0; int counter = 0; enum {TRUE, FALSE} ready; /* Loop until the device has its data ready */ ready = FALSE; while (ready == FALSE) { /* Perform a serial poll */ err = b9900_spoll(dev_ID, &spoll); if (err != 0) { b9900_error("Serial poll error in b9900_wait_until_data_ready"); return; } /* The data is ready if spoll bit 6 is set */ if (spoll & 64) 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; } /* ************************************************************** */ /* * b9900_message * This function handles messages about the B9900. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void b9900_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * b9900_error * This function handles error messages for the B9900. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void b9900_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nB9900 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); }