/******************************************************** 2000RS.EXE Example of 2000 RS232 code with error checking. This was written to give to a customer who wants to read the display and write to a file. If you include a value after the 2000RS, this is the delay between runs. There are 18.2 ticks per second, so I multiply your delay by 18.2 to get a delay in seconds. 2000rs .25 will give ~four readings per second. 09/28/95 Steve St. Amand *********************************************************/ #include #include #include #include #include "C:\CPORT2\INCLUDE\CPORT.H" #define COMPORT_TIMEOUT 5 void Flushthekeyboard(); void Errorhandler(char messagestring[80]); void Mainmenu(); char DISPLAY_DATA[50]; char DISPLAY_DATA2[50]; char in_key ; char message[80]; char *fptr1, *fptr2 ; unsigned long starttime, lasttime, startoftest, testduration ; unsigned long runnumber = 0 ; unsigned int x ; unsigned int baudrate = 19200 ; int comnumber = 1; struct tm *time_now; time_t secs_now; clock_t clock(void); long rundelay = 0; long delaytill ; COM com ; FILE *fileout ; int main(int argc, char *argv[]) { clrscr(); Mainmenu(); for(;;) { if(kbhit()){ in_key = getch(); if(toupper(in_key) == 'B' ){ if(baudrate == 19200) baudrate = 1200 ; else baudrate = baudrate*2 ; Flushthekeyboard(); Mainmenu(); } if(toupper(in_key) == 'C' ){ if(comnumber == 4) comnumber = 1 ; else comnumber++ ; Flushthekeyboard(); Mainmenu(); } if(in_key == 0x1b) { ComCloseAll(); clrscr(); exit(0) ; } if(in_key == 0x0d){ clrscr(); break; } } } /* this is added for my own use. If you include a number with the file name as an input parameter, the readings will be seperated by this amount of time. This is a "reading delay" and is in decimal seconds. "Clock" gives processor ticks, 18.2 per second */ if(argc > 0) rundelay = (atof(argv[1]) * 18.2 ) ; /*** Open the com port, 3000 rx buffer, 512 tx buffer ***/ if(comnumber == 1) com = ComOpen(COM1, 115200/baudrate, W8|S1|NONE, 3000, 512); if(comnumber == 2) com = ComOpen(COM2, 115200/baudrate, W8|S1|NONE, 3000, 512); if(comnumber == 3) com = ComOpen(COM3, 115200/baudrate, W8|S1|NONE, 3000, 512); if(comnumber == 4) com = ComOpen(COM4, 115200/baudrate, W8|S1|NONE, 3000, 512); if(com==NULL) { clrscr(); strcpy(message , "ERROR: unable to open serial port. Please check your cable connections"); gotoxy(40-(strlen(message)/2),15); cprintf("%s" , message); strcpy(message , "and confirm the instrument's Interface Baud rate matches the number at the"); gotoxy(40-(strlen(message)/2),16); cprintf("%s" , message); strcpy(message , "top of the screen. Press any key when ready."); gotoxy(40-(strlen(message)/2),17); cprintf("%s" , message); delay(2000); while(!kbhit()); Flushthekeyboard(); ComClose(com); return(comopen_errno); } /*** set up handshakeing ***/ ComHandshake(com, HARD2, 3000); startoftest = time(&secs_now); /********************* OPEN THE OUTPUT FILE*******************/ fileout = fopen("XXXITR.TXT", "a"); /************** Main loop, press esc to exit ******************/ while(in_key != 0x1b) { if(kbhit()) in_key = getch(); ComFlushTx(com); ComFlushRx(com); /*** send the command to the xitron ***/ x = ComPuts(com, "@\n") ; if(x < 1) Errorhandler( "ERROR: in placing characters in the Tx buffer."); /*** wait for the Tx buffer to send the command ***/ starttime = time(&secs_now); while(ComLenTx(com) != 0){ lasttime = time(&secs_now); if(lasttime - starttime > COMPORT_TIMEOUT) Errorhandler( "ERROR: Transimtting from buffer to unit. "); } /*** check the recieve buffer for the \n, when it is present, get the data ***/ starttime = lasttime = time(&secs_now); while(ComRxScan(com, '\n') == 0 ){ lasttime = time(&secs_now); if(lasttime - starttime > COMPORT_TIMEOUT) Errorhandler( "ERROR: waiting for unit to reply. "); } ComGets(com, DISPLAY_DATA, 50, '\n'); testduration = (time(&secs_now) - startoftest); /***************************************PRINT TO THE SCREEN ****************/ strcpy(message , "XITRON TECHNOLOGIES 2000 RS232 DEMO PROGRAM"); gotoxy(40-(strlen(message)/2),2); cprintf("%s", message); gotoxy(5,5); clreol(); cprintf("The DISPLAY's DATA is %s", DISPLAY_DATA ); gotoxy(5,17); clreol(); cprintf("The test has been running %u seconds" , testduration ); cprintf(" and logged %u entrys in the file.", runnumber); gotoxy(20,24); cprintf("PRESS THE ESCAPE KEY TO EXIT"); /*********** strip out the CR/LF, I'll add them later if I want them ****/ fptr1 = DISPLAY_DATA ; fptr2 = DISPLAY_DATA2 ; while (*fptr1 != '\r') { *fptr2 = *fptr1 ; fptr1++ ; fptr2++ ; } fprintf(fileout, "%s\n", DISPLAY_DATA2); runnumber++ ; delaytill = ( clock() + rundelay ); while(delaytill > clock() ){ if(kbhit()){ ComCloseAll(); fclose(fileout); /*** close the file ***/ ComFlushTx(com); ComFlushRx(com); ComClose(com) ; clrscr(); exit(0) ; } } } /******************** End of main loop **************************/ fclose(fileout); /*** close the file ***/ ComFlushTx(com); ComFlushRx(com); ComClose(com) ; } /**** End of program ***/ void Flushthekeyboard() { while(kbhit()) in_key = getch(); in_key = NULL; return; } void Errorhandler(char messagestring[80]) { clrscr(); strcpy(message , messagestring); gotoxy(40-(strlen(message)/2),10); cprintf("%s\n\n\rSeconds of Operation %u" , message , testduration ); strcpy(message , "PRESS ANY KEY TO EXIT") ; gotoxy(40-(strlen(message)/2),24); cprintf("%s" , message ); delay(2000); while(!kbhit()); Flushthekeyboard(); ComClose(com); fclose(fileout); exit(0); } void Mainmenu() { clrscr(); strcpy(message , "XITRON TECHNOLOGIES 2000 RS232 DEMO PROGRAM"); gotoxy(40-(strlen(message)/2),3); cprintf("%s", message); strcpy(message , "Press B to change the Baud rate. Baudrate is "); gotoxy(40-(strlen(message)/2),10); cprintf("%s %d", message, baudrate); strcpy(message , "Press C to change the Com port number. Com port is "); gotoxy(40-(strlen(message)/2),13); cprintf("%s %d", message, comnumber); strcpy(message , "Press ENTER to begin testing, Press ESC to Quit"); gotoxy(40-(strlen(message)/2),21); cprintf("%s", message); return ; }