How to check 'CR' in the char from Serial.read function?

I want use a serial port to read data from a GPS device, the data message spilitted to two part by CR and LF symbol, and I only need front part of messge, so I'm trying detect 'CR' symbol and cut follow part. I write a little test program to detect 'CR' from serial.read function, but the problem is, If I use these statements, it dos not work, the program allways output ""Test....CR received." text (Boolean allway true).

If I use other symbol like as 'a', it does work, I have tried use '13' replaced '\r' but still failure. How can I do?

void lockstatus()

{
  if (Serial.available())
  {
     char ch=Serial.read();
     if (ch=='\r')
        {
         lcd.setCursor(0,1);
         lcd.println("Test....CR received.");
         }
   }
}

Shouldn't serial be written as Serial? starting with a capital letter

Sorry for my mistake in post, in my program it's 'Serial'.

I just tested this:

void loop() {
//Add your repeated code here
	if (Serial.available()) {
		char ch = Serial.read();
		if (ch == '\r') {
			Serial.println("Test....CR received.");
		}
	}
}

Works fine.
But how do you determine it does not work?

ahhui:
Sorry for my mistake in post, in my program it's 'Serial'.

How about copying and pasting your program? In code tags.

Read this before posting a programming question

nicoverduin, thanks for your help. Whatever I sent any ASCII symbol from my serial monitor, the program will allways output, it should be output only at 'CR' symbol receieved.

Show all your code please.

Whatever I sent any ASCII symbol from my serial monitor, the program will allways output, it should be output only at 'CR' symbol receieved.

What setting are you using for the line ending that the serial monitor appends?

If you have it append a CR or CR/LF, then you will see the message about receiving a CR every time you press the send button or hit enter.

If not, you should NEVER see it, as there is then no way for you to send a CR.

PaulS:

Whatever I sent any ASCII symbol from my serial monitor, the program will allways output, it should be output only at 'CR' symbol receieved.

What setting are you using for the line ending that the serial monitor appends?

If you have it append a CR or CR/LF, then you will see the message about receiving a CR every time you press the send button or hit enter.

If not, you should NEVER see it, as there is then no way for you to send a CR.

My guess is that that is exactly what is happening.

PaulS:

Whatever I sent any ASCII symbol from my serial monitor, the program will allways output, it should be output only at 'CR' symbol receieved.

What setting are you using for the line ending that the serial monitor appends?

If you have it append a CR or CR/LF, then you will see the message about receiving a CR every time you press the send button or hit enter.

If not, you should NEVER see it, as there is then no way for you to send a CR.

I sent CR by use few Serial port tools, I also use ASCII and HEX mode sent it. When I rewrite my program, it's work ok now, I don't changed anything, that's wierd. Now my program can detect CR, but it still have problem, it can't break if readed CR. My GPS send a string like as "AAAAAA(CR LF)BBBBBB", I only want AAAAA part, so I write follow program, I added a test statement "GLCD.print("CR received.....")", it point out whether the serial port got CR or not, yes it worked. But the break statement not work.

void loop() 
{
   if (Serial.available()) 
   {
   delay(100);
   while (Serial.available() > 0)
      {
       char ch=Serial.read();
       if (ch=='\r') 
         {
          GLCD.print("CR received.....");
          break;
         }
         else 
           {
           GLCD.print(ch);
           }
       }
    }
}

That's because you're not waiting on the right thing. You're only looping around on whether there is serial data available or not. That may only be the one character at a time.

You need to have the while running on if you have received the CR or not.

majenko:
That's because you're not waiting on the right thing. You're only looping around on whether there is serial data available or not. That may only be the one character at a time.

You need to have the while running on if you have received the CR or not.

I am little confused. I used a Serial monitor tools sent ASCII string to my arduino board, if the string contain CR, progam will display "CR received.....", if not received CR, this statement will not excuted. So I think the "if (ch=='\r') " workded, and "GLCD.print("CR received....." also worked, why the "break"statement not working? It's same subroutine with GLCD.print statement?

Would you please give me a sample of this progrgam?

What makes you think the "break" isn't working?
It only breaks the while loop inside the function "loop()", which then gets called again.

I think the approach for this problem is all wrong. According to your remarks:
a) You only want the AAAAAA (= 6 characters part) from the GPS message
b) the message has a CR/LF in the middle? sounds a bit strange
c) then you get BBBBBB (again 6 characters)

couple of questions:
a) what comes after BBBBBB again CR/LF or AAAAAA again?
b) are AAAAAA and BBBBBB always 6 characters each?
c) if after BBBBBB a CR/LF comes again how do you identify AAAAAA from BBBBBB?

Why not something like

void loop() 
{

char gpsMessage[7]; // 6 chars plus null terminator

while (Serial.available())
  {
  char ch=Serial.read();
  switch(ch)
    {
    case '\r':
      GLCD.print("GPS message: ");
      GLCD.print(gpsMessage);
      break;
    case '\n':
      break; // ignore
    default:
      for(byte i = 0; i < 5; i++)
        { gpsMessage[i] = gpsMessage[i + 1]; } // byte shift
      gpsMessage[5] = ch; // tack new char onto the end
    }
  }
}

This will throw away not only junk \n chars but also junk data preceding a valid message.

You are assumong that after BBBBBB a CR/LF comes in again. And I have not heard that confirmed yet. Otherwise in you example you would assume BBBBBB is a valid message as well.

Hi Nick
I think the problem with the topic starter is that he himself is not shure what his data looks like. The rest is quite simple.

Yes, well all this serial reading code with delays in it makes me shudder. Having a good basis for reading serial data is always a good start, rather than trying to debug things which are done in fundamentally a, ah, non-optimal way.

nicoverduin:
Hi Nick
I think the problem with the topic starter is that he himself is not shure what his data looks like. The rest is quite simple.

If it's GPS, it's probably NMEA: GPS - NMEA sentence information

Looks like NMEA sentences start with 6-byte prefix so that's probably why the OP is asking about that. Perhaps he is not aware of Arduino Playground - HomePage or http://www.maartenlamers.com/nmea/ or TinyGPS | Arduiniana . Or, just wants the learning experience for himself (which I can understand, as I've re-invented the wheel plenty of times for my own educational purposes...)