Serial.Read

Good afternoon,

I have a big problem in my project.

I have the following code in Arduino, to read de data from labview (string with “LG:”) and this program not read the values in Serial.print.
My objective is when i put the button (“LG:”) in labview the code enters in (if string==“LG”) the serial monitor prints de value.
Can help me?

#include <eHealth.h>
#include <eHealthDisplay.h>

char c;
String string;
void setup()
{

eHealth.readGlucometer();
Serial.begin(115200);
delay(100);
}

void loop()
{
if(Serial.available() > 0) string = “”;

while(Serial.available() > 0)

{
c = (byte)Serial.read();
if(c == ‘:’)
{
break;
}
else
{
string += c; // string = string + c;
}
delay(1);
}

if(string == “LG”)
{
Serial.print(eHealth.glucoseDataVector[0].glucose);
Serial.print(eHealth.glucoseDataVector[1].glucose);
Serial.print(eHealth.glucoseDataVector[2].glucose);
Serial.print(eHealth.glucoseDataVector[3].glucose);
delay(1000);
}
if(string ==“DG”)
{
Serial.flush();
}

}

Thanks.

first PLEASE wrap [ code ] [ / code ] around your code secondly Serial.available only means "one or more characters ready to be read" it does NOT mean you can assume the whole string is there so your code is likely reading one character then emptying the string again just keep adding until you get ":", process it THEN empty the string in fact you should probably wait for NEWLINE before processing if LG: id followed by a number

if you are looking for a specific char to mark the end of the communication use Serial.readBytesUntil()

http://arduino.cc/en/Serial/ReadBytesUntil

Simple way to send serial commands to the arduino.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

you can not compare strings the way you have.. You must use .equals() E.g

String myString = "Hello";
String thisString = "World";

if(myString.equals(thisString)){ // this is the same as myString == thisString
    //it works
}
if(myString.equals("Add a String here")){ // this is the same as myString == "Add a String here"
    //it works
}

For more details check this reference page: [url=http://arduino.cc/en/Reference/StringEquals[/url]