user57258: I currently have 2 Arduinos both hooked up to HC-12s and have successfully got them communicating. But I am now trying to be able to process the data sent across and store it as a variable so as to make comparisons. But I'm getting junk data that comes over with the code, which prevents me from being able to do these comparisons.
For example, from one arduino when I sent "stop" I received
stop<⸮=l⸮⸮⸮

I cant figure out what this junk is from, also, I've limited the receivedChars to a size of 4, why am I getting more than 4 characters?
The code from the sender
#include <SoftwareSerial.h>

SoftwareSerial HC12(3, 2); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
  Serial.print("Checking HC-12 module: ");
  HC12.write("AT");               //Check for response(this is sending data to the HC, the SET pin must be low for this to work)
  delay(1000);
  HC12.write("AT+RX");            //Gets configs of HC-12
}

void loop() {
  while (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) {      // If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }
}

The simplified receiving code
    #define BYTELENGTH 4 //Limited to 4 chars
       #define ENDMARKER '\n'  //To know when stop stop reading serial
    
       SoftwareSerial HC12(3, 2); // HC-12 TX Pin, HC-12 RX Pin

  Serial.begin(9600); //Fastest it can be on the nano
  HC12.begin(9600); // Serial port to HC12s
      
      char receivedChars[BYTELENGTH - 1];
      int byteIndex;
      char curChar;
      
      while (HC12.available()) {        // If HC-12 has data
        curChar = HC12.read();
        if(curChar != ENDMARKER){
          receivedChars[byteIndex] = curChar;
          byteIndex++;
        }
      }
      if(strcmp(receivedChars,"") != 0){
        Serial.println(receivedChars);
        receivedChars[0] = 0;
      }


user42015: I would say you are printing data outside of your char array, because you didn't terminate the C-string in it and the size of your array is too small.
You have
#define BYTELENGTH 4 //Limited to 4 chars
char receivedChars[BYTELENGTH - 1];

so your char array is only 3 elements big. When you want to save a string in that array, you always need to terminate it with a null character \0 (which is also zero in decimal representation). So in that char array you only have space for 2 characters. But your message "stop" is 4 characters long, so you need a char array with at least 5 elements. If you are not in an extreme tight memory situation I would suggest using a buffer with a bit headroom above your biggest message (to be sure, that nothing breaks, if you later use messages that are a bit longer). So more like this:
#define BYTELENGTH 10 //Limited to 4 chars
char receivedChars[BYTELENGTH + 1]; // message length + null character termination

And then you need to change your receiving code to add a null character at the end of the message. Also you might want to actually use the newline character \n as message delimiting (which you are currently not doing):
bool full_message_received = false;

void loop(){
    while (HC12.available()) {        // If HC-12 has data
        curChar = HC12.read();
        if(curChar != ENDMARKER){
          receivedChars[byteIndex] = curChar;
          byteIndex++;
        } else {
          receivedChars[byteIndex] = '\0'; // terminating the c-string
          full_message_received = true; // marking the message as fully received
        }
    }
    if(full_message_received){
        Serial.println(receivedChars);
        receivedChars[0] = 0;
        full_message_received = false;
    }
}

What happens, if I don't terminate the string with a null character? All the functions, that handle C-strings (like strcmp() or also Serial.print()) use the null character to determine, at which point the string is at its end. They don't know the size of your array, so they just read byte after byte, until they reach the null character. They even read more data than is in your array. The junk characters are most likely exactly, what currently is placed in the memory directly after your array. Serial.print() doesn't know, that they don't belong to the C-string, since you didn't use the null character to tell it that. It just keeps printing characters, until it reaches a byte, that is zero.
You even wrote data to memory outside of the array. Depending on what comes directly after your array in the memory this can be without any problems, but you might also overwrite some other data, without knowing, what that data is. That can lead to very weird and hard to debug problems. Be sure to never write outside of your array. You might break things in weird ways.

57258: <p>I currently have 2 Arduinos both hooked up to HC-12s and have successfully got them communicating. But I am now trying to be able to process the data sent across and store it as a variable so as to make comparisons. But I'm getting junk data that comes over with the code, which prevents me from being able to do these comparisons.</p>
<p>For example, from one arduino when I sent &quot;stop&quot; I received</p>
<pre><code>stop&lt;⸮=l⸮⸮⸮
</code></pre>
<p>I cant figure out what this junk is from, also, I've limited the receivedChars to a size of 4, why am I getting more than 4 characters?</p>
<p>The code from the sender</p>
<pre><code>#include &lt;SoftwareSerial.h&gt;

SoftwareSerial HC12(3, 2); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
  Serial.print(&quot;Checking HC-12 module: &quot;);
  HC12.write(&quot;AT&quot;);               //Check for response(this is sending data to the HC, the SET pin must be low for this to work)
  delay(1000);
  HC12.write(&quot;AT+RX&quot;);            //Gets configs of HC-12
}

void loop() {
  while (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) {      // If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }
}
</code></pre>
<p>The simplified receiving code</p>
<pre><code>    #define BYTELENGTH 4 //Limited to 4 chars
       #define ENDMARKER '\n'  //To know when stop stop reading serial
    
       SoftwareSerial HC12(3, 2); // HC-12 TX Pin, HC-12 RX Pin

  Serial.begin(9600); //Fastest it can be on the nano
  HC12.begin(9600); // Serial port to HC12s
      
      char receivedChars[BYTELENGTH - 1];
      int byteIndex;
      char curChar;
      
      while (HC12.available()) {        // If HC-12 has data
        curChar = HC12.read();
        if(curChar != ENDMARKER){
          receivedChars[byteIndex] = curChar;
          byteIndex++;
        }
      }
      if(strcmp(receivedChars,&quot;&quot;) != 0){
        Serial.println(receivedChars);
        receivedChars[0] = 0;
      }
</code></pre>
 42015: <p>I would say you are printing data outside of your <code>char</code> array, because you didn't terminate the C-string in it and the size of your array is too small.</p>
<p>You have</p>
<pre><code>#define BYTELENGTH 4 //Limited to 4 chars
char receivedChars[BYTELENGTH - 1];
</code></pre>
<p>so your <code>char</code> array is only 3 elements big. When you want to save a string in that array, you always need to terminate it with a null character <code>\0</code> (which is also zero in decimal representation). So in that <code>char</code> array you only have space for 2 characters. But your message &quot;stop&quot; is 4 characters long, so you need a <code>char</code> array with at least 5 elements. If you are not in an extreme tight memory situation I would suggest using a buffer with a bit headroom above your biggest message (to be sure, that nothing breaks, if you later use messages that are a bit longer). So more like this:</p>
<pre><code>#define BYTELENGTH 10 //Limited to 4 chars
char receivedChars[BYTELENGTH + 1]; // message length + null character termination
</code></pre>
<p>And then you need to change your receiving code to add a null character at the end of the message. Also you might want to actually use the newline character <code>\n</code> as message delimiting (which you are currently not doing):</p>
<pre><code>bool full_message_received = false;

void loop(){
    while (HC12.available()) {        // If HC-12 has data
        curChar = HC12.read();
        if(curChar != ENDMARKER){
          receivedChars[byteIndex] = curChar;
          byteIndex++;
        } else {
          receivedChars[byteIndex] = '\0'; // terminating the c-string
          full_message_received = true; // marking the message as fully received
        }
    }
    if(full_message_received){
        Serial.println(receivedChars);
        receivedChars[0] = 0;
        full_message_received = false;
    }
}
</code></pre>
<p><strong>What happens, if I don't terminate the string with a null character?</strong> All the functions, that handle C-strings (like <code>strcmp()</code> or also <code>Serial.print()</code>) use the null character to determine, at which point the string is at its end. They don't know the size of your array, so they just read byte after byte, until they reach the null character. They even read more data than is in your array. The junk characters are most likely exactly, what currently is placed in the memory directly after your array. <code>Serial.print()</code> doesn't know, that they don't belong to the C-string, since you didn't use the null character to tell it that. It just keeps printing characters, until it reaches a byte, that is zero.</p>
<p>You even wrote data to memory outside of the array. Depending on what comes directly after your array in the memory this can be without any problems, but you might also overwrite some other data, without knowing, what that data is. That can lead to very weird and hard to debug problems. Be sure to never write outside of your array. You might break things in weird ways.</p>
