/*** scan7.c scan7_KT9.C - 22-aug-96 - Borland C++ V4.0 for V1.6 & V1.7 of KT-9 & PC 486 Copyright (C) 1994, 1995, 1996, Envi-2000 Modifications July 22, 1998 with Borland Turbo C/C++ Daniel B. LeBlanc Les Mines Selbaie Billiton Metals Canada ************************************** * * * REMOTE CONTROL OF KAPPAMETER KT-9 * * FROM KEYBOARD OF IBM-PC COMPUTER * * SCAN MODE * * * ************************************** Commands for KT-9: 'U' ... TEST KEY AFTER POW.DOWN 'T' ... Test of communication only 'D' ... DELETE ALL COM1 flags 'K' ... ENABLE OUTPUT kappa TO COM1 'C' ... key CLEAR/MEASURE 'R' ... key RECALL 29-jun-95 ***/ #include #include #include #include #include #include #include FILE *stream; /* COM1200.ASM */ extern void m_init( void ); extern void m_dis( void ); extern void m_readb( unsigned char *buf, int *n ); extern int m_wait( void ); extern void m_writ( char *buf, int *n ); extern int m_loc; char rbuf[80], wbuf[80], key; int err1, i, j, k, n, cntr, del; int Debug_Mode = 0; void StopDown( void ); int GetHeader(void); int kt9cmd(char key); int TimeOut(time_t TimeAtStart, double MaxWaitTime); void main( void ) { char sec, filename[20], *cptr; long int *lptr, ilong, jlong; float kappa; int R_key_pressed = 0; int status=0, output_file_opened=0, ScanModeOffManually=0; int port_initialized, CommunicationStatus; time_t TimeAtStart; time_t ScanModeOffAtTime; double TimeLeftToResumeScan, ResumeScanTimeMax; double MaxWaitTime; /* REENTRY */ /* ------- */ do { port_initialized = 0; printf( "\n scan7 \n" ); printf( " Revision 1.0, July 22, 1998 \n" ); printf( " Scan Mode of kappameter KT-9 \n" ); printf( "\n Connect KT-9 to COM1 of PC-computer \n" ); printf( " Switch KT-9 on \n" ); printf( " Select measuring mode 'nP' or 'Cor' \n" ); printf( " Erase all data from memory of KT-9 \n" ); printf( " To proceed, press Enter on PC or Q to exit "); key=toupper(getch()); if( key=='Q' ) break; printf( "\n\n Initialising the PC and the KT-9, please wait.."); /* OPEN RS-232 PORT & CLEAR */ /* ------------------------ */ m_init(); if(Debug_Mode) printf( "\n RS-232 initialized.\n" ); /* Test PC <-> KT-9 communication */ status = kt9cmd('T'); if (status) { printf(" \a\a\aCommunications with KT-9 test failed.\a\a\a\n" ); continue; } else if(Debug_Mode) printf(" Communication with KT-9 OK.\n"); CommunicationStatus = 0; port_initialized = 1; /* Clear COM1 flags */ /* ---------------- */ status = kt9cmd('D'); if (status) { printf(" COM1 flags failed to be cleared.\n"); continue; } else if(Debug_Mode) printf(" COM1 flags cleared OK.\n"); /* Enable KT-9 kappa output to COM1 */ /* -------------------------------- */ status = kt9cmd('K'); if (status) { printf(" Failed to enable KT-9 kappa output to COM1.\n"); continue; } else if(Debug_Mode) printf(" KT-9 kappa output to COM1 enabled.\n"); printf( "\n\n *** KT-9 ready *** " ); /* Get filename for kappa data */ /* --------------------------- */ printf( "\n Enter the filename or to Quit: "); gets(filename); if (strcmp(filename,"")) { stream = fopen( filename, "w" ); if( stream == NULL ) printf( "\n File %s not opened", filename ); else { printf( " File %s opened for input \n", filename ); j = fseek( stream, 0L, SEEK_SET ); output_file_opened = 1; } } else break; status = GetHeader(); if (status) continue; /* ======================= MAIN LOOP ========================== */ do { /* Press C */ printf( "\n Press 'C' to Clear >> " ); key = toupper(getch()); printf( "%c", key ); if( key != 'C' ) { printf( " is the wrong key - Try Again " ); continue; /* PressC */ } /* CLEAR/MEASURE */ /* ------------- */ printf ("\n"); status = kt9cmd('C'); if (status) { printf("\n Failed to 'CLEAR' KT-9.\n"); break; /* Re-Enter */ } else printf("\n KT-9 Display must be 'C: 0'"); /* Timed scan mode prompt */ /* ---------------------- */ printf( "\n Press 'R' to start scan mode >> " ); TimeAtStart = time(NULL); R_key_pressed = 0; MaxWaitTime = 10.0; while (!TimeOut(TimeAtStart,MaxWaitTime) && !R_key_pressed) { if (kbhit()) { key=toupper(getch()); if (key != 'R') { printf(" %c = Wrong key - try again",key); printf("\n Press the 'R' key to start scan mode >> "); } else R_key_pressed = 1; } } if( !R_key_pressed ) { printf( "End of %f seconds TIMEOUT ",MaxWaitTime ); continue; /* PressC */ } /* Recall - Get Into Scan Mode. */ /* ---------------------------- */ status = kt9cmd('R'); if (status) { printf(" \n Failed to 'Recall' KT-9 in Scan Mode."); continue; /* Re-Enter */ } else if(Debug_Mode) printf(" \n KT-9 in Scan Mode."); /* Scan Mode measurements */ /* ---------------------- */ printf( "\n Press 'C' to Stop\n" ); MaxWaitTime = 1.0; ScanModeOffManually = 0; ResumeScanTimeMax = 10.0; do { /* scan */ TimeAtStart = time(NULL); do {} while(!TimeOut(TimeAtStart,MaxWaitTime) && !kbhit() && m_loc<4 ); if (ScanModeOffManually && m_loc>=1 ) { /* Flush the first burst of data after Scan Mode */ /* has been switched Off Manually. */ /* It is probably data coming from a 0 reading. */ if (m_loc) { while (m_loc) { n = 1; m_readb(rbuf,&n); if (Debug_Mode) printf("\n Flushing rbuf: %4X",rbuf[0]); } ScanModeOffManually = 0; } } else if(m_loc >= 4) /* note, cntr is now initialized in GetHeader */ { lptr=&jlong; cptr=(char*)lptr; n = 4; m_readb(cptr,&n); kappa = jlong/100.0; printf("\r kappa = %6.2f [E-3 SI units] ",kappa); cntr--; if( !cntr ) cntr = 10, fprintf( stream, "\n" ); fprintf( stream, "%7.2f", kappa ); } else if(kbhit()) { key = toupper(getch()); printf( "%c", key ); ScanModeOffManually = 0; } else { /* TimeOut, assume the user has switched Scan Mode off. */ if (!ScanModeOffManually) { ScanModeOffAtTime = time(NULL); printf ("\n KT-9 Scan Mode Off, waiting...\n"); ScanModeOffManually = 1; } TimeLeftToResumeScan = ResumeScanTimeMax - difftime(time(NULL),ScanModeOffAtTime); if (TimeLeftToResumeScan > 0.0) printf ("\r Countdown: %3.1f",TimeLeftToResumeScan); else { /* TimeOut exceed delay to resume scan mode. */ /* Check communications to KT-9 */ printf("\n Testing communications with KT-9.\n" ); CommunicationStatus = kt9cmd('T'); if (CommunicationStatus) printf ("\a\a\a Communications problem with KT-9.\a\a\a\n" ); else printf(" Communications with KT-9 appear O.K.\n" ); break; } } } while(key != 'C'); /* scan */ /* Received a 'C', should stop scan mode */ /* ------------------------------------- */ if (!ScanModeOffManually) { status = kt9cmd('C'); if (status) { printf(" \n Failed to stop Scan Mode."); continue; /* Re-Enter */ } } do { printf( "\n Press 'C' to continue " ); printf( "\n Press 'E' to exit >> " ); key = toupper(getch()); printf( "%c", key ); } while( key != 'C' && key != 'E' ); if( key == 'C' ) { status = GetHeader(); if (status) break; } else break; /* break PressC to REENTRY */ } while( 1 ); /* PressC */ } while( key != 'E' ); /* REENTRY */ /* Exit keyboard monitor, clear COM1 flags, disable COM1 */ /* ----------------------------------------------------- */ if(port_initialized && !CommunicationStatus) { status = kt9cmd('D'); if (status) printf(" \n Failed to clear COM1 flags.\n"); else if(Debug_Mode) printf(" \n COM1 flags cleared OK.\n"); m_dis(); } if (output_file_opened) { fprintf( stream, "\n" ); j = fclose( stream ); printf( "\n File %s closed.\n",filename); } printf( "\n End of job.\n" ); } /* =============================================================== function: TimeOut(time_t TimeAtStart, double MaxWaitTime) returns 0 if time elapsed less than MaxWaitTime returns 1 if time elapsed exceeds MaxWaitTime =============================================================== */ int TimeOut(TimeAtStart, MaxWaitTime) time_t TimeAtStart; double MaxWaitTime; { if (difftime(time(NULL),TimeAtStart) <= MaxWaitTime) return(0); else return(1); } /* =============================================================== function: kt9cmd(char key) sends a one character command to the KT-9 returns an integer status code. 0 if = O.K. !0 = !O.K. =============================================================== */ int kt9cmd(cmd) char cmd; { int cmd_status = 0; StopDown(); if(err1) cmd_status = 1; else { wbuf[0] = cmd; n = 1; m_writ( wbuf, &n ); j = m_wait(); if( j ) { printf( " %c output to KT-9 ERROR \n", cmd ); cmd_status = 1; } if(Debug_Mode) printf( " %c output to KT-9 OK \n", cmd ); } return(cmd_status); } /* ====================================================== function: GetHeader() prompts the user for: a header, no header or quit. if the user specifies a new header - writes the header in the file, resets the column no. counter "cntr": for a new line, returns. if the user specifies no header - returns, with the column no. counter unchanged. if the user opts to quits - returns with a status value indicating to quit. returns 0 if a header supplied or none are desired. 1 if the user opts to quit. ====================================================== */ int GetHeader( void ) { char header[80]=""; printf( "\n Enter HEADER, or 'Q' to quit, or for no HEADER: " ); gets (header); if (strcmp(header,"Q")) if (strcmp(header,"q")) { if (strcmp(header,"")) { fprintf( stream, "\n%s\n", header ); cntr = 11; } return(0); } else return(1); else return(1); } /* =============================== function StopDown() =============================== */ void StopDown( void ) { time_t TimeAtStart; double MaxWaitTime; int GotEchoes; char StrangeButFrequentChar; int GotStrangeButFrequentChar; StrangeButFrequentChar = '\xF5'; GotStrangeButFrequentChar = 0; err1 = 0; MaxWaitTime = 0.01; /* Flush the buffer content */ TimeAtStart = time(NULL); while (!TimeOut(TimeAtStart,MaxWaitTime)) { while (m_loc) { n = 1; m_readb(rbuf,&n); if (Debug_Mode) printf ("\n StopDown flushing rbuf: %4X %c",rbuf[0],rbuf[0]); } } for( i = 0; i < 5; i++ ) { wbuf[0] = 'U'; n = 1; m_writ(wbuf,&n); j = m_wait(); if (j) { if (Debug_Mode) printf ("\n Timeout error, output to KT-9, loop %d\n", i); continue; } TimeAtStart = time(NULL); GotEchoes = 0; GotStrangeButFrequentChar = 0; while (!TimeOut(TimeAtStart,MaxWaitTime) ) { while (m_loc) { n = 1; m_readb(rbuf,&n); GotEchoes = 1; if( rbuf[0] == 'U' ) return; else if (rbuf[0] == StrangeButFrequentChar) { GotStrangeButFrequentChar = 1; break; } } } if (!GotStrangeButFrequentChar && Debug_Mode) { if ( !GotEchoes) printf ("\n No echo from KT-9, in %f seconds.",MaxWaitTime); else printf ("\n Bad echo from KT-9, rbuf:%4X %c %d", rbuf[0],rbuf[0],rbuf[0]); } } err1 = 1; if (i == 5) printf( "\n Too many retries in StopDown." ); printf( "\n Error on COM1, in StopDown\n" ); }