7
\$\begingroup\$

I have Arduino connected to a HC-06 bluetooth module.

I wan't to send data from some Android-Application I wrote to the Arduino and let Arduino do stuff depending on the received data. So far I'm Sending Integer values between 0 and 1024.

Unfortunately mySerial.read() won't come to an end here. It reads the Data as one big string.

My Modules RX and TX pin are connected to my Arduinos RX- and TX pin. (The console didn't show any data when I connected them to Port 10 and 11, so I thought this is how SoftwareSerial defines the RX and TX Port of Arduino...)

My Sketch:

#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(Data);
     Serial.println("foo");
    }

This will output:

154888143201270341421501588671750825906975101110241005969922864792711629540451369290213140600

I can't read this data properly and I can't parse the data to some function to handle it, because mySerial.read() won't come to an end. There must be a better way to receive or send this Data.

My Bluetooth Output Stream Source:

public void sendPWM(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");

                byte[] buffer = String.valueOf(progress).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();
            }

        }

I don't close the outputStream, because this will also cancel the connection with the HC-06 and I have to send a lot of data. I can't let the user reconnect to the module each time he sends data. Can you help me out here?

\$\endgroup\$
  • \$\begingroup\$ Unless I'm missing something don't you just need to send say a carriage return on the end of each reading? Then on the Arduino when you get that you know you have a complete reading and can clear the buffer next for the next reading. \$\endgroup\$ – PeterJ Oct 7 '13 at 8:56
  • \$\begingroup\$ I tried to and added +"\r\n" to my string. This will only add a break and won't stop mySerial.read(); \$\endgroup\$ – Wolfen Oct 7 '13 at 19:10
  • \$\begingroup\$ I'm not sure if this is related to your problem, but I would think that your "while(mySerial.available())" should end before Serial.println, otherwise you're re-sending the same String object each time a new character gets added. And since you're doing that, you may end up wasting time, thereby potentially missing some incoming characters. I assume what you want is read the mySerial as long as there is stuff to read, adding the value to "Data", and only at the end transmit the full "Data" string. \$\endgroup\$ – fm_andreas Oct 7 '13 at 19:29
  • \$\begingroup\$ I put it in the loop because I wanted to test, whether it would print "foo" or not. As you can see, It doesn't since it's stuck. Even when I added ` mySerial.println("foo my serial");` it wont print. \$\endgroup\$ – Wolfen Oct 7 '13 at 20:20
  • \$\begingroup\$ Does it print "foo my serial" when it is before the while loop, or even in setup()? \$\endgroup\$ – fm_andreas Oct 7 '13 at 21:43
4
\$\begingroup\$
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
String Data = "";

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

void loop() // run over and over
{
    while (mySerial.available())
    {
        char character = mySerial.read(); // Receive a single character from the software serial port
        Data.concat(character); // Add the received character to the receive buffer
        if (character == '\n')
        {
            Serial.print("Received: ");
            Serial.println(Data);

            // Add your code to parse the received line here....

            // Clear receive buffer so we're ready to receive the next line
            Data = "";
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Not working. Its stuck at mySerial.read(); it won't even check the incoming data for \n ... \$\endgroup\$ – Wolfen Oct 9 '13 at 13:18
3
\$\begingroup\$

I know it's a little bit old, but i got it working.

    #include <SoftwareSerial.h>

    SoftwareSerial mySerial(10, 11); // RX, TX
    String data = "";

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

    void loop() // run over and over
    {
      while(mySerial.available()==0)
      {}

      char character;
      while(mySerial.available()>0)
      {
        character = mySerial.read();
        mySerial.write(character);
      }

      data += character;

      if (character == 13) {  
        mySerial.print("Received: ");
        mySerial.println(data);
        data = "";
      } 
    }
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.