/* ************************************************************** */ /* * Module EF11PR * This module contains I/O functions for the Mitutoyo model * EF11PR counters. * * Zachary Wolf * 11/29/01 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include #include "ef11pr.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int ef11pr_open_dev(int rs232_dev_addr); void ef11pr_close_dev(int dev_ID); int ef11pr_check_dev_open(int dev_ID); int ef11pr_out(int dev_ID, char* buf); int ef11pr_in(int dev_ID, char* buf); int ef11pr_get_errors(int dev_ID); void ef11pr_message(char* msg); void ef11pr_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 RS232 addresses of opened devices * dev_count, contains the number of devices open of this type */ static int dev_addr[EF11PR_MAX_NUM_DEV + 1]; static int dev_count; /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ /* * com_port, port number for I/O * cmd, buffer for RS232 I/O strings * msg, buffer for message strings to Standard I/O */ static int com_port; static char cmd[EF11PR_MAX_CMD + 1]; static char msg[EF11PR_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * ef11pr_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Input: * rs232_com_port, RS232 com port the device is connected to (1 to 32) * rs232_dev_addr, RS232 address of the device (00 to 99, 00 is reserved) * * Output: * dev_ID, identifier for future references to the device * * Zachary Wolf * 11/29/01 */ void ef11pr_init(int rs232_com_port, int rs232_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int err; int i; double pos; /* Message */ ef11pr_message(""); ef11pr_message("Initializing the Mitutoyo EF11PR counter..."); /* Check input parameters */ if (rs232_com_port < 1 || rs232_com_port > 32) { Fmt(msg, "%s 99) { Fmt(msg, "%s EF11PR_MAX_NUM_DEV) { Fmt(msg, "%s EF11PR_MAX_NUM_DEV) { Fmt(msg, "%sGN%c[d]%c[d],%f", &pos_mm); if (num_scan != 3) { printf("EF11PR problem getting counter reading: %s\n", msg); printf("Try again...\n"); Delay(1.); goto try_again; } /* Convert from mm to m */ *pos = pos_mm / 1000.; /* Check for errors */ err = ef11pr_get_errors(dev_ID); if (err != 0) { ef11pr_error("The counter has an error"); ef11pr_close_dev(dev_ID); *pos = 0.; return; } /* Done */ return; } /* ************************************************************** */ /* * ef11pr_zero * This function zeros the counter. * * Input: * dev_ID, device identifier * * Zachary Wolf * 11/29/01 */ void ef11pr_zero(int dev_ID) { /* Declare variables */ int dev_open; int err; /* Check input parameters */ if (dev_ID < 1 || dev_ID > EF11PR_MAX_NUM_DEV) { Fmt(msg, "%s EF11PR_MAX_NUM_DEV) { Fmt(msg, "%s 99) { Fmt(msg, "%s 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * ef11pr_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 * 11/29/01 */ int ef11pr_out(int dev_ID, char* buf) { /* Declare variables */ int nbytes; int err; char out_buf[EF11PR_MAX_CMD + 1]; /* Flush the input and output queues */ err = FlushInQ(com_port); if (err != 0) printf("FlushInQ err = %i\n", err); err = FlushOutQ(com_port); if (err != 0) printf("FlushOutQ err = %i\n", err); /* Add the address and CR LF to the message */ Fmt(out_buf, "%s<%s%i[w2p0]%c%c", buf, dev_addr[dev_ID], 13, 10); //printf("out_buf = %s\n", out_buf); /* Send the message */ nbytes = StringLength(out_buf); err = ComWrt(com_port, out_buf, nbytes); /* Check for errors */ if (err <= 0) { Fmt(msg, "%s%s[t-]", buf); /* Already done */ /* Return the string in buf */ strcpy(buf, in_buf); /* Done */ return 0; } /* ************************************************************** */ /* * ef11pr_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 * 11/29/01 */ int ef11pr_get_errors(int dev_ID) { /* Send a command to the counter */ ef11pr_out(dev_ID, "GA"); /* Read the response from the counter */ ef11pr_in(dev_ID, msg); /* Check the response for an error message */ if (strstr(msg, "Error") != NULL) { ef11pr_message("EF11PR: An error was detected."); ef11pr_error(msg); return 1; } /* Done */ return 0; } /* ************************************************************** */ /* * ef11pr_message * This function handles messages about the EF11PR. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 11/29/01 */ void ef11pr_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * ef11pr_error * This function handles error messages for the EF11PR. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 11/29/01 */ void ef11pr_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nEF11PR 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); }