Skip to content. | Skip to navigation

Personal tools

dgh.cpp

dgh.cpp

dgh.cpp

/////////////////////////////////////////////////////////////////////////////
// DGH.CPP
//
//  Last updated: 3/12/2002
//
//  Part of the source code for the M3 Turret
//
// This file contains all DGH communication constants/routines
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CDGH::Init - Initializes the DGH module
void CDGH::Init(char address)
{
// Pass the DGH address in address
//
// Returns a 0 on success, non-0 on failure
	char temp[80];

	ErrorCount = 0;
	Address = address;
	Step = 0;
	Reading = 0.0;
	CheckSetup = 1;
	// Establish DGH initialization string
	strcpy(InitString,"xx0104C2");
	sprintf(temp,"%2X",Address);
	InitString[0] = temp[0];
	InitString[1] = temp[1];
}

/////////////////////////////////////////////////////////////////////////////
// CDGH::Send - Sends a string to the DGH module
void CDGH::Send(char *Data)
//	Output a string (Data) to the DGH.
{
	char temp[80];

	strcpy(&temp[2],Data);
	temp[0] = '$';
	temp[1] = Address;
	Log->Add("CDGH::Send(%d): Sending \"%s\" to DGH %c",2,__LINE__,temp,Address);
#ifdef ENABLEIO
	// Send the command
	SerialBufPutString(Guest,temp);
#endif

}

/////////////////////////////////////////////////////////////////////////////
// CDGH::Receive - Waits for a properly terminated message from the module
int CDGH::Receive(char *Response,int Timeout)
//	Put the DGH's response in Response, and wait for Timeout
//	milliseconds before giving up.
//	Returns a 0 on success, 1 on a failure
//	See the DGH manual for more info on communication with the DGH cards.
{
	int i; 							// Index variable
	int Done; 					// Flag: 1 when routine is done, otherwise 0
	int RecordResponse; // Flag: 1 when routine starts recording response
	unsigned long savetime; // Stores initial time in milliseconds
	char c;

	i = 0;
	Done = 0;
	RecordResponse = 0;
	savetime = msTimer;
#ifdef ENABLEIO
	// Wait for response
	do
	{
		if (SerialCharReady(Guest) && (i < 79))
		{
			if (RecordResponse)
			{
				Response[i++] = SerialGetChar(Guest);
				if ((Response[i-1] == '\r') && (Response[0] == '*'))
					Done = 1;
			}
			else
			{
				c = SerialGetChar(Guest);
				if (c == '*')
				{
					Response[i++] = c;
					RecordResponse = 1;
				}
			}
		}
	// Stop getting data when a '\r' is received, or the timeout has been reached.
	// (msTimer - savetime) gives the time in milliseconds since we started looking
	// for data.
	}
	while (((msTimer - savetime) < Timeout) && !Done);
#endif

	// Null-terminate the string
	Response[i] = 0;
	if (Done)
	{
		Log->Add("CDGH::Receive(%d): Received \"%s\" from DGH %c",2,__LINE__,Response,Address);
		ErrorCount = 0;
		return(0);
	}
	else
	{
		// Count the number of communication failures.
		// A communication failure means that a 'cr' was not received
		// before a Timeout occurred
		ErrorCount++;
		Log->Add("!CDGH::Receive(%d): Timeout!, Received \"%s\" from DGH %c",2,__LINE__,Response,Address);
		return(1);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CDGH::Command - Sends a command and then waits for a response
int CDGH::Command(char *Data,char *Response,int Timeout)
{
	// This routine sends the command in string Data to the DGH on port Port,
	// and puts the response from the DGH in string Response.  The routine will
	// timeout after Timeout milliseconds.
	// Returns a 0 on success, non-0 on failure.
	// A 1 means that the DGH timed-out (didn't give a '*' prompt back
	// before Timeout milliseconds)

	Delay(10);
	ClearSerialBuffer(Guest);
	Send(Data);
	return(Receive(Response,Timeout));
}

/////////////////////////////////////////////////////////////////////////////
// CDGH::Update - Reads the current reading from the DGH module
int CDGH::Update()
// This routine reads the DGH
// -1 = not done, 0 = done, 1 = error
{
	char temp[80],s[255];
	int Done = 0;

	// Check DGH setup on first pass
	if (CheckSetup)
	{
		CheckSetup = 0;
		// Check DGH's Init String
		if (Command("RS\r",temp,1000))
		{
			EDSLog.Add(53,"Error reading DGH %c setup",Address);
			return(1);
		}
		sprintf(s,"*%s\r",InitString);
		if (strcmp(s,temp))
			EDSLog.Add(54,"DGH %c setup wrong, expecting %s",Address,InitString);
	}
	if (Step == 0)
	{
		ClearSerialBuffer(Guest);
		// Query the DGH for its reading
		Send("\r");
		Time = msTimer;
		Step = 1;
		ResponseLength = 0;
		return(-1);
	}
	else if (Step == 1)
	{
		while (SerialCharReady(Guest) && (ResponseLength < 79) && !Done)
		{
			Response[ResponseLength++] = SerialGetChar(Guest);
			if (ResponseLength > 5)
				if ((Response[ResponseLength-1] == '\r') &&
						(Response[0] == '$') &&
						(Response[1] == Address) &&
						(Response[2] == '\r') &&
						(Response[3] == '*') &&
						(Response[ResponseLength-1] == '\r'))
					Done = 1;
		}
		if (Done)
		{
			// Null-terminate the string
			Response[ResponseLength] = 0;
			if (sscanf(&Response[4],"%f",&Reading) == 1)
			{
				Step = 0;
				ErrorCount = 0;
				Log->Add("CDGH::Update(%d): Received \"%s\" from DGH %c",2,__LINE__,Response,Address);
				return(0);
			}
			else
			{
				ErrorCount++;
				Step = 0;
				Log->Add("!CDGH::Update(%d): Error!, Received \"%s\" from DGH %c",2,__LINE__,Response,Address);
				return(1);
			}
		}
		else if (msTimer - Time > 1000)
		{
			// Count the number of communication failures.
			// A communication failure means that a '\r' was not received
			// before a Timeout occurred
			// Null-terminate the string
			Response[ResponseLength] = 0;
			ErrorCount++;
			Step = 0;
			Log->Add("!CDGH::Update(%d): Timeout!, Received \"%s\" from DGH %c",2,__LINE__,Response,Address);
			return(1);
		}
		return(-1);
	}
	return(-1);
}

/////////////////////////////////////////////////////////////////////////////
// DGHFormat - returns a float in a string properly formatted for the DGHs
char * DGHFormat(float f,char *s)
// Pass the float to be formatted in f, and the string will be returned in s
// Returns f in the format "+00000.00"
{
	int i,j;

	i = f;
	j = (f - i) * 100;
	if (i >= 0)
		sprintf(s,"+%05d.%02d",abs(i),abs(j));
	else
		sprintf(s,"-%05d.%02d",abs(i),abs(j));
	return(s);
}

Generated by GNU Enscript 1.6.5.2.
Document Actions