Skip to content. | Skip to navigation

Personal tools

remote/mstreds.cpp

remote/mstreds.cpp

remote/mstreds.cpp

/////////////////////////////////////////////////////////////////////////////
//  mastereds.cpp
//
//  Last updated: 1/23/2006
//
//  This file contains code to implement the Engineering Data Stream (EDS)
//  log which captures the responses from downstream and forwards them to
//  upstream when asked to.
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CEDSLog::Init: Initialize the log
void CEDSLog::Init(int logSize)
{
// Pass the maximum number of log entries in logSize (usually 100)

  int i;

  LogSize = logSize;
  // Dynamically allocate log arrays
  Data = new edsstr[logSize];
  // Initialize log to empty strings
  for (i=0;i<LogSize;i++)
    Data[i][0] = 0;
  Index = 0;
  LastIndex = -1;
  Last = 0;
//  EDSTime = 0;
}

/////////////////////////////////////////////////////////////////////////////
// CEDSLog::Add - Add a log entry
void CEDSLog::Add(int messagenum,char* data,...)
{
// This routine adds the string in "data" to the running log.
  char s[1000],s2[1000];
  va_list va;
  struct dostime_t t;

  // Store the message
  va_start(va,data);
  vsprintf(s2,data,va);
  va_end(va);

  // Time stamp and add a message number to the message
  _dos_gettime(&t);
  sprintf(s,"%02d%02d%02d%02d%03d%s",t.hour,t.minute,t.second,
          t.hsecond,messagenum,s2);

  // Insure that the log entry isn't longer than 90 chars
  // (leave room for checksum, CR, etc.)
  s[90] = 0;

  // Copy the string into the queue
  strcpy(Data[Last],s);

  // Adjust log indices
  Last++;
  if (Last >= LogSize)
    Last = 0;
  if (Last == LastIndex)
    LastIndex = -1;
  if (Last == Index)
    Index++;
  if (Index >= LogSize)
    Index = 0;
}

//CEDSLog::AddFromSlave - Take a response from downstream and put it in
//the EDSlog in a form suitable to be sent upstream
void CEDSLog::AddFromSlave(char* r)
{
	int i;
	char s[200];

	//make a copy, excluding Response, Address, messages remaining and
	//message length.
	strlcpy(s, r + 7, 200); 

	//find the termination character
	for(i=0; s[i] != '\0'; i++);

	//strip trailing checksum
	if (i < 100) s[i-2]='\0';

	//if it is a malformed (too long) string, do nothing
	else return; 

    // Insure that the log entry isn't longer than 90 chars
    // (leave room for checksum, CR, etc.)
    s[90] = 0;

	//copy the string into the log queue
	strcpy(Data[Last],s);

	//adjust the indices
	Last++;
	if(Last >= LogSize) Last=0;
	if(Last == LastIndex) LastIndex = -1;
	if(Last == Index) Index++;
	if(Index >= LogSize) Index = 0;;
}

/////////////////////////////////////////////////////////////////////////////
// CEDSLog::SendNextMessage - Take the first message out of the queue and send
void CEDSLog::SendNextMessage()
{
  int MessageCount;
  int Checksum;
  int MessageLength;
  int i;
  char s[120],s2[120];

  MessageCount = Last - Index;
  if (MessageCount < 0)
    MessageCount += LogSize;
  if (MessageCount == 0)
  {
    MessageLength = 0;
    sprintf(s,"2%02d%02d",MessageCount,MessageLength);
  }
  else
  {
    MessageLength = strlen(Data[Index]);
    sprintf(s,"2%02d%02d%s",MessageCount,MessageLength,Data[Index]);
  }
  Checksum = Address;
  for (i=0;i<strlen(s);i++)
    Checksum ^= s[i];
  Checksum &= 255;
  sprintf(s2,"%s%02X\r",s,Checksum);
  RespondToHost(s2);
  if (MessageCount == 0)
    LastIndex = -1;
  else
  {
    LastIndex = Index;
    Index++;
    if (Index >= LogSize)
    Index = 0;
  }
}

/////////////////////////////////////////////////////////////////////////////
// CEDSLog::RepeatLastMessage - Repeat the last message
void CEDSLog::RepeatLastMessage()
{
  int MessageCount;
  int Checksum;
  int MessageLength;
  int i;
  char s[120],s2[120];

  MessageCount = Last - Index;
  if (MessageCount < 0)
    MessageCount += LogSize;
  if (LastIndex == -1)
  {
    MessageLength = 0;
    sprintf(s,"3%02d%02d",MessageCount,MessageLength);
  }
  else
  {
    MessageLength = strlen(Data[LastIndex]);
    sprintf(s,"3%02d%02d%s",MessageCount,MessageLength,Data[LastIndex]);
  }
  Checksum = Address;
  for (i=0;i<strlen(s);i++)
    Checksum ^= s[i];
  Checksum &= 255;
  sprintf(s2,"%s%02X\r",s,Checksum);
  RespondToHost(s2);
}

/*
/////////////////////////////////////////////////////////////////////////////
// UpdateEDS - Add periodic EDS entries
void UpdateEDS()
{
  int i;

  // Send an EDS entry giving all digital I/O and parallel port data
  // once 1.5 seconds.
  // Also report all subsystem positions.
  if ((Timer - EDSTime) > (1000*CKF))
  {
    // Log Analog Readings
    EDSLog.Add(800,"C0D%+07.3fC1D%+07.3fC2D%+07.3fC3D%+07.3f",
               AnalogData[0],AnalogData[1],AnalogData[2],AnalogData[3]);

    // Log Digital I/O values
    EDSLog.Add(801,"D%03X%1X",
              (Tilt.Dir[0] << 0)|(Tilt.Dir[1] << 1)|(Tilt.Dir[2] << 2)|
              (Tilt.Cur[0] << 3)|
              (Tilt.Pwr[0] << 4)|
              (Tilt.Brk[0] << 5)|
              (Tilt.Mon[0] << 6)|(Tilt.Mon[1] << 7)|(Tilt.Mon[2] << 8)|
              (Tilt.Stp[0] << 9)|(Tilt.Stp[1] << 10)|(Tilt.Stp[2] << 11),
              (GetDigBit(19) << 0)|(GetDigBit(20) << 1));

    // Log DGH reading status
    for (i=0;i<3;i++)
    {
      if (DGHs[i].ErrorCount)
        EDSLog.Add(802,"D%cRE",'A'+i);
      else
        EDSLog.Add(802,"D%cR%+07.3f",'A'+i,DGHs[i].Reading);
    }

    // Log all subsystem positions
    // Log Tilts Motion
    EDSLog.Add(803,"PT0%1dPT1%1dPT2%1d",
               Tilt.Motion[0],Tilt.Motion[1],Tilt.Motion[2]);

    // Log servo parameters for m3 support
    EDSLog.Add(804,"S%1d%+07.3f%+07.3f%+07.3f%+07.3f",
               PresVacMode,PresVac.ave,PresVac.P,PresVac.I,PresVac.D);
    EDSLog.Add(805,"W%04d%04d",VacuumPulseWidth,PressurePulseWidth);

    EDSTime = Timer;
  }
}
*/

Generated by GNU Enscript 1.6.5.2.
Document Actions