/****************************************************************************                                                                        ***  FILE        :  instrument.c                                           ***                                                                        ***  DESCRIPTION :  Code to setup and control instruments				  ***					on MBARI Vertical Profiler through a serial interface ***                                                                        ***  COPYRIGHT   :  2000 Monterey Bay Aquarium Research Institute          ***                                                                        ***	REVISION HISTORY:													  ***		1.0.0	Initial release (prm)									  ***		1.1.0	Added commands for SBE-19 (prm)							  ***		1.1.1	Fixed bug in StopMBBP routine, which was flushing the	  ***				SBE39 port instead of the MBBP port. (PRM - 9Jun06)		  ***		1.2.0	Added wakeup char (space), followed by 500 ms delay to	  ***				MPPB wake routine to support DH-4 plugged into MPPB port. ***************************************************************************/#include	<tt8lib.h>#include	<tat332.h>		/* 68332 Tattletale (7,8) Hardware Definitions */#include	<tpu332.h>		/* 68332 Time Processing Unit Definitions */#include	<dio332.h>		/* TPU Digital Input Output */#include	<stdio.h>#include	<float.h>#include	"instrument.h"#include	"mvp.h"static char buf[10][TSBUFSIZ+TSER_MIN_MEM];		/* 5 input buffers, 5 output buffers *//* Open the TPU serial input and output ports for the instruments */void InitSerPorts(void){	int result;		if(result = TSerOpen(SBE39_IN, HighPrior, 0, buf[SBE39_IN], TSBUFSIZ, 9600, 'N', 8, 1) != tsOK) {		printf("Couldn't open serial input for SBE39, error code = %d\n", result);	} /* end if */	if(result = TSerOpen(SBE39_OUT, HighPrior, 1, buf[SBE39_OUT], TSBUFSIZ, 9600, 'N', 8, 1) != tsOK) {		printf("Couldn't open serial output for SBE39, error code = %d\n", result);	} /* end if */	if(result = TSerOpen(SBE19_IN, HighPrior, 0, buf[SBE19_IN], TSBUFSIZ, 9600, 'E', 7, 1) != tsOK) {		printf("Couldn't open serial input for SBE19, error code = %d\n", result);	} /* end if */	if(result = TSerOpen(SBE19_OUT, HighPrior, 1, buf[SBE19_OUT], TSBUFSIZ, 9600, 'E', 7, 1) != tsOK) {		printf("Couldn't open serial output for SBE19, error code = %d\n", result);	} /* end if */	if(result = TSerOpen(MBBP_IN, HighPrior, 0, buf[MBBP_IN], TSBUFSIZ, 9600, 'N', 8, 1) != tsOK) {		printf("Couldn't open serial input for MBBP, error code = %d\n", result);	} /* end if */	if(result = TSerOpen(MBBP_OUT, HighPrior, 1, buf[MBBP_OUT], TSBUFSIZ, 9600, 'N', 8, 1) != tsOK) {		printf("Couldn't open serial output for MBBP, error code = %d\n", result);	} /* end if */	} /* end function InitSerPorts() *//* Close the TPU serial input and output ports for the instruments */void CloseSerPorts(void){	int result;		if(result = TSerClose(SBE39_IN) != tsOK) {		printf("Couldn't close serial input for SBE39, error code = %d\n", result);	} /* end if */	if(result = TSerClose(SBE39_OUT) != tsOK) {		printf("Couldn't close serial output for SBE39, error code = %d\n", result);	} /* end if */	if(result = TSerClose(SBE19_IN) != tsOK) {		printf("Couldn't close serial input for SBE19, error code = %d\n", result);	} /* end if */	if(result = TSerClose(SBE19_OUT) != tsOK) {		printf("Couldn't close serial output for SBE19, error code = %d\n", result);	} /* end if */		if(result = TSerClose(MBBP_IN) != tsOK) {		printf("Couldn't close serial input for MBBP, error code = %d\n", result);	} /* end if */	if(result = TSerClose(MBBP_OUT) != tsOK) {		printf("Couldn't close serial output for MBBP, error code = %d\n", result);	} /* end if */	} /* end function CloseSerPorts() */void SetupSBE19(void) {} /* end function SetupSBE19() *//* Tell the SBE19 to start sampling data */void StartSBE19(void) {//	printf("Starting SBE19\n");				// DEBUG	PConfOutp(E,3);	PClear(E,3);			/* power up instrument RS232 drivers */	DelayMilliSecs(50L);					/* wait for power supplies to come up */		SndInstr(SBE19_OUT, SBE19START);		// start the SBE19	DelayMilliSecs(250L);	SndInstr(SBE19_OUT, SBE19START);		// send again to be sure it was received//	RcvInstr(SBE19_IN, inbuf, 1);			// receive reply//	printf("inbuf: %s\n", inbuf);			//  and echo to screen	TSerInFlush(SBE19_IN);					// discard rest of SBE19 output		DelayMilliSecs(50L);					/* wait for output to finish */	PSet(E,3);								/* shut down instrument RS232 drivers */} /* end function StartSBE19() *//* Tell the SBE19 to stop sampling data */void StopSBE19(void) {//	char inbuf[TSBUFSIZ];					// serial receive buffer//	printf("Stopping SBE19\n");				// DEBUG	PConfOutp(E,3);	PClear(E,3);			/* power up instrument RS232 drivers */	DelayMilliSecs(50L);					/* wait for power supplies to come up */		SndInstr(SBE19_OUT, SBE19STOP);			// stop the SBE19	DelayMilliSecs(250L);	SndInstr(SBE19_OUT, SBE19STOP);			// send again to be sure it was received//	RcvInstr(SBE19_IN, inbuf, 1);			// receive reply//	printf("inbuf: %s\n", inbuf);			//  and echo to screen	TSerInFlush(SBE19_IN);					// discard rest of SBE19 output		DelayMilliSecs(500L);					/* wait for command to be processed */		SndInstr(SBE19_OUT, SBE19SLEEP);		// put the SBE19 back to sleep	DelayMilliSecs(50L);					/* wait for output to finish */		PSet(E,3);								/* shut down instrument RS232 drivers */} /* end function StopSBE19() */void SetupMBBP(void) {} /* end function SetupMBBP() *//* Tell the MBBP to start sampling data */void StartMBBP(void) {//	char inbuf[TSBUFSIZ];					// serial receive buffer	PConfOutp(E,3);	PClear(E,3);			/* power up instrument RS232 drivers */	DelayMilliSecs(50L);					/* wait for power supplies to come up */		TSerPutByte(MBBP_OUT, ' ');				// send a space char to wake up the DH-4	DelayMilliSecs(1000L);					// wait for DH-4 to wake up	SndInstr(MBBP_OUT, MBBPSTART);			// start the MBBP//	RcvInstr(MBBP_IN, inbuf, 1);			// receive reply//	printf("inbuf: %s\n", inbuf);			//  and echo to screen	TSerInFlush(MBBP_IN);					// discard rest of MBBP output			DelayMilliSecs(50L);					/* wait for output to finish */	PSet(E,3);								/* shut down instrument RS232 drivers */} /* end function StartMBBP() *//* Tell the MBBP to stop sampling data */void StopMBBP(void) {//	char inbuf[TSBUFSIZ];					// serial receive buffer	PConfOutp(E,3);	PClear(E,3);			/* power up instrument RS232 drivers */	DelayMilliSecs(50L);					/* wait for power supplies to come up */		SndInstr(MBBP_OUT, MBBPSTOP);			// stop the MBBP//	RcvInstr(MBBP_IN, inbuf, 1);			// receive reply//	printf("inbuf: %s\n", inbuf);			//  and echo to screen	TSerInFlush(MBBP_IN);					// discard rest of MBBP output		DelayMilliSecs(50L);					/* wait for output to finish */		PSet(E,3);								/* shut down instrument RS232 drivers */} /* end function StopMBBP() *//* Write a string through the TPU serial output to an instrument */void SndInstr(int port, char *str){	int n = 0;		while(str[n]) {		TSerPutByte(port, str[n++]);	} /* end while *///	TSerPutByte(port, '\r');	} /* end function SndInstr() *//* Read a string through the TPU serial input from an instrument until a CR is seen.	Abort after a given timeout in seconds */int RcvInstr(int port, char *buffer, int timeout){	int n = 0;		gTimeDly = timeout;	while(gTimeDly) {		if(TSerByteAvail(port)) {			// if char available			gTimeDly = timeout;				// reset timeout			buffer[n] = TSerGetByte(port);	// put it in buffer			SerPutByte(buffer[n]);			//	put echo char in screen buffer			if(buffer[n] != CR && n < TSBUFSIZ)	// if not carriage rtn and buffer isn't full				n++;						//	point to next buffer position			else				break;  			fflush (stdout);				// print echo char to screen 		} /* end if */	} /* end while */	if(n == 0) {		printf("No reply from port %d after %d seconds\n", port, timeout/10);		return FALSE;	} /* end if */      	buffer[n] = 0;							// terminate string	putchar('\n');							// put carriage return in screen buffer	fflush(stdout);							//	and print it	return TRUE;} /* end function RcvInstr() *//* Get a depth reading from the SBE39 */float ReadSBE39Depth(void){	char inbuf[TSBUFSIZ];	// serial receive buffer	float depth = 10000;	// depth reading	static float lastDepth = 10000; // last good depth reading#if 0	/* DEBUG - prompt user for depth instead of reading SBE 39 */	printf("Enter DEBUG depth: ");	fflush (stdout);	GetLine(inbuf, (int) (sizeof(inbuf)));	sscanf(inbuf, "%f", &depth);#endif#if 0	/* DEBUG - simulate depth readings for bench testing */	static int debugdepth = 11;	// start at 10 meters depth		if(debugdepth > 2) {		debugdepth--;	} else {		debugdepth = 10;	} // end if		printf("debugdepth is %d meters\n", debugdepth);			return (float)debugdepth;		#endif#if 1	PConfOutp(E,3);	PClear(E,3);			// power up instrument RS232 drivers	DelayMilliSecs(50L);					// wait for power supplies to come up		SndInstr(SBE39_OUT, SBE39WAKE);			// wake up SBE39	RcvInstr(SBE39_IN, inbuf, 10);			// receive prompt	TSerInFlush(SBE39_IN);					// discard rest of SBE39 output		SndInstr(SBE39_OUT, SBE39SAMP);			// request a sample	RcvInstr(SBE39_IN, inbuf, 10);			// receive echo of wakeup cmd and discard//	printf("inbuf1: %s\n", inbuf);	RcvInstr(SBE39_IN, inbuf, 20);			// receive output data//	printf("inbuf2: %s\n", inbuf);	sscanf(inbuf, "%*f,%f", &depth);		// read temperature and depth	TSerInFlush(SBE39_IN);					// discard rest of SBE39 output		PSet(E,3);								// shut down instrument RS232 drivers	if(depth == 10000) {					// if no good depth reading		depth = lastDepth;					//  then repeat last good reading	} else {		lastDepth = depth;					//  otherwise, save current reading	} /* end if */		return depth;#endif} /* end function ReadSBE39Depth */