SoftwareSerial won't stop reading, even after carriage return

I wan’t to send some integer values from my android phone to arduino. I connected the HC-06 bluetooth module and established a connection. Unfortunately SoftwareSerial won’t stop reading after I’ve sent my value.

This is how my sketch looks like:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
char character;
void setup()  
{
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);

}
void loop() // run over and over
{
    String Data = "";
    while(mySerial.available()) {
     character = mySerial.read();
     Data.concat(character);
     Serial.println("foo");
mySerial.println("foo_software_serial");
    }

result:

0
1
8
22
58
.
.
.

My android app function that sends the integer-value

		public void sendData(int progress) {
			OutputStream tmpOut = null;
			try {
				tmpOut = mmSocket.getOutputStream();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Log.e("error", "NO OUTPUT STREAM", e);
				e.printStackTrace();
				return;
			}

			try {
				// byte[] buffer = stringToBytesUTFCustom("1");
				String value = String.valueOf(progress) + "\r\n";
				byte[] buffer = String.valueOf(value).getBytes();
				tmpOut.write(buffer);

				// tmpOut.flush(); /*I have turned this ON and OFF, to see if it
				// makes any difference*/

			} catch (IOException e) {
				// TODO Auto-generated catch block
				Log.e("error", "EXCEPTION DURING WRITE", e);
				e.printStackTrace();
			}

		}
	}

It looks like the SoftwareSerial doesn’t realize the carriage return. It just continues reading and outputs the Data. What do I do wrong here?

It looks like the SoftwareSerial doesn't realize the carriage return. It just continues reading and outputs the Data. What do I do wrong here?

Where in your receiving code do you tell the arduino to do something when a carriage return is encountered? Simple code using a comma , as a data packet delimiter.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
  
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

wolfenboard:
This is how my sketch looks like:

Which doesn't compile.

sketch_oct08d.ino: In function 'void loop()':
sketch_oct08d:18: error: expected `}' at end of input

Read this:

wolfenboard:
It looks like the SoftwareSerial doesn't realize the carriage return. It just continues reading and outputs the Data. What do I do wrong here?

If you want your sketch to do something when it encounters a carriage return, you have to put in code to do that.

Note that in your example you are appending each received character to a String without ever clearing the String so your sketch would eventually run out of memory. I would recommend using c-strings (null-terminated char arrays) rather than using the String class, but whichever you use you will need to control the amount of input that is buffered.

Also note that your sketch is printing multiple bytes to the Serial port for each byte received on mySerial so if mySerial remains busy then your sketch will eventually block on the Serial output and end up losing (discarding) bytes coming from mySerial.

Also note that you need to call Serial.begin() first in order for Serial.println() to do anything useful.

The problem is, my arduino won't stop reading. When I call mySerial.read() it will read the incoming data and there is no way to break out of it. I can't check for delimiters or do anything.

wolfenboard:
The problem is, my arduino won't stop reading. When I call mySerial.read() it will read the incoming data and there is no way to break out of it. I can't check for delimiters or do anything.

That isn't your problem. Your problems are many, but those do not include "won't stop reading".

Problem 1: This code:

void loop() // run over and over
{
    String Data = "";
    while(mySerial.available()) {
         character = mySerial.read();
         Data.concat(character);
         Serial.println("foo");
         mySerial.println("foo_software_serial");
    }

will NOT produce numbers on Serial or mySerial. It will produce strings of "foo" and "foo_software_serial".

Problem 2: Look at the comment right after void loop(). That's a hint. It tells you that it will keep running, right? So why would you think it's a good idea to declare and clear Data every time through the loop? You read 1 character, concatenate it to Data, do two serial println(), then immediately clear Data again.

Problem 3: You say you want to have it stop reading mySerial when you send it a C/R, which IS a delimiter, but you don't check for it, and of course, you can't do anything about it if you don't actually check for it. You say it's because mySerial keeps reading, but it doesn't. It reads 1 character every single time it has one in the serial buffer, and ONLY when it has a character in the serial buffer.

Now, someone has given you great advice. He pointed you to a link that you REALLY need to read and understand.. It's Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

If you can't understand it, we will certainly help you understand it.