How to use Serial.readBytesUntil?

Status
Not open for further replies.

tastewar

New member
I would like to add the ability to read a string from the serial console to my sketch. It should read from the serial port until the user hits the enter key. I tried:

Serial.readBytesUntil ( 13, buf, sizeof(buf) );

And variations (tried a 10 instead of 13), but couldn't make it do what I thought it should. I even tried with 65 (and 'A') but it didn't seem to stop when I hit the letter A.

Is there a sample sketch that uses this function that I could play around with? I'm using a Teensy 3.1
 
I ran into trouble reading from the console, too. The code linked in my sig has my solution. I've got a function that reads a line of text from the command line. It handles backspaces and timeouts. The timeouts are really handy if you need to be sure the application will be in a known state. If you have trouble finding the right code from the Doxygen pages, post here again and I'll go find it for you. The application has both a console interface and a Morse code interface so don't let that confuse you. :) The code works both on a Teensy 3.0 and a Teensy 3.1.
 
If I were you, I would probably look at the stuff that pictographer showed... I have used the readBytesUntil a few times now. For example in the Phoenix Hexapod code base, I have some debug terminal code base, where for example with some of the robots that use XBees I have a debug command to output some xbee information. Some of the code looks like:
Code:
void PrintXBeeIDInfo(char *pszID) {
  char ab[20];
  int cbRead;
  while (XBeeSerial.read() != -1)
    ;  // Flush out anything still pending. 
  XBeeSerial.print("AT");  
  XBeeSerial.println(pszID);  // Lets print out the ID;
  XBeeSerial.flush();
  cbRead = XBeeSerial.readBytesUntil('\r', ab, sizeof(ab));
  if (cbRead) {
    DBGSerial.print(pszID);
    DBGSerial.print(": ");
    DBGSerial.write(ab, cbRead);
    DBGSerial.println();
  }
}  

boolean CommanderInputController::ProcessTerminalCommand(byte *psz, byte bLen)
{
  if ((bLen == 1) && ((*psz == 'x') || (*psz == 'X'))) {
    char ab[10];
    int cbRead;
    delay(15);  // see if we have fast command mode enabled.
    XBeeSerial.print(F("+++")); 
    XBeeSerial.flush();
    XBeeSerial.setTimeout(20);  // give a little extra time
    if (XBeeSerial.readBytesUntil('\r', ab, 10) > 0) {
      // Ok we entered command mode, lets print out a few things about the XBee
      PrintXBeeIDInfo("MY");
      PrintXBeeIDInfo("DL");
      PrintXBeeIDInfo("ID");
      PrintXBeeIDInfo("EA");
      PrintXBeeIDInfo("EC");
Note: I have defines for XBeeSerial and DBGSerial as they may be on different Hardware (or software) serial ports on different robots...
Also on Teensy only could change the above to use other code to clear out the queue...

Note: one gotcha I have run into before with my terminal monitor code, which waits for a CR or LF to signal a command, is when I am using the Arduino Serial Monitor window. Hitting a return to send the line may not send out a CR or LF. This is controlled by the drop down list toward the bottom right of the terminal monitor window. I normally make sure to set mine to: both NL & CR

Kurt
 
Note: one gotcha I have run into before with my terminal monitor code, which waits for a CR or LF to signal a command, is when I am using the Arduino Serial Monitor window. Hitting a return to send the line may not send out a CR or LF. This is controlled by the drop down list toward the bottom right of the terminal monitor window. I normally make sure to set mine to: both NL & CR

Kurt

Thank you both, but this was the key! I sure overlooked that setting!
 
Source code at https://github.com/pictographer/didah
Complete documentation at http://pictographer.com/didah

I've got a function that reads a line terminated by a newline or by a timeout that might be a useful starting point in http://pictographer.com/didah
It is used for command line interaction. The function is called readLine() and is documented here:

http://pictographer.com/didah/action...664063ddd74f20

My original post used to have a link, but the signature feature got disabled at some point, so my post became less useful. :-(

The code contains various potentially useful bits including Morse code generation, touchRead() examples, a tiny domain-specific language interpreter for interpreting input commands, and various other bits that might be good to learn from or laugh at. Dreaming of someday rewriting the code for low power, but for now the main loop uses polling rather than interrupts, so the flow of control is relatively simple.
 
Status
Not open for further replies.
Back
Top