0

I am working on my final assignment. The problem is I want to make a prototype tool, with 2 string compare. If I send data "OK DISC DISCLOSED: 341513DB689AOK RSSI: -056 OK NAME: CarBT1 OK DISCE" will be parsed and bring up data "CarBT1 OK DISCE" if the data is correct then the LED will turn on, but my program isn't working? does anyone know the problem?

String dataIn; 
String dt[10];
int i;
boolean parsing=false;
const int pinLED = 8;
String readString;

void setup (){
  Serial.begin(9600);
  pinMode(pinLED, OUTPUT);
  Serial.print("Sketch:   ");   Serial.println(__FILE__);
  Serial.print("Uploaded: ");   Serial.println(__DATE__);
  Serial.println(" ");
  while(!Serial){
    ;
  }
}

void loop (){
  if(Serial.available()>0) {
  char inChar = (char)Serial.read();
  dataIn += inChar;
  if (inChar == '\n'){
  parsing = true;
  }
}
if(parsing){
  parsingData();
  parsing=false;
  dataIn="";
}
while(Serial.available()){
  delay(3);
  char c =  Serial.read();
  readString += c;
}
readString.trim();
  if(readString.length()>0){
    if(readString == "CarBT1 OK+DISCE"){
      digitalWrite(pinLED, HIGH);
      Serial.println("Switching On");
   }
  if(readString == "off"){
      digitalWrite(pinLED, LOW);
      Serial.println("Switching Off");
   }
readString = "";
 }
}

void parsingData(){
int j=0;
Serial.print("data masuk : ");
Serial.print(dataIn);
Serial.print("\n");
dt[j]="";
for(i=1;i<dataIn.length();i++){
  if ((dataIn[i] == ':') || (dataIn[i] == ':')){
  j++;
  dt[j]="";
}
  else{
  dt[j] = dt[j] + dataIn[i];
  }
 }
}
Serial.print("data 1 : ");
Serial.print(dt[0]);
Serial.print("\n");
Serial.print("data 2 : ");
Serial.print(dt[1]);
Serial.print("\n");
Serial.print("data 3 : ");
Serial.print(dt[2]);
Serial.print("\n\n");
Serial.print("data 4 : ");
Serial.print(dt[3]);
Serial.print("\n\n");
}

migrated from electronics.stackexchange.com Feb 24 at 17:51

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • i think you need to add more info than (does not work). perhaps try remove (\r) char with string.replace("\r",""). and its better to use (readUntil) function than your method. – Hasan alattar Feb 24 at 13:54
  • Part of your assignment is to learn debugging skills. Ask yourself what DOES work and where does it start to go wrong? Build from there. – Brian Drummond Feb 24 at 14:04
  • Thank you for your response. Actually I tried to parse the data entered through serial on Arduino, and it worked. The problem is when the data that has been parsed I compare it to the string. I take an example, if I enter a message past the Arduino serial in the form of ABC: DEF I will be parsed into two data. data1 = ABC, data2 = DEF. When one of my data compares for example if (data1 == "ABC") {digitalWrite (pinLed, HIGH)}. LED does not turn on – Caraka Tri Feb 24 at 15:04
  • while(Serial.available()){ delay(3); char c = Serial.read(); readString += c; } readString.trim(); if(readString.length()>0){ if(readString == "CarBT1 OK+DISCE"){ digitalWrite(pinLED, HIGH); Serial.println("Switching On"); } if(readString == "off"){ digitalWrite(pinLED, LOW); Serial.println("Switching Off"); } readString = ""; } and this is the problem code, I took it from the code above – Caraka Tri Feb 24 at 15:06
  • your code is inconsistently indented ..... that makes it difficult to follow the program flow ..... please try to correct the indentation of your code – jsotola Feb 24 at 19:24
0

I think that the biggest problem that you are encountering is that reading a line from serial is not as straightforward as you would like. I don't have a quick and easy solution for you, but there is much on this issue that you can look at.

You have two serial read while loops. The first exits as soon as there are no characters to read (even though some may be on the way). When "\n" is read, you set parsing but it does not mean that \n is removed from dataIn.

The second serial read while loop is using a delay(3) after detecting that a character is available, but that character could have arrived after the first read and be the only character in the buffer, for example.

I suspect that the reason that your String comparison does not work is simply because the Strings are not equal.

Your String looks like something read from an HM-10/11 or something similar. I have had some success reading those transmissions by doing the following:

Make sure the serial buffer is empty. When a character is available, read it into your building String, but continue looking for characters that are available for a set amount of time (maybe a couple of seconds). Continue looking for characters until the time has elapsed and not as soon as there are no characters available (because more may be coming). Alternatively, keep building the string until you recognize the "DISCE" marking the end of that transmission.

A problem, however, is that, in my experience, if it is a very long line (that can occur when there are many devices out there), you will never get that "DISCE". I assumed that this was a firmware issue.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy