Serial String Input

Bullwinkle

  • Full Member
  • ***
    • Posts: 14
    • View Profile
UPDATE! Now I *am* embarassed!  I myself had posted a solution / workaround to this problem 3 Years ago! LOL!!   See the post, "Serial.read baud-rate issues..." in this forum.  In a nutshell, as Ben Rose pointed out, the processor just can't accurately do high baud rates (at least on Input).
So, the workaround is: USE 9600 Baud!   I just tested it, and it's working!
-------------
Hi y'all!  This question is embarassingly simple, but I'm stumped!  I'm talking to TinyDuino via USB Serial connection 115200 baud.  I send single-character "commands" from a Windows program to the tiny, pull them off the wire in the main loop() with:
  if (Serial.available() > 0)
      processIncomingByte(Serial.read());
 and all is well.  Works great, no issues.   I've decided to expand my "protocol" a bit, to consist of multi-character "strings", e.g. - "DELAY 500", etc.  So I have tried this:
String str; // module declaration
in loop():
   if (Serial.available() > 0)
   {
     str = Serial.readStringUntil('\n');
     Serial.print("str = "); Serial.println(str);
     // also tried:  Serial.print("str = "); Serial.println(str.c_str());
   }
An example of the result (from the input, "M1234") is:
str = L⸮⸮
   I've also tried using char buffer[32], and appending single chars to that buffer, with a check for the '\n'.  Similar result.
What the heck am I missing here?  This ought to be falling-off-a-rock simple!
This doesn't work even with the Arduino serial monitor either; tho single-char inputs are fine, and all println outputs from the tiny.
  Any thoughts?
« Last Edit: September 16, 2021, 12:38:27 PM by Bullwinkle »


lennevia

  • Administrator
  • Hero Member
  • *****
    • Posts: 437
    • View Profile
Hello Bullwinkle,

Could you attach your program's file? Should be faster to help you debug that way!

Did you try Serial.readString before trying the Until version? https://www.arduino.cc/reference/en/language/functions/communication/serial/readstring/

My last quick tip is to check that your baud rate in the code matches your baud rate in the serial monitor. Let me know if that helps!

Best,
Réna


Bullwinkle

  • Full Member
  • ***
    • Posts: 14
    • View Profile
Hi Rena,
   Please "refresh" this topic/post - I updated it 20 mins before you replied :)
readStringUntil('\n') works just fine.
Thanks for the reply!
Jim


lennevia

  • Administrator
  • Hero Member
  • *****
    • Posts: 437
    • View Profile
Bullwinkle,

I am glad you were able to solve the problem! Sorry for missing the update!


 

SMF spam blocked by CleanTalk