Newline Character vs Line Feed

Even though I solved my problem with a workaround, I wanted to ask about this to satisfy my own curiosity. I have an Arduino based weather station that sends in (RF) a string of comma separated data, terminated with a '\n' character. I use a Serial.print("\n"); in my sketch to send it. It is received by a Linux board running a Python program that parses the data, etc. All of that has been working fine for almost 2 years.

Ok, so, I now want to also capture the RF data stream on a separate Arduino Uno connected to a Nixie Tube display. The issue that surfaced was my sketch wouldn't recognize my check for the termination character '\n'.

Finally, I captured a data stream and converted it to hex. Right away, I noticed the termination character was actually a line feed or 0xA. So, I changed my check from '\n' to 0xA and all is good.

I realize a Serial.println() sends a \r\n and a Serial.print() sends a \n. But my question is why would checking for '\n' versus 0xA make a difference? I'm basically sending a '\n' from one Arduino to another Arduino. Shouldn't I be able to just check for '\n' ?

m207365:
I realize a Serial.println() sends a \r\n and a Serial.print() sends a \n.

The print usually does not send a termination character. This is only because you send it manually.

m207365:
But my question is why would checking for '\n' versus 0xA make a difference? I'm basically sending a '\n' from one Arduino to another Arduino. Shouldn't I be able to just check for '\n' ?

Could post the code for receiving and checking the '\n'. Because it shouldn't make a difference, as you said.

Serial.print() does not send a ‘\n’; you did so by using Serial.print("\n").

Why it did not work, no idea. You will have to post the code of both the transmitter and the receiver.

Actually, as I mentioned in my 1st paragraph, I am sending my termination character with a Serial.print("\n"); statement. I'll try to crop and post some pertinent lines of code. Here's the Arduino code in the weather station that sends the data. It's been working fine for close to 2 years. The data is received and parsed by a Python program running under Linux.

//Serial.println(); //Serial Print String format Modified by M. ALLEN
    Serial.print("~,"); 
    Serial.print(winddir);
    Serial.print(",");
    Serial.print(windspeedmph, 1);
    Serial.print(",");
    Serial.print(windgustmph, 1);
    Serial.print(",");
    Serial.print(windgustdir);
    Serial.print(",");
    Serial.print(windspdmph_avg2m, 1);
    Serial.print(",");
    Serial.print(winddir_avg2m);
    Serial.print(",");
    Serial.print(windgustmph_10m, 1);
    Serial.print(",");
    Serial.print(windgustdir_10m);
    Serial.print(",");
    Serial.print(humidity, 1);
    Serial.print(",");
    Serial.print(dewptf, 1); //Added by M. ALLEN
    Serial.print(",");
    Serial.print(tempf, 1); //Off-board MCP9808 temp
    Serial.print(",");
    Serial.print(rainin, 2);
    Serial.print(",");
    Serial.print(dailyrainin, 2);
    Serial.print(",");
    Serial.print(baroin, 2); //Added by M. ALLEN
    Serial.print(",");
    Serial.print(batt_lvl, 2);
    Serial.print(",");
    Serial.print(light_lvl, 2);
    Serial.print(",");
    Serial.print(tempP, 1); //Added by M. ALLEN pressure sensor temp used to control heater
    Serial.print(",");
    Serial.print(tempA, 1); //Added by M. ALLEN AM2315 temp for comparison
    Serial.print("\n");

Here's my test code running in the new Arduino that is receiving the same RF data:

char ch;
String inData;
char startCh = '~'; 
char endCh = 0xA;
boolean storeData = false; 
int delimPos;
String temp;
int i;

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

void loop() {
  while(Serial.available()>0){
    ch = Serial.read();    
    if(ch == startCh){
        storeData = true;
    }
    if(storeData){
      if(ch == endCh){
        do{          
          delimPos = inData.indexOf(',');
          if(delimPos != -1){            
            //Serial.println(inData.substring(0,delimPos)); 
            
            if(i == 11){
              temp = inData.substring(0,delimPos);           
            }
            i++;
            
            inData = inData.substring(delimPos +1, inData.length());            
          }
          else{
            if(inData.length() > 0){
              //Serial.println(inData);
            }
          }
        }
        while(delimPos >= 0);{
          delay(3000);
        }
        Serial.print("Temperature: ");
        Serial.println(temp);
        i = 0;       
        storeData = false;
        inData = "";
      }
      else{
        inData = inData + ch;
      }
    }
  }
}

If I change char endCh = 0xA; to char endCh = '\n'; my program doesn't recognize the termination code. I'm just trying to understand why it would make a difference.

I just tried '\n' again, and today it's working fine. I must have tried it 2 dozen times yesterday. Beats me. Anyway, thanks for the responses!

I found this while trying to solve a similar issue I had.
I don’t have an answer exactly but I’d say that looking at ascii code in the messages both 10 and 13 were present, things didn’t work for me until id got them both right for my application, different software/firmware seem to look at different conditions for ‘end of message’ 0x0A/0x0D. (10/13)
Doesn’t help on many sites/conversion apps the difference between linefeed/carriage return/new line are as clear as mud.
For me, in the end things worked much better when I gave up trying to mess with the messages and just used Serial.println to terminate my strings. Whatever I was missing is dealt with println weather it’s from Arduino to console or to hardware device. Hope that helps anyone else who finds this!

Mattty5:

carriage return (CR) = 13 = 0x0D
line feed (LF) = 10 = 0x0A

Windows typically uses CR+LF line endings (in that order) while *nix systems use just LF line endings. "New line", in my understanding, just means whatever sequence is used to end a line.

m207365:
I just tried '\n' again, and today it's working fine. I must have tried it 2 dozen times yesterday. Beats me. Anyway, thanks for the responses!

Did you make the mistake of using "\n" instead of '\n' ?

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