#include #include #include #include #include #include "definitions.h" #include "rs232.h" /* Compumotor_command function that passed the command to the compumotor for intrepretation. The argruements are file_descriptor = device opened by rs232_openport function that describes the serial device where the compumotor is found. command = pointer to the string containing the command that will be passed to the compumotor command. The command should not have the terminating ":". This will be appended to the string in the function automatically. responce = array of string pointers that will contain the reply from the compumotor. If response==NULL, response_buffer_length==0, or number_of_terminators==0 then no reply is expected. response_buffer_length = length of the reply buffer string. This is the maximum size of the reply buffer any string beyond this will not be read in. number_of_terminators = number of "\n" (carrage returns) expected from the reply of the compumotor. This is to allow for reading of multiple line replies from the compumotor. Initial version of this software was written by Robert C. Berrington. Please contact for questions regarding the code at rberring@uwyo.edu. */ int compumotor_command(int file_descriptor, char *command, char **response, int response_buffer_length, int number_of_terminators) { char c='\0'; int i; int j; int max_attempts=1; int status=FAILURE; size_t bytes_read; unsigned int sleep_s=1; /* seconds to sleep */ char *command_with_terminator=NULL; /* Test to make sure the arguements passed into the compumotor_command function are correct */ if (command == NULL) { fprintf(stderr, "'command' buffer pointer passed was NULL. No command sent.\n"); fflush(stderr); return NULL_ARGUEMENT; } if ((response_buffer_length>0) && (response==NULL)) { fprintf(stderr, "'response' buffer pointer passed was NULL with positive expected buffer length!\n"); fflush(stderr); return NULL_ARGUEMENT; } if (number_of_terminators==0) { fprintf(stderr, "*WARNING* expected number of terminators was %d! No reply expected\n", number_of_terminators); fflush(stderr); } /* Allocate the memory required for the string that will include the terminator sequence for the compumotor command. This is the will be the string that will actually be sent to the compumotor */ if ((command_with_terminator = calloc(strlen(command)+2, sizeof(char)))==NULL) { fprintf(stderr, "*ERROR* failed to allocate command buffer!"); fflush(stderr); return FAILURE; } strcpy(command_with_terminator, command); strcat(command_with_terminator, ":\0"); /* Append end of command sequence */ #ifdef DEBUG /* Just to show that we got this far. */ printf("The command '%s' was received.\n", command_with_terminator); #endif status = rs232_putline(file_descriptor,command_with_terminator); free(command_with_terminator); if (status != SUCCESS) { return status; } /* Get the response, if one is expected. */ if ( (response != NULL) || (response_buffer_length != 0) || (number_of_terminators != 0)) { for (j=0; j= max_attempts) { if (status != SUCCESS) { fprintf(stderr, "Failed at attempt to discard unread characters"); fflush(stderr); return FAILURE; } } } } return SUCCESS; } /* function compumotor_command */