[SOLVED] Arduino Uno communication with external software

That’s the code:

#include "RTClib.h"
#include <SoftwareSerial.h>

SoftwareSerial portOne(10, 11);
char inByte;
const byte numChars = 45;
char receivedChars[numChars]; // an array to store the received data
char rc;
boolean newData = false;
static const unsigned long REFRESH_INTERVAL = 1000; // ms
static unsigned long lastRefreshTime = 0;
char ostiah;
RTC_DS1307 rtc;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif
  portOne.begin(9600);
  if (! rtc.begin()) {
   Serial.println("Couldn't find RTC");
    Serial.flush();
  abort();
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void recvWithEndMarker() {
 static byte ndx = 0;
char endMarker = '\n';


if (portOne.available() > 0) {
  
  while (portOne.available() > 0 && newData == false) {
    rc = portOne.read();
   
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
      //ostiah="terminal";
    }
  }
  } 
}
void loop() {
  // put your main code here, to run repeatedly:
  recvWithEndMarker();
  if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    lastRefreshTime += REFRESH_INTERVAL;
    getdata();
    }
  }
  
void getdata() {
  DateTime now = rtc.now();
  float a=analogRead(A0)*5/1023;
  if(newData==true){
  Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
  Serial.print("\t");
  Serial.print(a);
  Serial.print("\t");
  Serial.write(receivedChars);
  //Serial.print(ostiah);
  Serial.println();
  newData=false;
  }

  

}

I am not understanding why you need the recvWithEndMarker() function.

  1. Read A0.
  2. Write out serial port.
    repeat

.

I think this line

Serial.write(receivedChars);

should be

Serial.print(receivedChars);

...R

ieee488:
I am not understanding why you need the recvWithEndMarker() function.

The OP is collecting data from an external device over a serial connection.

...R

Robin2:
The OP is collecting data from an external device over a serial connection.

...R

Thanks.
Personally, I would get all the data from the external device then read A0, and then do the serial write. No need for newData flag.

ieee488:
I am not understanding why you need the recvWithEndMarker() function.

  1. Read A0.
  2. Write out serial port.
    repeat

.

In this way I will have one char per row instead al the data received in a single row.
Could be a solution associated with a post-processing in Rstudio, but I prefer the code proposed by Robin2 because is more structured.

By the way I realised that the mismatch is due to serial buffer; with the code below I did a great progress, now data are almost consistent. I don’t know why each 24 rows I get a mismatch.

I’ve included a buffer cleaning and a starting chart check:

#include "RTClib.h"
#include <SoftwareSerial.h>

SoftwareSerial portOne(10, 11);
char inByte;
const byte numChars = 45;
char receivedChars[numChars]; // an array to store the received data
char rc;
boolean newData = false;
static const unsigned long REFRESH_INTERVAL =1000; // ms
static unsigned long lastRefreshTime = 0;
char ostiah;
RTC_DS1307 rtc;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif
  portOne.begin(9600);
  if (! rtc.begin()) {
   Serial.println("Couldn't find RTC");
    Serial.flush();
  abort();
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char startMarker = 'H';
  static boolean recvInProgress = false;

  if (portOne.available() > 0) {
  
    while (portOne.available() > 0 && newData == false) {
      rc = portOne.read();
      if (recvInProgress == true) {
          //Serial.println(rc);
          if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
          if (ndx >= numChars) {
            ndx = numChars - 1;
          }
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
        }
       } else if(rc==startMarker){
          recvInProgress=true;
       }
    }
  } 
}


void loop() {
  // put your main code here, to run repeatedly:
  recvWithEndMarker();
  if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    lastRefreshTime += REFRESH_INTERVAL;
    getdata();
    }
  }
  
void getdata() {
  DateTime now = rtc.now();
  float a=analogRead(A0)*5/1023;
  if(newData==true){
    Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
    Serial.print("\t");
    Serial.print(a);
    Serial.print("\t");
    Serial.write(receivedChars);
    Serial.println();
    newData=false;
    while (portOne.available() > 0) {
      portOne.read();
    }
  }
}

the output on serial monitor:
2020-07-11T17:47:12 1.00 H1470 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:13 1.00 H1475 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:14 1.00 H1480 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:15 1.00 H1485 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:16 1.00 H1490 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:17 0.00 H1495 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:18 1.00 H1500 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:19 1.00 H1505 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:20 1.00 H1510 0000 0961 0965 +32 0000 10 01 004 0000
2020-07-11T17:47:21 1.00 H1515 0000 0961 0965 +32 0000 10 01 004 0000
2020-07-11T17:47:22 1.00 H1520 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:23 1.00 H1525 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:24 1.00 H1530 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:25 0.00 H1535 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:26 0.00 H1540 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:27 0.00 H1545 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:28 0.00 H1550 0000 0961 0965 +32 0000 10 01 004 0000
2020-07-11T17:47:29 0.00 H1555 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:30 1.00 H1560 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:31 1.00 H1565 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:32 1.00 H1570 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:33 1.00 H1575 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:34 1.00 H1580 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:35 1.00 H1585 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:36 1.00 H1590 0000 0962 0966 +32 0000 10 01 004 0000
2020-07-11T17:47:37 1.00 0 01 004 0000

2020-07-11T17:47:38 1.00 H1601 0000 0962 0966 +32 0000 10 01 004 0000

What happens if you try this

void loop() {
  // put your main code here, to run repeatedly:
  recvWithEndMarker();
  if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    lastRefreshTime += REFRESH_INTERVAL;
    //~ getdata();
    Serial.println(receivedChars);
    while (portOne.available() > 0) {
      portOne.read();
    }
    newData = false;
  }
}

Also, you have increased the buffer size to 45 - is that a few characters bigger than the biggest message you will ever receive? Unless you are short of memory bump it up to 64.

...R

paolo_cristoforetti:
In this way I will have one char per row instead al the data received in a single row.

You do realize that how you write out the bytes is totally under your control, right ?

.

ieee488:
You do realize that how you write out the bytes is totally under your control, right ?

.

I do, it's just what I'm trying todo..simply managing how I read, that, in my modest opinion, is the better way..

Robin2:
What happens if you try this

void loop() {

// put your main code here, to run repeatedly:
  recvWithEndMarker();
  if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    lastRefreshTime += REFRESH_INTERVAL;
    //~ getdata();
    Serial.println(receivedChars);
    while (portOne.available() > 0) {
      portOne.read();
    }
    newData = false;
  }
}




Also, you have increased the buffer size to 45 - is that a few characters bigger than the biggest message you will ever receive? Unless you are short of memory bump it up to 64.

...R

whit the new loop I get this output:
H2368 0000 0967 0970 +31 0000 10 01 004 0000
H2373 0000 0967 0970 +31 0000 10 01 004 0000
H2378 0000 0967 0970 +31 0000 10 01 004 0000
H2383 0000 0967 0970 +31 0000 10 01 004 0000
H2388 0000 0967 0970 +31 0000 10 01 004 0000
H2393 0000 0967 0970 +31 0000 10 01 004 0000
H2398 0000 0967 0970 +31 0000 10 01 004 0000
H2403 0000 0967 0970 +31 0000 10 01 004 0000
H2408 0000 0967 0970 +31 0000 10 01 004 0000
H2413 0000 0967 0970 +31 0000 10 01 004 0000
H2418 0000 0967 0970 +31 0000 10 01 004 0000
H2423 0000 0967 0970 +31 0000 10 01 004 0000
H2428 0000 0967 0970 +31 0000 10 01 004 0000
H2433 0000 0967 0970 +31 0000 10 01 004 0000
H2438 0000 0967 0970 +31 0000 10 01 004 0000
H2443 0000 0967 0970 +31 0000 10 01 004 0000
H2448 0000 0967 0970 +31 0000 10 01 004 0000
H2453 0000 0967 0970 +31 0000 10 01 004 0000
H2458 0000 0967 0970 +31 0000 10 01 004 0000
H2463 0000 0967 0970 +31 0000 10 01 004 0000
H2468 0000 0967 0970 +31 0000 10 01 004 0000
H2473 0000 0967 0970 +31 0000 10 01 004 0000
H2478 0000 0966 0969 +31 0000 10 01 004 0000
H2483 0000 0967 0970 +31 0000 10 01 004 0000
004 0000

004 0000

0

H2504 0000 0967 0970 +31 0000 10 01 004 0000
H2509 0000 0967 0970 +31 0000 10 01 004 0000
H2514 0000 0967 0970 +31 0000 10 01 004 0000
H2519 0000 0967 0970 +31 0000 10 01 004 0000
H2524 0000 0967 0970 +31 0000 10 01 004 0000
H2529 0000 0967 0970 +31 0000 10 01 004 0000
H2534 0000 0967 0970 +31 0000 10 01 004 0000
H2539 0000 0967 0970 +31 0000 10 01 004 0000
H2544 0000 0967 0970 +31 0000 10 01 004 0000

and this is the output with buffer with 64 position
H2368 0000 0967 0970 +31 0000 10 01 004 0000
H2373 0000 0967 0970 +31 0000 10 01 004 0000
H2378 0000 0967 0970 +31 0000 10 01 004 0000
H2383 0000 0967 0970 +31 0000 10 01 004 0000
H2388 0000 0967 0970 +31 0000 10 01 004 0000
H2393 0000 0967 0970 +31 0000 10 01 004 0000
H2398 0000 0967 0970 +31 0000 10 01 004 0000
H2403 0000 0967 0970 +31 0000 10 01 004 0000
H2408 0000 0967 0970 +31 0000 10 01 004 0000
H2413 0000 0967 0970 +31 0000 10 01 004 0000
H2418 0000 0967 0970 +31 0000 10 01 004 0000
H2423 0000 0967 0970 +31 0000 10 01 004 0000
H2428 0000 0967 0970 +31 0000 10 01 004 0000
H2433 0000 0967 0970 +31 0000 10 01 004 0000
H2438 0000 0967 0970 +31 0000 10 01 004 0000
H2443 0000 0967 0970 +31 0000 10 01 004 0000
H2448 0000 0967 0970 +31 0000 10 01 004 0000
H2453 0000 0967 0970 +31 0000 10 01 004 0000
H2458 0000 0967 0970 +31 0000 10 01 004 0000
H2463 0000 0967 0970 +31 0000 10 01 004 0000
H2468 0000 0967 0970 +31 0000 10 01 004 0000
H2473 0000 0967 0970 +31 0000 10 01 004 0000
H2478 0000 0966 0969 +31 0000 10 01 004 0000
H2483 0000 0967 0970 +31 0000 10 01 004 0000
004 0000

004 0000

0

H2504 0000 0967 0970 +31 0000 10 01 004 0000
H2509 0000 0967 0970 +31 0000 10 01 004 0000
H2514 0000 0967 0970 +31 0000 10 01 004 0000
H2519 0000 0967 0970 +31 0000 10 01 004 0000
H2524 0000 0967 0970 +31 0000 10 01 004 0000
H2529 0000 0967 0970 +31 0000 10 01 004 0000
H2534 0000 0967 0970 +31 0000 10 01 004 0000
H2539 0000 0967 0970 +31 0000 10 01 004 0000
H2544 0000 0967 0970 +31 0000 10 01 004 0000

Could be possible that I loose some data when it restart the wile loop after condition startMarker==true? Whait if I put this condition at the beginnig of the while loop?

paolo_cristoforetti:
Could be possible that I loose some data when it restart the wile loop after condition startMarker==true? Whait if I put this condition at the beginnig of the while loop?

Don't mess with the code in recvWithEndMarker(). It has worked for lots of other people. Just use it EXACTLY as it is in my Tutorial.

...R

Robin2:
Don't mess with the code in recvWithEndMarker(). It has worked for lots of other people. Just use it EXACTLY as it is in my Tutorial.

...R

And at last it works for me too in normal condition, but not with 1 second between outputs. Mine was only an ipothesys to try to resolve my particular problem..sorry

paolo_cristoforetti:
And at last it works for me too in normal condition, but not with 1 second between outputs.

Please post the program that works “normally” and also the one with the 1 second intervals.

…R

First:

#include "RTClib.h"
#include <SoftwareSerial.h>

SoftwareSerial portOne(10, 11);
char inByte;
const byte numChars = 45;
char receivedChars[numChars]; // an array to store the received data
char rc;
boolean newData = false;
static const unsigned long REFRESH_INTERVAL =1000; // ms
static unsigned long lastRefreshTime = 0;
char ostiah;
RTC_DS1307 rtc;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif
  portOne.begin(9600);
  if (! rtc.begin()) {
   Serial.println("Couldn't find RTC");
    Serial.flush();
  abort();
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char startMarker = 'H';
  static boolean recvInProgress = false;

  if (portOne.available() > 0) {
  
    while (portOne.available() > 0 && newData == false) {
      rc = portOne.read();
      if (recvInProgress == true) {
          //Serial.println(rc);
          if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
          if (ndx >= numChars) {
            ndx = numChars - 1;
          }
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
        }
       } else if(rc==startMarker){
          recvInProgress=true;
       }
    }
  } 
}


void loop() {
  // put your main code here, to run repeatedly:
  recvWithEndMarker();
  //if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    //lastRefreshTime += REFRESH_INTERVAL;
    getdata();
    //}
  }
  
void getdata() {
  DateTime now = rtc.now();
  float a=analogRead(A0)*5/1023;
  if(newData==true){
    Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
    Serial.print("\t");
    Serial.print(a)
    Serial.print("\t");
    Serial.write(receivedChars);
    Serial.println();
    newData=false;
    while (portOne.available() > 0) {
      portOne.read();
    }
  }
}

Second:

#include "RTClib.h"
#include <SoftwareSerial.h>

SoftwareSerial portOne(10, 11);
char inByte;
const byte numChars = 45;
char receivedChars[numChars]; // an array to store the received data
char rc;
boolean newData = false;
static const unsigned long REFRESH_INTERVAL =1000; // ms
static unsigned long lastRefreshTime = 0;
char ostiah;
RTC_DS1307 rtc;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif
  portOne.begin(9600);
  if (! rtc.begin()) {
   Serial.println("Couldn't find RTC");
    Serial.flush();
  abort();
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char startMarker = 'H';
  static boolean recvInProgress = false;

  if (portOne.available() > 0) {
  
    while (portOne.available() > 0 && newData == false) {
      rc = portOne.read();
      if (recvInProgress == true) {
          //Serial.println(rc);
          if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
          if (ndx >= numChars) {
            ndx = numChars - 1;
          }
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
        }
       } else if(rc==startMarker){
          recvInProgress=true;
       }
    }
  } 
}


void loop() {
  // put your main code here, to run repeatedly:
  recvWithEndMarker();
  if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    lastRefreshTime += REFRESH_INTERVAL;
    getdata();
    }
  }
  
void getdata() {
  DateTime now = rtc.now();
  float a=analogRead(A0)*5/1023;
  if(newData==true){
    Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
    Serial.print("\t");
    Serial.print(a);
    Serial.print("\t");
    Serial.write(receivedChars);
    Serial.println();
    newData=false;
    while (portOne.available() > 0) {
      portOne.read();
    }
  }
}

Thanks. It is nice to see that there is almost complete commonality between the programs.

In the output in Reply #25 you said that a problem arises with every 24th row. I presume that means every 24 seconds.

Have you checked whether there is a problem in "normal" program after about 24 seconds - because that probably means many more than 24 rows.

It would be useful to know how many rows per second (or per minute) the "normal" program produces.

It would also be very useful to know exactly what data comes from receivedChars without any of the other stuff that you print.

...R

Robin2:
Thanks. It is nice to see that there is almost complete commonality between the programs.

In the output in Reply #25 you said that a problem arises with every 24th row. I presume that means every 24 seconds.

Correct, each rows is 1 second left.

Have you checked whether there is a problem in “normal” program after about 24 seconds - because that probably means many more than 24 rows.

It would be useful to know how many rows per second (or per minute) the “normal” program produces.

It would also be very useful to know exactly what data comes from receivedChars without any of the other stuff that you print.

…R

In normal condition it prints new data 5 times per second (each 0.2 sec, that is the output rate of the external sensor), so, eventually, mismatching will happen around 24*5=120 rows of output. It doesn’t happen, received data are really clean, see attachment if you want.

This is how the output is with only the serial data:
H1627 0000 0960 0963 +29 0000 10 01 004 0000
There are 10 fields separated with tab firs field is an identifier other fields are data end of line is cr ln

Thanks

output_without_delay.txt (74.4 KB)

Without having your equipment for testing I don't know what's happening.

Do you get the error every 24th row when you just output the content of receivedChars to the Serial Monitor once per second?

...R

Yes each 24 rows.

Maybe could be interesting this output done with 0.2s of interval

10:07:50.443 -> H1079 0000 0967 0970 +31 0000 10 01 004 0000
10:07:50.643 -> H1080 0000 0967 0970 +31 0000 10 01 004 0000
10:07:50.843 -> H1081 0000 0967 0970 +31 0000 10 01 004 0000
10:07:51.043 -> 0967 0970 +31 0000 10 01 004 0000

10:07:51.243 -> 0967 0970 +31 0000 10 01 004 0000

10:07:51.443 -> 967 0970 +31 0000 10 01 004 0000

10:07:51.642 -> 0970 +31 0000 10 01 004 0000

10:07:51.842 -> 0970 +31 0000 10 01 004 0000

10:07:52.042 -> 970 +31 0000 10 01 004 0000

10:07:52.242 -> +31 0000 10 01 004 0000

10:07:52.442 -> +31 0000 10 01 004 0000

10:07:52.642 -> 31 0000 10 01 004 0000

10:07:52.842 -> 0000 10 01 004 0000

10:07:53.042 -> 000 10 01 004 0000

10:07:53.242 -> 00 10 01 004 0000

10:07:53.442 -> 10 01 004 0000

10:07:53.642 -> 0 01 004 0000

10:07:53.842 -> 01 004 0000

10:07:54.042 -> 1 004 0000

10:07:54.242 -> 004 0000

10:07:54.442 -> 004 0000

10:07:54.642 -> 04 0000

10:07:54.842 -> 0000

10:07:55.042 -> 0000

10:07:55.242 -> 000

10:07:55.442 ->

10:07:55.642 ->

10:07:55.842 ->
10:07:56.242 -> H1108 0000 0967 0970 +31 0000 10 01 004 0000
10:07:56.642 -> H1110 0000 0967 0970 +31 0000 10 01 004 0000

When the proble rise it starts to loose one char each iteration...

The output in Reply #36 include the time and analogRead() data. Just produce a version of the program that does nothing except receive and print receivedChars so we find out if that is where the problem lies.

And please confirm that the output on Reply #36 and Reply #29 is from the Arduino Serial Monitor. If that output was from Rstudio then please produce the new output on the Serial Monitor only.

If I get time later I may set up a pair of Arduinos so one sends the message to the other 5 times per second and see what results I get.

...R

I find this solution:

//#include "RTClib.h"
#include <SoftwareSerial.h>

SoftwareSerial portOne(10, 11);
char inByte;
const byte numChars = 45;
char receivedChars[numChars]; // an array to store the received data
char receivedChars_complete[numChars]; 
char rc;
boolean newData = false;
static const unsigned long REFRESH_INTERVAL =1000; // ms
static unsigned long lastRefreshTime = 0;
char ostiah;
//RTC_DS1307 rtc;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif
  portOne.begin(9600);
 // if (! rtc.begin()) {
 //  Serial.println("Couldn't find RTC");
 //   Serial.flush();
 //abort();
 // }
 // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char startMarker = 'H';
  static boolean recvInProgress = false;

  if (portOne.available() > 0) {
  
    while (portOne.available() > 0 && newData == false) {
      rc = portOne.read();
      if (recvInProgress == true) {
          //Serial.println(rc);
          if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
          if (ndx >= numChars) {
            ndx = numChars - 1;
          }
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
        }
       } else if(rc==startMarker){
          recvInProgress=true;
       }
    }
  } 
}


void loop() {
// put your main code here, to run repeatedly:    +

  recvWithEndMarker();
  if(newData==true){
    for (int i=0; i<=45;i++){
      receivedChars_complete[i]=receivedChars[i];
      newData=false;
      while (portOne.available() > 0) {
        portOne.read();
      }
    } 
  }
  if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
    lastRefreshTime += REFRESH_INTERVAL;
    getdata();
  }
}
  
void getdata() {
//DateTime now = rtc.now();
  float a=analogRead(A0)*5/1023;
  //    Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
  //Serial.print("\t");
  Serial.print(a);
  Serial.print("\t");
  Serial.print(receivedChars_complete);
  Serial.println();
}

I store the receivedChars in receivedChars_complete, clean the buffer and reset the variable NewData each iteration, now matter is the refreshtime. It seems it does the job. I suspect that in my previous code there was problem with the buffer. What do you think?