#include /* Tattletale Model 8 Definitions */ #include /* definitions and prototypes for Model 8 library */ #include #include #include #include #include "lobo.h" /************************************************************************/ /* Function : SampleISUS */ /* Purpose : Function to sample ISUS and return the data (buf) */ /* Inputs : None */ /* Outputs : None */ /************************************************************************/ /* This routine is called on EVERY sample loop of the logger. It is up */ /* to the sample routine to determine whether it's time to actually run */ /* the device. This is normally done by simply counting calls, and */ /* running every Nth call. */ /* This routine should put the resulting data in *datap */ /************************************************************************/ short SampleISUS(FILE *fptr, ushort mode) { char buf[2048]; ushort relays; short tsret; int year; float tstamp; long isusret; if( (mode != SAMPLE) && (mode != CONNECT) ) return( -1 ); tstamp = GetDOYtime(&year); if( ((RdLatch1() >> 8) & KON(ISUS_CHAN) ) != FALSE) Set_K1_K8_State( KOFF(ISUS_CHAN) ); /* Turn comms relay for ISUS OFF */ if( tsret=tserOpen(ISUS_CHAN, (long)ISUS_BAUD, ISUS_PARITY, ISUS_DATABITS, ISUS_STOPBITS) == tsOK ){ SerActivate(); //powerup TT8 RS232 transmitter Sleep(0); Sleep(TICKS_PER_SECOND/2); relays = Set_K1_K8_State( KON(ISUS_CHAN) ); /* Turn comms relay for ISUS ON */ Sleep(0); Sleep(TICKS_PER_SECOND/2); tserInFlush(ISUS_CHAN); if(mode == SAMPLE){ if( (isusret=LogIsusData(fptr, ISUS_CHAN, buf, sizeof(buf), ISUS_TIMEOUT)) < 0 ) fprintf(fptr, ", %04d.%010f, ISUS LogIsusData() ret = %ld\n", year, tstamp, isusret); } else if(mode == CONNECT){ if( WakeUpIsus(ISUS_CHAN) == TRUE ){ printf("\nConnected... CTRL_D to Exit.\n"); InstrumentConnect(ISUS_CHAN); } else{ printf("\nInstrument Not Responding!\n"); if( QueryYesNo("\n Force Connect", FALSE) ){ printf("\nConnecting... CTRL_D to Exit.\n"); InstrumentConnect(ISUS_CHAN); } } } Set_K1_K8_State( KOFF(ISUS_CHAN) ); /* Turn comms relay for ISUS OFF */ tserClose(ISUS_CHAN); return(TRUE); } else{ fprintf(fptr,", %04d.%010f, ISUS tserOpen() ret = %d\n", year, tstamp, tsret); return(FALSE); } } /* SampleISUS() */ /************************************************************************/ /* Function : WakeUpIsus */ /* Purpose : Look for ACK response from wakeup char transmission */ /* Inputs : Timeout in seconds */ /* Outputs : TRUE or FALSE */ /************************************************************************/ short WakeUpIsus(ushort chan) { char rbuf[32]; short i; tserInFlush(chan); tserBreak(chan, TICKS_PER_SECOND/2); //send a 0.5 sec BREAK Sleep(0); Sleep(TICKS_PER_SECOND/2); for(i = 10; i; i--){ Sleep(0); tserPutc(chan, 'W'); Sleep(TICKS_PER_SECOND/4); if( tserGetsTmout(chan, rbuf, sizeof(rbuf)-1, 1) > 0 ){ if( strstr(rbuf, "ACK") != NULL ){ tserInFlush(chan); return( TRUE ); } } else tserInFlush(chan); } return(FALSE); /* If timeout or loop finished */ } /************************************************************************/ /* Function : LogIsusData */ /* Purpose : to get ISUS data */ /* Inputs : Buffer, size of Buffer */ /* Outputs : Number of bytes received from ISUS */ /************************************************************************/ long LogIsusData(FILE *fptr, ushort chan, char *buf, ushort buflen, ushort tmout) { register ushort nChars, i; register ushort nRecs, maxRecs; char *ptr; int year; float tstamp; if( WakeUpIsus(chan) == FALSE ) return( -1L ); tserPutc(chan, 'R'); if( tserGetsTmout(chan, buf, buflen, tmout) > 0 ){ nRecs = 0; maxRecs = MAX_ISUS_RECS_TO_LOG; nRecs = (ushort) atoi(buf); for( i = 0; i < nRecs; i++ ){ tserInFlush(chan); tstamp = GetDOYtime(&year); tserPutc(chan, 'D'); if( (nChars = tserGetsTmout(chan, buf, buflen, tmout)) > 0 ){ if( i < maxRecs ) fprintf(fptr,", %04d.%010f, %s\n", year, tstamp, buf); } else if( nChars == 0 ){ fprintf(fptr,", %04d.%010f, ISUS LogIsusData() Recieve Data Timeout\n", year, tstamp); } Sleep(0); Sleep(TICKS_PER_SECOND/2); } //end for loop } //end R cmd if else return( -2L ); return( nRecs ); }