/* ************************************************************** */ /* * Module PT2025 * This module contains I/O functions for the Metrolab precision * teslameter model 2025. * * Zachary Wolf * 1/8/03 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include #include "pt2025.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int pt2025_open_dev(int gpib_board_addr, int gpib_dev_addr); void pt2025_close_dev(int dev_ID); int pt2025_check_dev_open(int dev_ID); int pt2025_out(int dev_ID, char* buf); int pt2025_in(int dev_ID, char* buf); int pt2025_get_errors(int dev_ID); void pt2025_get_status(int dev_ID, int reg, char* status); void pt2025_calc_status_val(char status[], int* status_val); int pt2025_serial_poll(int dev_ID, int* spoll); void pt2025_message(char* msg); void pt2025_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[PT2025_MAX_NUM_DEV + 1]; static int dev_descr[PT2025_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[PT2025_MAX_CMD + 1]; static char msg[PT2025_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * pt2025_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: * ID, identifier for future references to the device * * Zachary Wolf * 1/8/03 */ void pt2025_init(int gpib_board_addr, int gpib_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int i; char status[PT2025_MAX_CMD]; int status_val; char buf[80]; /* Message */ pt2025_message(""); pt2025_message("Initializing the PT2025..."); /* Check input parameters */ if (gpib_board_addr < 0 || gpib_board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s%c%f%c", lock_status, meas_val, units); if (num_scan != 3) { pt2025_message("Unable to extract field value"); *lock_status = 'X'; *meas_val = 0.; *units = 'X'; } /* Check for errors */ err = pt2025_get_errors(dev_ID); if (err != 0) { pt2025_error("Problem with PT2025"); return; } /* Done */ return; } /* ************************************************************** */ /* * pt2025_get_b * This function gets a field reading from the teslameter. * * Input: * dev_ID, device identifier * * Output: * b, field reading (T) * * Zachary Wolf * 1/8/03 */ void pt2025_get_b(int dev_ID, double* b) { /* Declare variables */ int dev_open; int err; char lock_status, units; /* Check input parameters */ if (dev_ID < 1 || dev_ID > PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s 8) { Fmt(msg, "%s b_max) { Fmt(msg, "%s PT2025_MAX_NUM_DEV) { Fmt(msg, "%s 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * pt2025_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/8/03 */ int pt2025_out(int dev_ID, char* buf) { /* Declare variables */ char out_buf[PT2025_MAX_CMD]; int nbytes; int err; /* Add CR LF to the command */ Fmt(out_buf, "%s<%s%c%c", buf, 13, 10); /* Send the command */ 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; } /* ************************************************************** */ /* * pt2025_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 * 1/8/03 */ int pt2025_get_errors(int dev_ID) { /* Declare variables */ char status[PT2025_MAX_CMD]; int status_val; int err; /* Default return value, no error */ err = 0; /* Register 1 */ pt2025_get_status(dev_ID, 1, status); pt2025_calc_status_val(status, &status_val); /* Register 1, bit 2, value 4, syntax error */ if ((status_val & 4) == 4) { pt2025_error("Syntax error"); err = -1; } /* Register 1, bit 3, value 8, hard error */ if ((status_val & 8) == 8) { pt2025_error("Hard error"); err = -1; } /* Register 2 */ /* No error bits */ /* Register 3 */ /* No error bits */ /* Register 4 */ /* No error bits */ /* Done */ return err; } /* ************************************************************** */ /* * pt2025_serial_poll * This function performs a serial poll of the device. * * Input: * dev_ID, device identifier * * Output: * spoll, contents of the serial poll register * err, 0 if ok, -1 otherwise * * Zachary Wolf * 1/8/03 */ int pt2025_serial_poll(int dev_ID, int* spoll) { /* Declare variables */ int err; char spoll_char; /* Perform the serial poll */ err = ibrsp(dev_descr[dev_ID], &spoll_char); if (err & 0x8000) { Fmt(msg, "%s%i", spoll); /* Debug */ /* printf("%c %i\n", spoll_char, *spoll); */ /* Done */ return 0; } /* ************************************************************** */ /* * pt2025_get_status * This function gets the contents of a status register. * * Input: * dev_ID, device identifier * reg, register number * * Output: * status, contents of the status register * * Zachary Wolf * 1/8/03 */ void pt2025_get_status(int dev_ID, int reg, char* status) { /* Declare variables */ char status_cmd[PT2025_MAX_CMD]; /* Check the input parameters */ if (reg < 1 || reg > 4) { Fmt(status_cmd, "%sS%x", status_val); /* Debug */ /* printf("status = %s, value = %i\n", status, *status_val); */ /* Done */ return; } /* ************************************************************** */ /* * pt2025_message * This function handles messages about the PT2025. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 8/3/98 */ void pt2025_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * pt2025_error * This function handles error messages for the PT2025. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 8/3/98 */ void pt2025_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nPT2025 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); }