CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Parsing a response string from rs232 port

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GUEST
Guest







Parsing a response string from rs232 port
PostPosted: Tue Jul 27, 2004 9:52 am     Reply with quote

I want to issue a command through rs232 and store the result as a string.
The problem I have is that the string is formatted with a carriage return and line feed at the begining and end, i.e CRLFResponseCRLF. Is it possible to store the whole thing into a string and retrieve the Response only, or will the carriage return cause nothing to be stored.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 27, 2004 10:07 am     Reply with quote

Here is an example that may help a bit. I am not posting all the code but data is received and put into a buffer through an ISR. This function is called from main to receive commands
Code:

void TerminalHandler()
{
  static uint8_t  index = 0;         /* index of buffer */
  int           i;                  /* character from buffer */
  char          ch;                 /* character from buffer */
  static char   buf[MAX_LINE_LEN]; /* for the line entry */

  while (TRUE)
  {
    i = HostReadChar();

    /* port error */
    if (i < 0)
      return;

    ch = (char)i;
    switch (ch)
    {
      /* illegal characters */
      case '\a':
      case '\f':
      case '\n':
      case '\t':
      case '\v':
      /* escape */
      case 0x1B:
        break;

      /* backspace */
      case '\b':
        if (index)
        {
          --index;

          /* do a destructive backspace */
          HostWriteChar('\b');
          HostWriteChar(' ');
          HostWriteChar('\b');
        }
        break;

      /* enter */
      case '\r':
        HostWriteChar('\r');
        HostWriteChar('\n');
        buf[index] = '\0';

        /* did we get something? */
        if (index)
        {
          if (!CommandHandler(buf, &CommandData[0]))
            HostWriteRomString("Invalid Command. Type ? for help.\r\n");
        }

        buf[0] = '\0';
        index = 0;

        /* write the prompt */
        HostWriteChar('>');
        break;

      /* all the rest of the characters */
      default:
        /* leave room for null at the end */
        if (index < (MAX_LINE_LEN - 1))
        {
          buf[index] = ch;
          if (!ch)
            ch = 0;
          HostWriteChar(ch);
          index++;
        }
        break;
    }
  }
}


Here is the command handler:
Code:

static bool CommandHandler(
  char            *pArgs,         /* FIX ME: add comment */
  COMMAND_DATA    *pCommandData)  /* FIX ME: add comment */
{
  static char cmd[MAX_LINE_LEN];  /* local buffer */
  bool        found = 0;          /* valid command? */

  if (pArgs)
  {
    /* parse and remove the only the first command */
    pArgs = stptok(pArgs, cmd, sizeof(cmd), " ");

    /* find and execute command function */
    while (pCommandData->pStr != NULL)
    {
      if (strcmpipgm2ram(cmd, pCommandData->pStr) == 0)
      {
        if (pCommandData->pFunction != NULL)
        {
          (void)strupr(cmd);
          (void)pCommandData->pFunction(cmd, pArgs);
        }
        found = 1;
        break;
      }
      pCommandData++;
    }
  }
  return (found);
}


And here are the commands:
Code:

typedef rom struct  _cmd_t
{
  const rom char  *pStr;  /* command */
  void (*pFunction) (char *pCmd, char *pArgs); /* command function */
  const rom char  *pDescription;  /* command description (for help) */
} COMMAND_DATA;

static COMMAND_DATA CommandData[] =
{
  /* hidden commands - description is NULL */
  { { "help"      },   VHelpCommand,     { NULL                     } },
  { { "TEST"      },   SelfTestCommand,  { NULL                     } },
  { { "VIRGINIZE" },   VirginCommand,    { NULL                     } },
  { { "bootmode"  },   BootCommand,      { NULL                     } },

  /* application commands */
  { { "ver"       },   VersionCommand,   { "sofware version"        } },
  { { "abus"      },   ABUSCommand,      { "sends command out abus" } },
  { { "input"     },   InputCommand,     { "input status"           } },
  { { "photocell" },   PhotocellCommand, { "photocell status"       } },
  { { "data"      },   DataCommand,      { "dump program data"      } },
  { { "restore"   },   ReInitCommand,    { "restore defaults"       } },
  { { "restart"   },   RestartCommand,   { "restart device"         } },
  { { "dump"      },   EEPROMDumpCommand,{ "dumps the contents of the eeprom" } },
  { { "host"      },   HostModeCommand,  { "allows this device to act as host" } },
  { { "??"        },   VHelpCommand,     { "verbose help"           } },
  { { "?"         },   THelpCommand,     { "terse help"             } },
  { { NULL        },   NULL,             { NULL                     } },
};



Not sure how well CCS handles pointers to functions. Also note that since CCS does not allow constant strings the above would not work with CCS and you would have to change it a bit to check for the commands. This code is for the C18 compiler BTW.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Jul 27, 2004 12:00 pm     Reply with quote

Bytes is Bytes. You can choose any termination character you want, or none at all if you know how many bytes long the string will be.
In your case you could poll for a <CR> and then read till the next <CR>.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group