Serial read strncmp fails shortly after code runs

Hey everyone, I'm a long time lurker but a first time poster.

I'm working on a project using arduino mega's where I will have a module sending data to the main user interface module via serial. I have the serial string divided in to six sections with a long integer in each section. The digits of the long integer will change depending on what data needs to be sent to the user interface module.

The problem that I'm having is when the "user interface" mega receives the data and performs the strncmp function it will only pass the strncmp 4-5 times after the mega first powers up and after that it fails causing it to not process the data. I'm able to view the serial data being sent to the "user interface" mega via pluggy and can see that there shouldn't be an issue with the data coming in.

The below serial read code is modified from a code that I previously used for process data from a gps receiver that I found online. The code worked for processing the gps data so I'm not sure what I'm doing wrong that is causing an issue with this code.

I have also uploaded screen shots of the serial monitor showing that the strncmp fails and of pluggy showing the continuous data stream coming in to the "user interface" mega.

The only thing that I can say has changed from the strncmp code from when I used it on a teensy to read gps data is the characters listed in the strncmp if statement and the code inside the if statement on what to do with the indexed data.

Any help and advice with what I'm doing wrong would be greatly appreciated.

This is the code that I'm using to generate the serial data.

int d1 = 0;
int d2 = 0;
int d3 = 0;
int d4 = 0;
int d5 = 0;
int d6 = 0;

unsigned long time;
unsigned long podtime = 0;
unsigned long dout = 0;
int dnumber = 1;

void setup() {
  Serial2.begin(9600);

}

void loop() {
  Datacalc();
  Dataout();

}

void Datacalc(){
  dout = dnumber;
  if(d1 == 0){
    unsigned long j1calc = dout * 10;
    dout = j1calc;
  }
  else{
    unsigned long j1calc = dout * 10 + 1;
    dout = j1calc;
  }
    if(d2 == 0){
    unsigned long j2calc = dout * 10;
    dout = j2calc;
  }
  else{
    unsigned long j2calc = dout * 10 + 1;
    dout = j2calc;
  }
    if(d3 == 0){
    unsigned long j3calc = dout * 10;
    dout = j3calc;
  }
  else{
    unsigned long j3calc = dout * 10 + 1;
    dout = j3calc;
  }
    if(d4 == 0){
    unsigned long j4calc = dout * 10;
    dout = j4calc;
  }
  else{
    unsigned long j4calc = dout * 10 + 1;
    dout = j4calc;
  }
    if(d5 == 0){
    unsigned long j5calc = dout * 10;
    dout = j5calc;
  }
  else{
    unsigned long j5calc = dout * 10 + 1;
    dout = j5calc;
  }
    if(d6 == 0){
    unsigned long j6calc = dout * 10;
    dout = j6calc;
  }
  else{
    unsigned long j6calc = dout * 10 + 1;
    dout = j6calc;
  }
}

void Dataout(){
  time = millis();
  unsigned long odtime = time;
  if((odtime - podtime) >= 1000){  
    Serial2.print("$TDS,");
    Serial2.print(dout);
    Serial2.print(",");
    long dout2 = dout + 1000000;
    Serial2.print(dout2);
    Serial2.print(",");
    long dout3 = dout2 + 1000000;
    Serial2.print(dout3);
    Serial2.print(",");
    long dout4 = dout3 + 1000000;
    Serial2.print(dout4);
    Serial2.print(",");
    long dout5 = dout4 + 1000000;
    Serial2.print(dout5);
    Serial2.print(",");
    long dout6 = dout5 + 1000000;
    Serial2.println(dout6);
    podtime = odtime;
  }
}

This is the code that I'm using to receive the data

boolean dataLock = false;
boolean newdata = false;
int charCount = 0;
char inputBuf[255];
byte tIndex[16];
long d1 = 0;
long d2 = 0;
long d3 = 0;
long d4 = 0;
long d5 = 0;
long d6 = 0;


void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);

}

void loop() {
  Dataread();
  Printserial3();

}

void Dataread(){
  while(Serial3.available()) {
  
    char ch = Serial3.read();

    if(ch != '\n') {
      inputBuf[charCount] = ch;
      charCount++;
      inputBuf[charCount] = 0;
    }
    if(ch == '\n') {
      Serial.println("Serial3 Available");
      dataLock = true;
      if(strncmp(inputBuf,"$TDS",4) == 0){
            int commaCount = 0;
            Serial.println("String Compare Good");

        for(int i = 0; i < 254; i++){
          if(inputBuf[i] == ',') {
            tIndex[commaCount] = i + 1;
            inputBuf[i] = 0;
            commaCount++;
          }
          else if(inputBuf[i] == '*'){
            inputBuf[i] = 0;
            break;
          }
        }
        d1 = atol(&inputBuf[tIndex[0]]);
        d2 = atol(&inputBuf[tIndex[1]]);
        d3 = atol(&inputBuf[tIndex[2]]);
        d4 = atol(&inputBuf[tIndex[3]]);
        d5 = atol(&inputBuf[tIndex[4]]);
        d6 = atol(&inputBuf[tIndex[5]]);
        
        newdata = true;
      }
    }
  }
}

void Printserial3(){
  if(newdata == true){

    Serial.print("$TDS,");
    Serial.print(d1);
    Serial.print(",");
    Serial.print(d2);
    Serial.print(",");
    Serial.print(d3);
    Serial.print(",");
    Serial.print(d4);
    Serial.print(",");
    Serial.print(d5);
    Serial.print(",");
    Serial.println(d6);
    newdata = false;
  }
}


Hi @knucklestripper ,
Welcome to the forum..
need to reset charCount to 0 once you've recvd all the data..

good luck.. ~q

Try increasing the baud rate for Serial, running both Serial and Serial3 at 9600 baud is likely filling the Serial buffer, causing a delay that results in a buffer overrun for Serial3.
< edit > Not likely, I see your sending routine has a 1000mS delay.

Looking at the actual documentation, I see :In Arduino (C++), strncmp() is a standard library function used to compare the first n characters of two null-terminated C-style strings (char arrays). Unlike the standard strcmp(), it includes a length limit, which helps prevent memory errors if strings are missing a null terminator".

So, do you ever see nulls in your long integers? Try printing them in hex and count the nulls. Your scheme has built-in failure mode.

qubits-us,

Thanks for catching that! It looks like I missed two lines at the bottom of the if statement which resets the charCount. I copied the two lines of over from the code that I started from and everything works now.

I really appreciate your help!

For reference below is the code.

boolean dataLock = false;
boolean newdata = false;
int charCount = 0;
char inputBuf[255];
byte tIndex[16];
long d1 = 0;
long d2 = 0;
long d3 = 0;
long d4 = 0;
long d5 = 0;
long d6 = 0;


void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);

}

void loop() {
  Dataread();
  Printserial3();

}

void Dataread(){
  while(Serial3.available()) {
  
    char ch = Serial3.read();

    if(ch != '\n') {
      inputBuf[charCount] = ch;
      charCount++;
      inputBuf[charCount] = 0;
    }
    if(ch == '\n') {
      Serial.println("Serial3 Available");
      dataLock = true;
      if(strncmp(inputBuf,"$TDS",4) == 0){
            int commaCount = 0;
            Serial.println("String Compare Good");

        for(int i = 0; i < 254; i++){
          if(inputBuf[i] == ',') {
            tIndex[commaCount] = i + 1;
            inputBuf[i] = 0;
            commaCount++;
          }
          else if(inputBuf[i] == '*'){
            inputBuf[i] = 0;
            break;
          }
        }
        d1 = atol(&inputBuf[tIndex[0]]);
        d2 = atol(&inputBuf[tIndex[1]]);
        d3 = atol(&inputBuf[tIndex[2]]);
        d4 = atol(&inputBuf[tIndex[3]]);
        d5 = atol(&inputBuf[tIndex[4]]);
        d6 = atol(&inputBuf[tIndex[5]]);
        
        newdata = true;
      }
      charCount = 0;
      inputBuf[0] = 0;
    }
  }
}

void Printserial3(){
  if(newdata == true){

    Serial.print("$TDS,");
    Serial.print(d1);
    Serial.print(",");
    Serial.print(d2);
    Serial.print(",");
    Serial.print(d3);
    Serial.print(",");
    Serial.print(d4);
    Serial.print(",");
    Serial.print(d5);
    Serial.print(",");
    Serial.println(d6);
    newdata = false;
  }
}

for future reference when parsing strings for data consider strtok which enables the use of multiple delimiters
e.g. parse comma separated data into integer array

// strtok example - parse comma seperated data into integer array

void setup() {
  int data[100] = { 0 };  // array for resultant data
  int index = 0;          // array index
  Serial.begin(115200);
  Serial.println("\n\nparse comma separated data into integers");
  char str[] = " {DATA: 1304, 1188,  494, 1210,  2100, 1206,  1306, 1188 : END}";  // text to tokenise
  char* pch;                                                                 // pointer to tokens
  Serial.print("Splitting string ");
  Serial.print(str);
  Serial.println(" into tokens:");
  pch = strtok(str, ",:");                      // get first token
  while (pch != NULL) {
    Serial.print("token found ");
    Serial.print(pch);                           // print it
    if (sscanf(pch, "%d", &data[index]) == 1) {  // attempt to convert string to integer
      Serial.print("   integer = ");
      Serial.println(data[index++]);             // print integer
    } else
      Serial.println(" NOT integer");
    if (index == 100) {
      Serial.println("array overflow!!");         // check for array overflow
      break;
    }
    pch = strtok(NULL, ",:");                     // get next token
  }
  Serial.println("\nresultant data array ");      // print resultant array
  for (int i = 0; i < index; i++) {
    Serial.print(data[i]);
    Serial.print(' ');
  }
}

void loop() {}

serial monitor output

parse comma separated data into integers
Splitting string  {DATA: 1304, 1188,  494, 1210,  2100, 1206,  1306, 1188 : END} into tokens:
token found  {DATA NOT integer
token found  1304   integer = 1304
token found  1188   integer = 1188
token found   494   integer = 494
token found  1210   integer = 1210
token found   2100   integer = 2100
token found  1206   integer = 1206
token found   1306   integer = 1306
token found  1188    integer = 1188
token found  END} NOT integer

resultant data array 
1304 1188 494 1210 2100 1206 1306 1188 

Yeth. strtok() is not the usual function you come across wrangling strings.

google

strtok C++

to find explanations that aren't obscured by a particular problem's solution code.

Here's one

and another

https://cplusplus.com/reference/cstring/strtok/

and another


They all say the same thing variously. This is one of those functions that is a mystery until it isn't, but is quite handy to know about. Reading multiple expositions on it may help.

a7

Just beware that it modifies the subject character string. And it is most definitely not reentrant-safe, nor thread-safe.

@gfvalvo is right to remind us of the problems with strtok:
the C++ reference strtok and strtok_s states

  • strtok his function is destructive: it writes the '\0' characters in the elements of the string str. In particular, a string literal cannot be used as the first argument of strtok.

  • Each call to strtok modifies a static variable: is not thread safe.

some C++ implementations have strtok_s (guarding against storing outside of the string being tokenized, and by checking runtime constraints.)

on more powerful microcontrollers such as ESP32 could use istringstream, e..g.

// ESP32 istringstream tokenizer example - parse space seperated data into integer array

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

void setup() {
  int data[100] = { 0 };  // array for resultant data
  int index = 0;          // array index
  Serial.begin(115200);
  cout << "\n\nparse space separated data into integers" << std::endl;
  std::string str = " {DATA:  1304,  1188,  494, 1210,  2100, 1206,  1306, 1188 : END}";  // text to tokenise
  std::istringstream iss(str);
  while (iss.good()) {
    int val;
    if (iss >> val)               // attempt to read an integer
      std::cout << "integer " << (data[index++] = val) << '\n';
    else {
      std::string s;              // integer read failed read a string
      iss.clear();
      iss >> s;
      std::cout << "string " << s << std::endl;
    }
  }
  cout << "\nresultant data array " << endl;  // print resultant array
  for (int i = 0; i < index; i++) 
     cout << data[i] << ' ' << endl;
}

void loop() {}

serial monitor output

parse space separated data into integers
string {DATA:
integer 1304
string ,
integer 1188
string ,
integer 494
string ,
integer 1210
string ,
integer 2100
string ,
integer 1206
string ,
integer 1306
string ,
integer 1188
string :
string END}

resultant data array 
1304 
1188 
494 
1210 
2100 
1206 
1306 
1188 

Another option may be sscanf and its variants. Here is a simple example of parsing a date/time stamp. The syntax is a bit cryptic and like a cut down Regex :

  int dd = 0, mm = 0, yyyy = 0, hh = 0, mi = 0, ss = 0;

  // target string example: "26-03-2026 10:31:27" in buf[]
  // see https://cplusplus.com/reference/cstdio/fscanf/
  int rv = sscanf(buf, "%2d%*[-]%2d%*[-]%4d %2d%*[:]%2d%*[:]%2d", &dd, &mm, &yyyy, &hh, &mi, &ss);

agreed sscanf() is very powerful!
another example scanning a string delimited by commas to extract received message "HELLO"

// To read strings not delimited by space characters, substitute a set of characters in brackets ([ ])
//for the s (string) type character.The corresponding input field is read up to the first character that
// does not appear in the bracketed character set.If the first character in the set is a caret(^),
// the effect is reversed : the input field is read up to the first character that does appear in the rest of the character set.
// see https://www.ibm.com/docs/en/i/7.3?topic=functions-scanf-read-data

void setup() {
Serial.begin(115200);
char str1[] = "+RCV=50,5,HELLO,-99,40 ";
int address = 0, length = 0, rSSI = 0, sNR = 0;
char data[20] = "";
if (sscanf(str1, "+RCV=%d,%d,%[^','],%d,%d", &address, &length, data, &rSSI, &sNR) == 5) {
  Serial.println("\nconverted OK");
  Serial.print("address ");
  Serial.println(address);
  Serial.print("length ");
  Serial.println(length);
  Serial.print("data ");
  Serial.println(data);
  Serial.print("rSSI ");
  Serial.println(rSSI);
  Serial.print("sNR ");
  Serial.println(sNR);

} else Serial.println("\nconversion failed");
}

void loop() {
}

serial monitor output

converted OK
address 50
length 5
data HELLO
rSSI -99
sNR 40