So, how does Serial.readBytesUntil() work?

I’ve been toying with the idea of setting the clock on my Arduino RTC (a DS3234 dead-on real time clock) because even though it still keeps good time, it’s still impacted by daylight savings time changes and because setting the time using a boot-loader based upload can be such a hit or miss sort of thing.

So, to accomplish this in as close to real-time as possible, I started thinking about how I could send commands to the arduino from Python code on my Raspberry Pi. I opted for a COMMAND,DATA structure to simplify parsing.

The goal is to keep it simple and to use a part of the serial library that I have not used yet: “Serial.readBytesUntil()” while avoiding the requirement to include “string.h” in my code. Why? Because I need this to be part of a larger program that already stretches the limits of available RAM on my UNO. The upside of using “readBytesUntil” is that you don’t have to keep checking the input buffer and assemble an array of char… the downside of using “readBytesUntil” is that it has a default timeout of 1 second. So how useful this is in the end remains to be fully tested.

Right now, there is no bounds checking… so if the command text is larger than 10 chars and data is larger than 15, you get “unpredictable” results.

Arduino Code:

 

//==============================================================================
// Program:      SerialParse.ino
// Author:       Pete Willard
// Version:      1.0
// Target:       UNO R3, IDE 1.03 
// Date:         2013/04/08
// Time:         09:47:54
// Notes:        
//               Uses Serial I/O
// Reference:    
//==============================================================================
 
//=====[ INCLUDE ]==============================================================
//#include <stdlib.h>
 
 
//=====[ CONSTANTS ]============================================================
#define   DEBUG   1   // 0 = debugging disabled, 1 = enabled
const int bSize = 20; 
 
//=====[ PINS ]=================================================================
int Led = 13;
 
//=====[ VARIABLES ]============================================================
 
    char Buffer[bSize];  // Serial buffer
        char Command[10];    // Arbitrary Value for command size
        char Data[15];       // ditto for data size
        int ByteCount;
 
//=====[ SUBROUTINES ]==========================================================
 
void SerialParser(void) {
//
//  One command per line.  Eventually, Data may have multiple 
//   fields separated by ":"
//  Command Format: "up to 5 Letter command, up to 10 letter data<\n>"
//                  No checking.
//
//  count will  be below Zero on a timeout.
//  read up to X chars or until EOT - in this case "\n" 
 ByteCount = -1;
 ByteCount =  Serial.readBytesUntil('\n',Buffer,bSize);  
 
   if (ByteCount  > 0) {
        strcpy(Command,strtok(Buffer,","));
                  
        strcpy(Data,strtok(NULL,","));             
   }
   memset(Buffer, 0, sizeof(Buffer));   // Clear contents of Buffer
   Serial.flush();
}
 
 
//=====[ SETUP ]================================================================
void setup() {
    Serial.begin(9600);
    pinMode(Led,OUTPUT);
    if (DEBUG) {
        Serial.println("Debugging Enabled");
    }
     
}
//==============================================================================
 
//=====[ LOOP ]=================================================================
void loop() {
 
    SerialParser();
         if (ByteCount  > 0) {
          Serial.print("Command     : ");
          Serial.println(Command);
          Serial.print("Data        : ");
          Serial.println(Data);
         } 
       
}

2 thoughts on “So, how does Serial.readBytesUntil() work?

  1. Shubham says:

    Serial.readBytesUntil(‘#’, (char *) buffer, 70)

    If I am changing this char * to int data type … it’s displaying an error but I want to input integer instead of characters in RFID Tagging systems

    • myadmin says:

      SerialReadBytesUntil will not work with INT datatype. Change the result into what you need later. There are alternative routines for working with numbers, like ParseInt()

Leave a Reply

Your email address will not be published. Required fields are marked *