Identifying a character from serial.read()

Hi guys i am reading some string("A1234B1234C1234" test data)at arduino via Serial.read() now i want to identify the Character "B" and read next 4 data. I tried using substring but it is not getting detected. Any idea??

Thanks & Regards

Niladri

Not enough information. Posting your code would help.

Sorry…

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

void loop()
{
  if(Serial.available()>0)
  {
  
     String data = (String)Serial.read();
//     char* serial_data[20]={data};
//   char *ch= serial_data;
    
//    if(ch[0] == "A" )
//    {
     if(data.substring(0,4) == "A1234")
//    for(int i = 0; i<=20; i++)
    {

     Serial.print(1);
//     delay(500);
    }
   else
          Serial.print(0); 
  }
  delay(500);
}

Did you mean
data += (String)Serial.read(); ?

(Sorry, I don't use String)

     String data = (String)Serial.read();

Wish all you want, but it does not change the fact that Serial.read() returns ONE character. Casting that as a String is a complete waste of time.

What is sending the data that you are trying to read? Do you have any control over that end?

Not exactly. I found the substring example sketch so i tried to use that but if i don't use typecasting it says " Invalid conversion from int to const char*". My main requirement is i will have set of data preceding with A B C etc so that it will be my indicator that i have to read next 4 character do engineering conversion to make floating data if you better solution/idea please post it.

Thanks & Regards

Niladri

Read characters until you get you get a 'B'.
Buffer the next four characters in a five element char array.
Terminate the string with '\0';
Apply "atof".

You may add some code to cope with dropped characters.

Yeah that i have it in my mind but the data i read from serial read is one byte i cannot store in a array or if i store it a char then the entire data is one character that i don't no any traversal technique of find a character in a one "big character"???

Thanks & Regards

Niladri

My old code to read strings.

This code was using it to read an analog modem 56k response.

Probably could be useful for you like example

Regards

#include <WString.h>

// Define cadena maxima de texto para el modem
#define maxLength 30
String inString = String(maxLength); 
String dataString;       // Crea un nuevo string


// ????????????????????????????????????????????????
// ?  FUNCION lee  Lee los datos del puerto serie ?
// ????????????????????????????????????????????????
void lee()  {  
  while(Serial.available() > 0) 
    {
    char inChar = Serial.read();
    if (inString.length() < maxLength) {
      inString.append(inChar);} 
    else {
      inString = inChar;}
    }
      Serial.flush();
 }

// ????????????????????????????????????????????????
// ?  FUNCION CheckLee  Comprueba datos del modem ?  
// ????????????????????????????????????????????????
void CheckLee(){
  if (inString.contains("RING"))
     ..... your code....
    {

but the data i read from serial read is one byte i cannot store in a array

Why not?

char aChar = Serial.read();
buff[index++] = aChar;
buff[index] = '\0';

Look, the character was added to an array.

or if i store it a char then the entire data is one character

No, an array of chars is NOT on character.

that i don't no any traversal technique of find a character in a one "big character"???

Of course not... You can'''t find a g in a G... There are traversal techniques that allow you to find a character in a string,,, though...

Do you find that the triple punctuation helps??? Personally, I don'''t!!!

The generally accepted ratio of answers to questions is 1.0 or higher. Your ratio is a lot lower than that.

The generally accepted ratio of answers to questions is 1.0 or higher. Your ratio is a lot lower than that.

Sorry I did not get you. :~

Thanks & Reagrds

Niladri

Any idea??

  1. read your data into a string;
  2. search for 'B'.
  3. read out the next 4 bytes.
  4. open a beer in celebration.

Quote
The generally accepted ratio of answers to questions is 1.0 or higher. Your ratio is a lot lower than that.

Sorry I did not get you. smiley-confuse

What is sending the data that you are trying to read? Do you have any control over that end?

Two questions require two answers for a 1:1 ratio.

I followed the procedure what Mr. PaulS suggested yet still not able to detect the character “A”.

char serial_data[100];
int index = 0;

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

void loop()
{
  if(Serial.available()>0)
  {
    for(int index = 0; index <=20; index++) 
     char data = Serial.read();

//    if(ch[0] == "A" )
//    {
  
      serial_data[index] = data;
      
  }
           
//      char frstchar = buff[0];
     if(serial_data[0] == 'A')
//    for(int i = 0; i<=20; i++)
    {

     Serial.print(1, DEC);
//     delay(500);
    }
   else
          Serial.print(0, DEC); 
  }
  delay(500);
}
if(Serial.available()>0)
  {
    for(int index = 0; index <=20; index++) 
     char data = Serial.read();

The usual mistake (I should have a keyboard macro for this one) - check to see that there is at least ONE character in the buffer, then go ahead and read all 21 of them.
Wrong. Serial is sloooooow.

Edit: I took another look (as should you) - it isn’t really surprising you were not able to detect an ‘A’ - code that doesn’t even compile cannot run.

Good Morning,
I think is necessary to do a for cicle, scan char by char and check the condition ...

Regards,
Andrea

But, more importantly, it is necessary to post code that compiles, and doesn't read characters that haven't arrived yet.

And, it’s important to answer the questions that are asked. If you have any control over the sender, you can make your job so much easier. For instance, if the sender could me made to send “” instead of “A1234B4567C8901D3456”, you could use code like this to read the data whenever it gets there:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Then, where it says “Process the packet”, you could separate out the A, B, C, and D values.

Hey sorry for the last one. Here the compile code.

char serial_data[100];
int index = 0;
char data;
void setup()
{
  Serial.begin(9600);
 
}

void loop()
{
  if(Serial.available()>0)
  {
   data = Serial.read();
//    if(ch[0] == "A" )
//    {
   serial_data[index++] = data;
          
//      char frstchar = buff[0];
     if(serial_data[0] == 'A')
//    for(int i = 0; i<=20; i++)
    {

     Serial.print(1, DEC);
//     delay(500);
    }
   else
          Serial.print(0, DEC); 
  }
  delay(500);
}

Ok Mr. PaulS I will make the packet in a way you have describe(because i am also the sender).