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 ?.

asked Jun 13, 2017 at 22:23

1 Answer 1

2
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.

answered Jun 13, 2017 at 23:13
4
  • Also needs to add a null to the end of the string before printing it. Commented Jun 13, 2017 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. Commented Jun 14, 2017 at 9:25
  • @Delta_G :guys , i hope u can see the full code given in the link in the comment above. Commented Jun 14, 2017 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." Commented Jun 15, 2017 at 9:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.