Skip to content. | Skip to navigation

Personal tools

lcu/dgh.cpp

lcu/dgh.cpp

lcu/dgh.cpp

/////////////////////////////////////////////////////////////////////////////
// DGH.CPP
//
//  Last updated: 3/16/2005
//
//  Part of the source code for the Remote 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 = 1;
  Address = address;
  Step = 0;
  Reading = 0.0;
  CheckSetup = 1;
  // Establish DGH initialization string
  strcpy(InitString,DGHInitString);
  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;
  // Send the command
  SerialBufPutString(Guest,temp);
  //temp[strlen(temp)-1] = 0; // get rid of cr (13)
  //EDSLog.Add(810,"CDGH::Send(%d): Sending \"%s\" to DGH %c",__LINE__,temp,Address);
}

/////////////////////////////////////////////////////////////////////////////
// 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 = Timer;
  // Wait for response
  do
  {
    if (SerialBufCharReady(Guest) && (i < 79))
    {
      if (RecordResponse)
      {
        Response[i++] = SerialBufGetChar(Guest);
        if ((Response[i-1] == '\r') && (Response[0] == '*'))
          Done = 1;
      }
      else
      {
        c = SerialBufGetChar(Guest);
        if (c == '*')
        {
          Response[i++] = c;
          RecordResponse = 1;
        }
      }
    }
  // Stop getting data when a '\r' is received, or the timeout has been reached.
  // (Timer - savetime) gives the time in milliseconds since we started looking
  // for data.
  }
  while (((Timer - savetime) < (Timeout*CKF)) && !Done);

  // Null-terminate the string
  Response[i] = 0;
  if (Done)
  {
    //EDSLog.Add(811,"CDGH::Receive(%d): Received \"%s\" from DGH %c",__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++;
    //EDSLog.Add(812,"!CDGH::Receive(%d): Timeout!, Received \"%s\" from DGH %c",__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)

  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,DGHTimeout))
    {
      EDSLog.Add(10,"Error reading DGH %c setup",Address);
      return(1);
    }
    sprintf(s,"*%s\r",InitString);
    if (strcmp(s,temp))
      EDSLog.Add(11,"DGH %c setup wrong, expecting %s, got %s",Address,s,temp);
    else
      EDSLog.Add(910,"DGH %c setup OK",Address);
  }
  if (Step == 0)
  {
    ClearSerialBuffer(Guest);
    // Query the DGH for its reading
    Send("\r");
    Time = Timer;
    Step = 1;
    ResponseLength = 0;
    return(-1);
  }
  else if (Step == 1)
  {
    while (SerialBufCharReady(Guest) && (ResponseLength < 79) && !Done)
    {
      Response[ResponseLength++] = SerialBufGetChar(Guest);
      if (ResponseLength > 5)
        if ((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;
        //EDSLog.Add(813,"CDGH::Update(%d): Received \"%s\" from DGH %c",__LINE__,Response,Address);
        return(0);
      }
      else
      {
        ErrorCount++;
        Step = 0;
        //EDSLog.Add(814,"!CDGH::Update(%d): Error!, Received \"%s\" from DGH %c",__LINE__,Response,Address);
        return(1);
      }
    }
    else if ((Timer - Time) > (DGHTimeout*CKF))
    {
      // 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;
      //EDSLog.Add(815,"!CDGH::Update(%d): Timeout!, Received \"%s\" from DGH %c",__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