user19892: This should be fairly straightforward, but there must be something I'm not understanding about buffers. 
I have serial data coming in to my Mega via Zigbee. I'm sending a string just like this: ~-0.6016800, where ~ is my unique starting delimiter and my end delimiter is a carriage return \n. I'm sure it's coming through the zigbee correctly, because I can see my string show up looking good in XCTU when I plug the zigbee into the PC. Here is my code:
const int numChars = 15;
char incoming[numChars];

static boolean Receiving = false;
static byte ndx = 0;
char startMarker = '~';
char endMarker = '\n';
byte K;
bool allreceived = false;

void Get() {
    memset(incoming, NULL, strlen(incoming));
    while (allreceived == false) {
        if (Serial.available() > 0) {
            K = Serial.read();
            if (Receiving == true) {
                if (K != endMarker) {
                    incoming[ndx] = K;
                    ndx++;
                    if (ndx >= numChars) {
                        ndx = numChars-1;
                    }
                }
                else {
                    incoming[ndx] = '\0'; // terminate the string
                    Receiving = false;
                    ndx = 0;
                    allreceived = true;
                }
            }
            else if (K == startMarker) {
                Receiving = true;
                ndx = 0;
            }
        }
    }
}

So somehow my buffer is overflowing or something, no matter how big I make it. I see the array start off as I expect it for the first few characters, then it turns into unintelligible characters. I think part of the problem might be that the serial data is coming through at a different rate or something, because I can see my starting delimiter ~ on my LCD screen in the middle of my array. The way it's set up, I should never be able to see my start delimiter unless I miss my end delimiter without ending the array. (Note- I've tried different unique end delimiters too, but this seems to work just the same) 
Any ideas on what might be causing this??
EDIT: NOW TRYING THIS CODE- I'm still having the buffer overflow, and I'd be really grateful if someone could take a look.
Sending code:
void PayloadSendit(void) {   

Payload[0] = '~';  //1 char unique start

if (outFormat == 0) {
    Payload[1] = '0';
    Payload[2] = '$';  //spacer
    strcat(Payload, stringval); //5 char for hashmarks
}
else {
    Payload[1] = '1';
    strcat(Payload, stringvalin);  //6 char for inch
}
if (dotpos <= 99) {
    Payload[8] = '@';  //spacer
    char dotpos2[2];
    sprintf(dotpos2, "%i", dotpos);
    strcat(Payload, dotpos2);    //2 char 
}
else {
    char dotpos3[3];
    sprintf(dotpos3, "%i", dotpos);
    strcat(Payload, dotpos3);   //3 char ///////////////////////////
}

Payload[11] = '0';  //1 char for "no errors" code
Payload[12] = '*';  //1 char end delimiter
Serial.println(Payload);
Serial.flush();
memset(Payload, '\0', 13);

}
Receiving code:
  void Get2() {
    memset(Payload, '\0', bufsize);
    while (allreceived == false) {
        int K = Serial.read();
        switch (K) {
        case -1: // Nothing to read
            break;

        case '~': // Starting a new string
            Receiving = true;
            break;

        default: // Any other characters.
            break;

        }
        if (Receiving) {
            for (int i = 0; i <= 10; i++) {
                Payload[i] = Serial.read();
            }
            if (Serial.read() == '*') {
                Receiving = false;
                allreceived = true;
            }
            else { memset(Payload, '\0', bufsize); }  //destroy array if end delimiter not where it should be
        }

    }
}  


user4143: My guess is this line:
memset(incoming, NULL, strlen(incoming));

When "incoming" hasn't yet been cleared, what is the length of it? It's whatever it counts up until the first '\0' character - and where is that character on the first run through? Who knows.
Instead you should memset the size of the buffer, not the number of non-NULL characters in the buffer:
memset(incoming, NULL, numChars);

However, that is a pointless operation, since you are storing a C string and (should be) properly terminating it, so it really doesn't matter what is in the array to begin with since you are just overwriting it anyway. So you can completely lose that whole function call.
You might like to re-write your receiving routine to use a switch/case construct instead of ifs and elses - it'll be much easier to follow:
#define EVER ;;

const int numChars = 15;
char buffer[numChars];

void Get() {
    int pos = 0;
    bool receiving = false;

    for (EVER) {
        int K = Serial.read();
        switch(K) {
            case -1: // Nothing to read
                break;

            case '\n': // Got the end of the string.
                if (receiving) return; // We have a valid string.
                pos = 0; // Reset pointer to start of buffer
                break;

            case '~': // Starting a new string
                pos = 0;
                receiving = true;
                break;

            default: // Any other characters.
                if (receiving) {
                    if (pos < numChars-2) {
                        buffer[pos++] = K;
                        buffer[pos] = 0;
                    }
                }
                break;
        }
    }
}


19892: <p>This should be fairly straightforward, but there must be something I'm not understanding about buffers. </p>

<p>I have serial data coming in to my Mega via Zigbee. I'm sending a string just like this: <code>~-0.6016800</code>, where <code>~</code> is my unique starting delimiter and my end delimiter is a carriage return <code>\n</code>. I'm sure it's coming through the zigbee correctly, because I can see my string show up looking good in XCTU when I plug the zigbee into the PC. Here is my code:</p>

<pre class="lang-cpp prettyprint-override"><code>const int numChars = 15;
char incoming[numChars];

static boolean Receiving = false;
static byte ndx = 0;
char startMarker = '~';
char endMarker = '\n';
byte K;
bool allreceived = false;

void Get() {
    memset(incoming, NULL, strlen(incoming));
    while (allreceived == false) {
        if (Serial.available() &gt; 0) {
            K = Serial.read();
            if (Receiving == true) {
                if (K != endMarker) {
                    incoming[ndx] = K;
                    ndx++;
                    if (ndx &gt;= numChars) {
                        ndx = numChars-1;
                    }
                }
                else {
                    incoming[ndx] = '\0'; // terminate the string
                    Receiving = false;
                    ndx = 0;
                    allreceived = true;
                }
            }
            else if (K == startMarker) {
                Receiving = true;
                ndx = 0;
            }
        }
    }
}
</code></pre>

<p>So somehow my buffer is overflowing or something, no matter how big I make it. I see the array start off as I expect it for the first few characters, then it turns into unintelligible characters. I think part of the problem might be that the serial data is coming through at a different rate or something, because I can see my starting delimiter <code>~</code> on my LCD screen in the middle of my array. The way it's set up, I should never be able to see my start delimiter unless I miss my end delimiter without ending the array. (Note- I've tried different unique end delimiters too, but this seems to work just the same) </p>

<p>Any ideas on what might be causing this??</p>

<p>EDIT: NOW TRYING THIS CODE- I'm still having the buffer overflow, and I'd be really grateful if someone could take a look.</p>

<p><strong>Sending code:</strong></p>

<pre><code>void PayloadSendit(void) {   

Payload[0] = '~';  //1 char unique start

if (outFormat == 0) {
    Payload[1] = '0';
    Payload[2] = '$';  //spacer
    strcat(Payload, stringval); //5 char for hashmarks
}
else {
    Payload[1] = '1';
    strcat(Payload, stringvalin);  //6 char for inch
}
if (dotpos &lt;= 99) {
    Payload[8] = '@';  //spacer
    char dotpos2[2];
    sprintf(dotpos2, "%i", dotpos);
    strcat(Payload, dotpos2);    //2 char 
}
else {
    char dotpos3[3];
    sprintf(dotpos3, "%i", dotpos);
    strcat(Payload, dotpos3);   //3 char ///////////////////////////
}

Payload[11] = '0';  //1 char for "no errors" code
Payload[12] = '*';  //1 char end delimiter
Serial.println(Payload);
Serial.flush();
memset(Payload, '\0', 13);
</code></pre>

<p>}</p>

<p><strong>Receiving code:</strong></p>

<pre><code>  void Get2() {
    memset(Payload, '\0', bufsize);
    while (allreceived == false) {
        int K = Serial.read();
        switch (K) {
        case -1: // Nothing to read
            break;

        case '~': // Starting a new string
            Receiving = true;
            break;

        default: // Any other characters.
            break;

        }
        if (Receiving) {
            for (int i = 0; i &lt;= 10; i++) {
                Payload[i] = Serial.read();
            }
            if (Serial.read() == '*') {
                Receiving = false;
                allreceived = true;
            }
            else { memset(Payload, '\0', bufsize); }  //destroy array if end delimiter not where it should be
        }

    }
}  
</code></pre>
 4143: <p>My guess is this line:</p>

<pre><code>memset(incoming, NULL, strlen(incoming));
</code></pre>

<p>When "incoming" hasn't yet been cleared, what is the length of it? It's whatever it counts up until the first '\0' character - and where is that character on the first run through? Who knows.</p>

<p>Instead you should memset the size of the buffer, not the number of non-NULL characters in the buffer:</p>

<pre><code>memset(incoming, NULL, numChars);
</code></pre>

<p><strong>However</strong>, that is a pointless operation, since you are storing a C string and (should be) properly terminating it, so it really doesn't matter what is in the array to begin with since you are just overwriting it anyway. So you can completely lose that whole function call.</p>

<p>You might like to re-write your receiving routine to use a <code>switch/case</code> construct instead of ifs and elses - it'll be much easier to follow:</p>

<pre><code>#define EVER ;;

const int numChars = 15;
char buffer[numChars];

void Get() {
    int pos = 0;
    bool receiving = false;

    for (EVER) {
        int K = Serial.read();
        switch(K) {
            case -1: // Nothing to read
                break;

            case '\n': // Got the end of the string.
                if (receiving) return; // We have a valid string.
                pos = 0; // Reset pointer to start of buffer
                break;

            case '~': // Starting a new string
                pos = 0;
                receiving = true;
                break;

            default: // Any other characters.
                if (receiving) {
                    if (pos &lt; numChars-2) {
                        buffer[pos++] = K;
                        buffer[pos] = 0;
                    }
                }
                break;
        }
    }
}
</code></pre>
