Guus
Guus

Reputation: 11

C char array, strange characters

I'm experimenting with C for a project i'm working on, the idea is to loop over an RX buffer (received data, pointer to a string in this case) and parse out all command codes.

The structure of a command:

|COMMAND/CHECKSUM#

So the | character indicates a the start of a data block, followed by the command, than a / character which separates the command from the checksum and then the # character to indicate the end of a data block.

Everything seems to be working fine, here's the full code:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void append(char *s, char c)
{
   int len = strlen(s);
   s[len] = c;
   s[len+1] = '\0';
}

int getPayload(char *RXbuf, char *rxPayload, char *rxChecksum)
{
   int started = 0;
   int separated = 0;
   int parsedLength = 0;
   int len = strlen(RXbuf);

   if (RXbuf[0] == '#')
   {
      parsedLength++;
   }

   for (int i = 0; (i < len && RXbuf[i] != '#'); i++)
   {
      parsedLength++;

      if (started == 0 && RXbuf[i] == '|')
      {
         started = 1;
         continue;
      }

      if (RXbuf[i] == '/')
      {
         separated = 1;
         continue;
      }

      if (started == 1)
      {
         if (separated == 0)
         {
            append(rxPayload, RXbuf[i]);
         }
         else
         {
            append(rxChecksum, RXbuf[i]);
         }
      }
   }

   return parsedLength;
}

int main()
{
   char *RXbuf = "|SR-5632-LED-0002/412215590#dfsfdsf|SR-5632-LED-0002/412215590#dsf|SR-5632-LED-0002/412215590#";

   printf("RX Data: %s\n", RXbuf);

   printf("##############################################################################################################################\n");

   while(1)
   {
      char rxPayload[50];
      char rxChecksum[20];

      int started = 0;
      int stopped = 0;
      int separated = 0;

      int parsedLength = getPayload(RXbuf, rxPayload, rxChecksum);

      if (strlen(rxPayload) > 10)
      {
         printf("-----------------------------------------------------------------------------------------------------------------\n");
         printf("RX Data: %s\n", RXbuf);
         printf("%s/%s\n", rxPayload, rxChecksum);
         printf("\n");
         printf("-----------------------------------------------------------------------------------------------------------------\n");
      }

      RXbuf = RXbuf + parsedLength;

      rxPayload[0] = 0;
      rxChecksum[0] = 0;

      if (parsedLength == 0)
      {
         break;
      }

   }

   printf("\n");
   printf("##############################################################################################################################\n");
}

It compiles ok, and it runs without errors, but the output isn't what i expect to be. It loops and parses out the commands, but together with the first command it outputs some weird characters as you can see in the output of the above program:

$ ./rxdestruct
RX Data: |SR-5632-LED-0002/412215590#dfsfdsf|SR-5632-LED-0002/412215590#dsf|SR-5632-LED-0002/412215590#
##############################################################################################################################
-----------------------------------------------------------------------------------------------------------------
RX Data: |SR-5632-LED-0002/412215590#dfsfdsf|SR-5632-LED-0002/412215590#dsf|SR-5632-LED-0002/412215590#
���\�SR-5632-LED-0002/412215590

-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
RX Data: dfsfdsf|SR-5632-LED-0002/412215590#dsf|SR-5632-LED-0002/412215590#
SR-5632-LED-0002/412215590

-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
RX Data: dsf|SR-5632-LED-0002/412215590#
SR-5632-LED-0002/412215590

-----------------------------------------------------------------------------------------------------------------

##############################################################################################################################

I think this has something to do with the pointers for the char arrays rxPayload & rxChecksum but i don't know how to fix it in this case. These are now set to char arrays of [50] and [20] but ideally these would have a dynamic size so i can have long command codes including several parameters, some help on that would also be very welcome! But the weird characters are the main problem.

What am i doing wrong, what can i do to improve it?

Upvotes: 1

Views: 1455

Answers (1)

Barmar
Barmar

Reputation: 780655

You need to initialize your string variables before you can safely append to them.

  char rxPayload[50] = "";
  char rxChecksum[20] = "";

Or you could just move these two lines:

  rxPayload[0] = 0;
  rxChecksum[0] = 0;

before the call to getPayload().

Upvotes: 2

Related Questions