Serial write substring index of showing all input

Hello guys,

I am having a problem getting the right serial print from my program.

I serial write then save it to a char
transform chat to int
and use index of() to search for specific char
then i want to print out the string from 0 to indexOf()
but instead i receive the whole serial input.
Any suggestion would be much appreciated
serial_write_example.ino (925 Bytes)

char d;
char d1;
String str_d;
int int_d;
int servo1degree, servo2degree;
int indexOfA, indexOfB;
String str_send;
String str_servo1, str_servo2;

void Receive_serial_data(){
  
  while (Serial.available()>0){
    
    d=Serial.read();
    if(d=='\n') {break;}
    else {str_d+=d;}
    }
  
  }
void Parse_the_data()
{
   
  indexOfA = str_d.indexOf("A");
  indexOfB = str_d.indexOf("B");

  if(indexOfA >-1){ str_servo1=str_d.substring(0,indexOfA );          servo1degree=str_servo1.toInt();   }
  
  if(indexOfB >-1){ str_servo2=str_d.substring(indexOfA+1, 
 indexOfB);  servo2degree=str_servo2.toInt();   }
  }

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
 Serial.write("\n8A13\nB56");
 Receive_serial_data();

 if(d=='\n') 
 {
  Parse_the_data();
  d=0;
  str_d=""; 
  }
Serial.print(str_servo1);

}

and the print i am getting is whatever i put on serial write command

where?

[quote="giorgos_tzaf, post:1, topic:937661"]

void Receive_serial_data(){
  
  while (Serial.available()>0){
    
    d=Serial.read();
    if(d=='\n') {break;}
    else {str_d+=d;}
    }
  
  }

void Parse_the_data()
{
   
  indexOfA = str_d.indexOf("A");
  indexOfB = str_d.indexOf("B");

  if(indexOfA >-1){ str_servo1=str_d.substring(0,indexOfA );          servo1degree=str_servo1.toInt();   }
  
  if(indexOfB >-1){ str_servo2=str_d.substring(indexOfA+1, 
 indexOfB);  servo2degree=str_servo2.toInt();   }
  }

hello

I receive serial data from void receive_serial_data()
and then at void parse the data() if indexOF >-1 then create substring from (0 to indexOfA).

Hello, I’ve seen your code, my question is about the difference between what you say and what you do, could you please be so kind and comment this in your code explicitly because I cannot find it

yes sure:
commented code:

char d; // char reads serial input

String str_d; // convert serial input to str

int servo1degree, servo2degree;  // wanted output
int indexOfA, indexOfB;

String str_servo1, str_servo2;




void setup() {

Serial.begin(9600);
}

void loop() {
 Serial.write("8 A"); // serial input
 Receive_serial_data(); // function of receiving data from serial (below)

 if(d=='\n') //checks input
 {
  Parse_the_data(); // parse data function (below)
  
  d=0;
  str_d=""; 
  }
Serial.println(indexOfA); // serial print output

}

void Receive_serial_data(){  //function for receiving serial data
  
  while (Serial.available()>0){
    
    d=Serial.read();   //save serial read at char d
    if(d=='\n') {break;}
    else {str_d+=d;}  // save serial read at str str_d
    }
  
  }

void Parse_the_data(){
   
  indexOfA = str_d.indexOf("A"); //check if "Α" is in the str_d
  indexOfB = str_d.indexOf("B"); //check if "Α" is in the str_d

  if(indexOfA >-1){ str_servo1=str_d.substring(0,indexOfA );          servo1degree=str_servo1.toInt();   } // if "Α" exist in str_d create a sub string from (0, indexOfA )
  
  if(indexOfB >-1){ str_servo2=str_d.substring(indexOfA+1, indexOfB);  servo2degree=str_servo2.toInt();   }// if "B" exist in str_d create a sub string from (indexOfA+1, indexOfB )
  }

Your code It's a little bit confused.
Why you split the parsing job between loop and function?

void Parse_the_data()
{
  indexOfA = str_d.indexOf("A");
  indexOfB = str_d.indexOf("B"); 
  if(indexOfA >-1){
    str_servo1 = str_d.substring(indexOfA+1);          
    servo1degree = str_servo1.toInt();  
    Serial.print("Servo1: ");
    Serial.println(servo1degree);
  }

  if(indexOfB >-1){
    str_servo2 = str_d.substring(indexOfB+1);              
    servo2degree = str_servo2.toInt();
    Serial.print("Servo2: ");
    Serial.println(servo2degree);
  }
  str_d = "";
}

So what is the result?

for Serial.println(indexOfA); i am geting output : "serialinput""0"
no mater what the input is.
What i am trying to do is to split serial input to strings based on letters.
Is there any example for that?

and how should the loop function be?
just

void loop() {
Serial.write("\n813B56");
 Receive_serial_data();

 
  Parse_the_data();
  

Serial.println(str_servo1);

?

This is not in your code, this means you are not showing your whole code. Good luck with your mysterious problem.

I figure it out.
The code was fine, the problem was with Serial.Write() function
When entering directly from serial monitor it works fine!!!

Thanks a lot for your time!!
Giorgos

This is how me would do something similar.
Using as less as possible global variables especially if String type.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.