Serial2.println issue

Not sure if this is only on my device… wondering if others have seen this:

If I have a UART connected to Serial2:
Serial2.println(“012345678”);
Serial2.println(“abc”);

The UART sees buffers of 4 chars as follows:
0123
4567
8[CR][LF]a
bc[CR][LF]

Note that the char “a” is tacked on after a Carriage return and line feed.

Anyone else see this?

Hi @hk101

I am having trouble seeing what is wrong here–can you explain what the problem is? The serial ports are buffered so the timing of output may not be related to the println that you wrote, but it looks like the output is correct here.

I would expect to see the ASCII chars 0 to 8, followed , then abc followed by and that looks like what you got–16 characters total.

Does your receiving UART buffer four characters?

@bko,
Thank you for your query and interest,

I have a Teensy 3 connected to the Photon on Serial 2.
The Teensy 3 appears to be receiveing 4 chars at a time from Photon.
So If I have a serial terminal echoing the characters from Photon,
I see the data correctly as 012345678 followed by abc on the next line.
However if I try to parse the chars comming from Photon to T3 and break parsing on Linefeed, then the next char “a” that is packed into the 4 chars from photon is lost. So I see:

012345678
bc

May be it is my code on T3 (arduino IDE)? Here is the parser code:

T3 Code:

    #define POUART Serial2
    unsigned long baud = 115200;
    #define POT3BUFSZ 80
    
    byte POT3buf[POT3BUFSZ];  
    static boolean New_PO_Msg_Received = false;
    static int New_PO_Msg_Sz = 0;
    void setup() {
      Serial.begin(baud);  // for T3 to PO connection
      POUART.begin(baud); // use this for T3 PO interaction
    }
    
    void PO_read()
    {
        int rd, n;
        byte inchar[POT3BUFSZ];
      // check if any data has arrived on the hardware serial port
      rd = POUART.available();
      if (rd > 0) {
          // read data from the hardware serial port
          n = POUART.readBytes((char *)inchar, rd);
          int Cctr=0;
          for(Cctr=0;Cctr<n;Cctr++) {
            if(inchar[Cctr] != 10 ) 
            { //  if in comming char not LF
              POT3buf[Cctr+New_PO_Msg_Sz] = inchar[Cctr];
            } else { // update size 
               New_PO_Msg_Sz += Cctr;
               New_PO_Msg_Received = true; 
            }  
          } // for new n of chars recd
          if(!New_PO_Msg_Received) {
            New_PO_Msg_Sz += n;
          }
      }
    }
    void loop()
    {
      PO_read();
      if(New_PO_Msg_Received) {
        //Serial.print("New msg size: ");
        //Serial.println(New_PO_Msg_Sz);
      for(int c=0;c<New_PO_Msg_Sz;c++) {
        Serial.write(POT3buf[c]);
      }
      Serial.println("");
      New_PO_Msg_Received = false;
      for(int i=0;i<POT3BUFSZ;i++) {POT3buf[i] = '0'; }
      New_PO_Msg_Sz=0;
     }
     
    }


Photon code:

    #define UART Serial2
    
    void setup()
    {
      UART.begin(115200);
    }
    
    void loop()
    {
        UART.println("012345678");
        UART.println("abc");
        delay(5000); // delay 5 secs
    }

Hi @hk101

It looks to me like your T3 code reads all available chars from the POUART and then scans looking for a but then it does not handle any thing after the so it just gets discarded.

Why don’t you use a while loop reading one char a time looking for and not reading past the terminator.

Another approach would be to use the peek() method thart Arduino defines for streams. This lets you see what the next char is without removing it from the buffer.

1 Like

@bko,
Thanks for the suggestions… edited to remove previous post.

I think there is still an issue. If I insert a delay after the first Serial
print on the photon, the photon appears to stream the output correctly. Else it fails…see output below

Here is the updated code:
T3:

#define POUART Serial2
unsigned long baud = 115200;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete

void setup() {
  Serial.begin(baud);  // for T3 to PO connection
  POUART.begin(baud); // use this for T3 PO interaction
}

void POUART_read() { 
  while (POUART.available()) {
   // get the new byte:
   char inChar = (char)POUART.read(); 
   if (inChar == 13) {
     stringComplete = true;
   } else {
    inputString += inChar;
   }
  }
}

void loop()
{
  POUART_read();
  if(stringComplete) {
    Serial.print("Got input: ");
    Serial.println(inputString);
    if (inputString == "abc"){
      Serial.println("Got String: ABC");
    }
    stringComplete = false;
    inputString = "";
  }
  
}

Photon:

#define UART Serial2

void setup()
{
  UART.begin(115200);
  UART.println("With Delay inserted after first print");
}

void loop()
{
    UART.println("012345678");
    delay(10); // This delay after first print appears to help..
    UART.println("abc");
    delay(5000); // delay 5 secs
}

Output: (Note the first Odd char ÿ <— Unprintable Photon buffer data?)

ÿNo Delay inserted after first print
01
Got input: 2345678
Got input: 
abc

Got input: 012345678
a
Got input: bc

Got input: 012345678
a
Got input: bc

Got input: 012345678
a
Got input: bc

Got input: 012345678
a
Got input: bc

Got input: 

ÿWith Delay inserted after first print

Got input: 012345678

Got input: abc
Got String: ABC
Got input: 
012345678

Got input: abc
Got String: ABC
Got input: 
012345678

Got input: abc
Got String: ABC
Got input: 
012345678

Got input: abc
Got String: ABC
Got input: 
012345678

Hi @hk101

I think the delay() is masking problems on the other side since POUART.available() will return false. You should EXIT the while loop when you find a since that is your terminating char. Then when you come back the next time, all the rest of the data should be there.

The funny character is probably due to an off-by-one or reading off the end of an array error but I don’t see it off hand.

void POUART_read() { 
  while (POUART.available() && !stringComplete) {
   // get the new byte:
   char inChar = (char)POUART.read(); 
   if (inChar == 13) {
     stringComplete = true;
   } else {
    inputString += inChar;
   }
  }
}
1 Like

@bko,

OK, Thanks, the following appears to work consistently ( so far)
without any delay on photon: ( Thanks for your suggestions)

char inputString[100]; // a string to hold incoming data
static int iPtr =0;    
void POUART_read() { 
      while (POUART.available()) {
       char inChar = (char)POUART.read();
       if (inChar == 13) {
         stringComplete = true;
         return;
       } else {
        if(inChar != 10) { inputString[iPtr++] = inChar; }
       }
      }
    }

The inChar != 10 is necessary because some how
either T3:
is inserting the LF that follows CR (13 dec)
or Photon:
appears to send a LF after CR which
seems to have been shoved into the left end of input string…
Anyway the above appears to be working so far…

2 Likes

println() does that on purpose. If you don't want that and only [CR] use print("12345\r");

For reading until you find a terminator you could readStringUntil() or readBytesUntil() (these are inherited from Stream)

1 Like