Long serial input not working

Hello all. I got an issue that has been killing me for weeks. I'm not a noob, just really terrible at this. I am trying to put together a data logger for a serial device. I have limited control over what the serial device spits out, and my code works for short serial lines that come out of the device. However, when I get a long serial line (greater than 256 characters) it doesn't work. There is nothing I can do to make the line shorter, so I just have to figure out a way to deal with it. In my full code I am tokenizing the input and only saving parts of it.

To show everyone my issue, I came up with a small simulation. I set up two Arduinos (one Arduino Pro Mini and one Teensy 35) with the TX/RX from the Pro hooked to the RX/TX of the Teensy. The Pro Mini is simulating the serial device by sending out this line every three seconds:

$MUX1230,2,1,70,2,0002,0,1,1,1,60,240,0,109,1000,100,50,100,0,656,2CC,0,652,33E,0,624,0,0,624,0,0,624,0,0,624,0,0,1,2,1,2,1,2,1,2,1,2,1,2,26,145,0,5,276,1,1,0,0,0,0,443,451,0,0,0,0,-4,-2,-624,-624,-624,-624,13,22,0,0,0,0,4,4,0,0,0,0,563,20-12-2016 04:15:01 THIS IS THE ADDITIONAL CHARS

Here is the code I have running on the Teensy, which is going to be the data logger:

#define SERIAL_BUFFER_SIZE 512
const unsigned int MAX_INPUT = 512;                                 // how much serial data we expect before a newline
char inputLine [MAX_INPUT];                                         // where to store the string
byte inputPosition = 0;                                             // input position of the inputLine character array.
bool debug = 0;
//***************************************************
void setup() {
  delay(3000);                                                // Allow for Teensy startup
  Serial.begin(57600);
  Serial4.begin(57600);
  Serial.println("Logger is starting up");
}
//***************************************************

//***************************************************
void loop() {
  while (Serial4.available () > 0) {                          // If the MUX sends something out, process it
    processByte(Serial4.read ());
  }
}
//***************************************************

//***************************************************
void processByte (const byte c) {                              // Used to process all bytes coming in from the MUX
  switch (c) {
    case 10:                                                   // If it is an end of text because we see the 'new line' character from the MUX
      inputLine [inputPosition] = 0;                           // Terminating null byte  '/0'
      processInput(inputLine);
      Serial.println("");

      inputPosition = 0;                                       // Reset buffer for next time
      break;

    default:
      if (inputPosition < (MAX_INPUT - 1)) {                   // If we haven't reached the max input size for the array
        inputLine [inputPosition++] = c;                       // Keep adding if not full ... allow for terminating null byte
        if (debug)
          Serial.write(c);                                     // If debug true, write each character as it comes in
      }
      else {                                                   // In case of an overflow, clear the array and reset the pointer
        memset(inputLine, 0, sizeof(MAX_INPUT - 1));
        inputPosition = 0;
      }
  }
}
//***************************************************

//***************************************************
void processInput(const char * c) {
  if (!debug)
    Serial.write(inputLine);
}
//***************************************************

When I have debug 0, this is what I see on the serial of the Teensy:

Logger is starting up
THIS IS THE ADDITIONAL CHARS

It is missing the first 255 characters. It is like there is a buffer that filled up, and at the 256th character the whole buffer was cleared and now I am only seeing the rest of the characters.

When I have debug 1 (it prints each character as it comes in the serial port) then I see this:

Logger is starting up
$MUX1230,2,1,70,2,0002,0,1,1,1,60,240,0,109,1000,100,50,100,0,656,2CC,0,652,33E,0,624,0,0,624,0,0,624,0,0,624,0,0,1,2,1,2,1,2,1,2,1,2,1,2,26,145,0,5,276,1,1,0,0,0,0,443,451,0,0,0,0,-4,-2,-624,-624,-624,-624,13,22,0,0,0,0,4,4,0,0,0,0,563,20-12-2016 04:15:01 THIS IS THE ADDITIONAL CHARS

I have struggled with this for weeks now, and I have no clue what I am doing wrong. I tried the #define SERIAL_BUFFER_SIZE 512, but obviously that doesn't help.

I've tried bumping the buffer size up to 512 for both RX_BUFFER_SIZE and TX_BUFFER_SIZE and that doesn't seem to help either.

Any suggestions?

Just for completeness, here is what is running on the Arduino Pro Mini:

char str[] = "$MUX1230,2,1,70,2,0002,0,1,1,1,60,240,0,109,1000,100,50,100,0,656,2CC,0,652,33E,0,624,0,0,624,0,0,624,0,0,624,0,0,1,2,1,2,1,2,1,2,1,2,1,2,26,145,0,5,276,1,1,0,0,0,0,443,451,0,0,0,0,-4,-2,-624,-624,-624,-624,13,22,0,0,0,0,4,4,0,0,0,0,563,20-12-2016 04:15:01 THIS IS THE ADDITIONAL CHARS";
int led = 13;

void setup() {
  Serial.begin(57600);
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on 
  Serial.println(str);
  digitalWrite(led, LOW);    // turn the LED off 
  delay(3000);
}

You hold the buffer index in 'byte inputPosition'

This variable will wrap with the 257th character to zero, basically clearing out the first 256 Bytes.

Try using int inputPosition.

YOU ARE AWESOME!

I just tried it in my test code and that fixed it! I am going to make that change in my original code and report back. I have to change some wiring around, but I think you just solved weeks of heartache.

Thank you!

Never mind, it was quite easy. :wink:

That was my problem. Have the whole thing back together and now it actually works. Thanks again.