1

I am trying to communicate NODEMCU and Arduino Uno using Serial Communication (RX,TX) using SoftwareSerial library.

Here is my code

int count;
char reqst[] = " ";

void setup() {

  Serial.begin(115200);    
  Serial.println("Ready");

}

void loop() {

 while ( Serial.available() > 0 )  { 

  char inChar = Serial.read();
  reqst[count] = inChar;
  count++;

     if (inChar == '\n') {

        Serial.print("String: ");
        Serial.println(reqst);
        count = 0;
        delay(1000);

     }

 }


}

here my output in serial monitor is showing ;

String: aj 779

String: am 779

I have checked that from NODEMCU i am sending correct string but in Arduino Uno the number "779" is coming from nowhere.

So my problem is i am not be able to execute my function according to the desired string. Can anyone tell why the number is coming below the string ?.

3
char reqst[] = " ";

Here you created an array two chars in length. But one of those characters must be used for the null terminator so actually it can only hold a string that's a single character long. If you write past the end of the array that will cause undefined behavior. This is likely to be the cause of the strange results you encountered. You need to size reqst[] large enough to contain any string you may receive, including the null terminator. You should add code to ensure that you never write off the end of the array and that it will always end in a null terminator.

I realize it may only be a simplified minimal example, but in the code you posted there is no need to store the incoming characters. Instead you could just print them as they are received.

4
  • Also needs to add a null to the end of the string before printing it. – Delta_G Jun 13 '17 at 23:44
  • thanks for the help. the reason why i am storing the receiving string is because i will be doing string comparison operations and then do further operations using that string i have received. Do u mean to say that i should fix the char reqst[] array size .? can you have a look at the original code . strong apology for not sharing this one. textuploader.com/d0li1 here output is writting to radio is : am00779 writting to radio is : aj00779 still the 00779 is coming. – JavaEnthusias Jun 14 '17 at 9:25
  • @Delta_G :guys , i hope u can see the full code given in the link in the comment above. – JavaEnthusias Jun 14 '17 at 12:16
  • It's reasonable to provide a minimal code example, I just thought I'd mention it in case you weren't using the string for anything. As I said: "You need to size reqst[] large enough to contain any string you may receive, including the null terminator. You should add code to ensure that you never write off the end of the array and that it will always end in a null terminator." – per1234 Jun 15 '17 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.