Why is my code not returning all of the serial data being given to it?

I am currently running a code on Matlab that feeds data to a microcontroller via serial connection. I set up a microcontroller to log the incoming data to an SD card so that I may see how it looks after being fed through. However, in the CSV file it oftentimes is splitting data into multiples entries, splicing multiple into one entry, or skipping a line entirely. I am assuming that this is a buffer issue in the serial connection, but I am unsure of how to solve this. I am fairly certain that it is not an issue on the Matlab side, though I could be wrong. I have narrowed it down to either being an input issue on the Arduino side (likely) or an output issue on the Matlab side (less likely). I'm pretty new to this so any help or information that would aid would be appreciated!

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

#include <SD.h>

#define chipSelect BUILTIN_SDCARD                       // Using built in chipselect on Teensy 3.5
File datalog;                                           // File object to be opened, written to, and closed
char filename[] = "TST00.csv";                          // File name as will be seen on SD card -- can have maximum of 99 files on SD card ('GLM00' -> 'GLM99')
bool sdActive = false;                                  // Boolean to check if there are any available filenames left to be used

int period = 10;
unsigned long time_now = 0;
int dataLogs = 0;

void setup() {

void loop() {
  String pass;
  time_now = millis();

  while (millis() < time_now + period) {     //wait approx. [period] ms

    if (Serial.available() > 0) {

      String data1 = Serial.readString();
      while (Serial.available()) Serial.read();
      while (Serial.read() >= 0) ; // flush the input buffer
      Serial.println(data1); // loopback of serial data back to computer



Yeah, my bad forgot about that. New to posting on the site.

I do not quite know if your code makes sense to me.

  1. There is a chance that String data1 = Serial.readString(); just reads one or two bytes.
  2. while (Serial.available()) Serial.read(); throws anything away that might be useful.
  3. while (Serial.read() >= 0) ; does the same as (2)

I suggest that you study Serial Input Basics - updated to get some ideas for your serial communication.

1 Like

Thanks for the resource! Also, weirdly enough the issue was worse before I added those lines, though I see what you're saying. I'll have to look into those for sure

Also check out my Arduino Software Solutions for various ways to read Strings and their pros and cons

Thanks, that link you sent was enough for me to figure it out! Turns out I needed start and end characters. The lack of these was causing my program to get confused.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.