Collecting and Parsing string data from Serial.read();

I am using an Arduino Due and a Bluegiga WT32 A2DP bluetooth module connected to Serial2 together. I managed to setup a basic program to communicate with the WT32 using SPI. I am trying to collect the Artist and Track information from WT32 when I send a command through Serial and parse it into artist and song title, so I can then display it on LED matrix.

I have read several posts about collecting serial data as a string and then parsing it, but my attempts are not panning out. I am coming from ActionScript, and I consider myself only a novice coder, so please try and help me understand what I'm doing wrong.

The command "AVRCP PDU 20 " tells the WT32 to spit out all the track information which looks like this:

AVRCP GET_ELEMENT_ATTRIBUTES_RSP COUNT 7 TITLE "Do You Want It All? (Live at Brixton Academy)" ARTIST "Two Door Cinema Club" ALBUM "Beacon (Deluxe)" TRACK_NUMBER "14" TOTAL_TRACK_NUMBER "12" GENRE "" PLAYING_TIME "229000"

But when I run the program and send the "#" command I get this:

Asking for INFO...
this is the message: VRCP PDU 20
AVRCP GET_ELEMENT_ATTRIBUTES_RSP COUNT 7 TITLE "Do You Want It All? (Live at Brixton Academy)" ARTIST "Two Door Cinema Club" ALBUM "Beacon (Deluxe)" TRACK_NUMBER "14" TOTAL_TRACK_NUMBER "12" GENRE "" PLAYING_TIME "229000"

For some reason it seems to be collecting the command and then when I call:
I thought that when I call:

 Serial.print("this is the message: " + st);

It is returning the command as my string st, and then returning the track information separately.

Here's the whole code:

long t1;
String st, msgChar, msgInt;
int val;

void setup() {
  // initialize both serial ports:
  Serial.begin(19200);
  Serial2.begin(19200);
}

void loop() {
  
  // read from Serial2, send to Serial:
  if (Serial2.available()) {
      int inByte = Serial2.read(); //read data
      Serial.write(inByte); //write data
  }
  
  // read from Serial, send to Serial2
  if (Serial.available()) {
    int outByte = Serial.read();
    if (outByte == '#'){  //if the value is "#" then write a special phrase
      Serial2.write("AVRCP PDU 20 ");
      Serial.write("Asking for INFO...\n");
      st = String("");  //setup st string
      while(Serial2.available()){  //repeat while there is data
        st = st + char(Serial2.read()); //save data to st string
        delay(1); //wait 1
      }
      Serial.print("this is the message: " + st);
    }
    else{
      Serial2.write(outByte);
    }
  }
  
}

Almost certainly the first piece of advice that you will get is to stop using Strings and to use strings instead. Note the subtle difference in the name. The Arduino implementation of Strings is often regarded as buggy and although fixed in IDE 1.0.4 many still regard them as problematic and unnecessary and prefer C style strings instead.

Look at this page to see how a string (lower case) can be read into an array for later manipulation.

Another problem is that you expect that the response to the "AVRCP PDU 20 " command will arrive instantaneously. Not by a long shot it won't.

A third problem is that you are printing stuff to the Serial Monitor without identifying what that stuff is.

 Serial.print("st: [");
 Serial.print(st);
 Serial.println("]");

would be far better. Then, you'd know exactly what you printed, and exactly what the variable contained.

I have read several posts about collecting serial data as a string and then parsing it, but my attempts are not panning out. I am coming from ActionScript, and I consider myself only a novice coder, so please try and help me understand what I’m doing wrong.

Simple servo test code that captures characters sent to the arduino in to a String, parses the String into the desired data, and does something useful with the result.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
    servo1="";
    servo2="";
  } 
}