How to parse a string and convert to integer

I have ten samples that I collect but can’t figure out how to parse the data and get it to a workable integer ( from the right 0.000) , and then get the average. a little help please

OK00,CxxH 0.363
OK00,CxxH 0.362
OK00,CxxH 0.359
OK00,CxxH 0.357
OK00,CxxH 0.357
OK00,CxxH 0.358
OK00,CxxH 0.357
OK00,CxxH 0.357
OK00,CxxH 0.357
OK00,CxxH 0.358

mega

[/void WriteSD()
{ 
content="";
dataString = "";

  for (int Cnt = 0; Cnt < 11; Cnt++)
 { 
  Serial1.write("Grab");    //CMD
  Serial1.write(13);      //cr
  Serial1.write(10);      //LF
  delay (3000);
while(Serial1.available()>0) 
  if(content !="ER00") 
  {
      character = Serial1.read();
      content.concat(character);
     // Serial.println(content);
  }

   {
     String dataString = "";
     dataString += String(content);
     //dataString += ",";
     Serial.println("ds = ");
     Serial.println(dataString);
   }   
 delay(1);
 }
    Serial.println("content");
    Serial.println(content);
 
   delay(1000); // slow down the program
    // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("lsdata.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(content);
    dataFile.close();
    // print to the serial port too:
    Serial.println(content);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening lsdata.txt");
  }

//EOC  
} code]
s = "OK00,CxxH 0.363";

int value;

scanf(s, "OK00,CxxH 0.%d", &value);

Serial.println(value);

should get you a step further....

Use strings instead of Strings
Use strtok() to get the chars after the space
Then use atof() to turn them into a float (NOT an integer !)

Something like this

char testData[] = "OK00,CxxH 0.363";

void setup()
{
  Serial.begin(115200);
  char * part1;
  part1 = strtok (testData," ");
  float aFloat = atof(strtok (NULL," "));  
  Serial.println(aFloat, 3);
}

void loop()
{
}

retexas:
I have ten samples that I collect

  for (int Cnt = 0; Cnt < 11; Cnt++)

The parse example in Serial Input Basics may be useful.

...R

I'm a novice coder, and I went thru the examples and appreciate the effort, but I got wrapped around the axel, with you're examples using Char, and I'm working with strings. Could not figure out how to merge my serial read in with the examples. Thank you

I’m working with strings

You are better off if you don’t. See reply #4.

retexas: you're examples using Char, and I'm working with strings.

My code uses strings (small s) (arrays of char terminated with a 0). I presume your code uses Strings (capital S).

Strings (cap S) are not a good idea in the small memory of an Arduino. They can easily cause memory corruption.

...R

robtillaart: ``` s = "OK00,CxxH 0.363";

int value;

scanf(s, "OK00,CxxH 0.%d", &value);

Serial.println(value);




should get you a step further....

sscanf would get you even closer

sscanf would get you even closer you're right - apologies (just too eager to answer questions ;)

I appreciate Eagerness, more so the patience!! Thanks everyone, but I appear to be in an apples and oranges mode working with this serial port When using :

s = “OK00,CxxH 0.363”;

int value;

scanf(s, “OK00,CxxH 0.%d”, &value);

Serial.println(value);

I get error “cannot convert ‘String’ to ‘const char*’ for argument ‘1’ to ‘int scanf(const char*, …)’”
how do I get “s” to recognize my variable datastring
// s = “OK00,CxxH 0.363”;
s = dataString;

My code block gets me to my end result, so I think I’m close.

 content="";
  dataString = "";
  
    for (int Cnt = 1; Cnt < 11; Cnt++)
   { 
    Serial1.write("Grab");    //CMD
    Serial1.write(13);      //cr
    Serial1.write(10);      //LF
    delay (3000);
  while(Serial1.available()>0) 
    if(content !="ER00") 
    {
        character = Serial1.read();
        content.concat(character);
       // Serial.println(content);
    }
  
       {
         String dataString = "";
         dataString += String(content);

         //dataString += ",";
         Serial.print("ds = ");
         Serial.println(dataString);

       // s = "OK00,CxxH 0.363";
       s = dataString;
        
        int value;
        
        scanf(s, "OK00,CxxH 0.%d", &value);
        
        Serial.println(value);
  
       }

Will the value of interest all ways be less than one? if so, then you might could parse starting at the decimal point/period, capture those characters, and work with those for numeric conversion.

Instead of:

s = "OK00,CxxH 0.363";

int value;
scanf(s, "OK00,CxxH 0.%d", &value);
Serial.println(value);

use:

char s[] = "OK00,CxxH 0.363";
int value;
sscanf(s, "OK00,CxxH 0.%d", &value); // sscanf !
Serial.println(value);

use code tags and get rid of the Strings.

My variable " content" contains “OK00,CxxH 0.363”, I guess I don’t understand how to embed that in to the routine.

char s[] = "OK00,CxxH 0.363";
char s[] = content;

error received

invalid conversion from 'char' to 'const char*' [-fpermissive]

You would use memcpy() to copy character array data.

But if content is already a character array, why do you want to copy it somewhere else?

Apparently I'm in to something that I do not fully understand. I have an instrument that I connect with using serial1. I have established communication, and receive the correct data coming back. I would like to parse the string, or the array if that is easier to get just the numeric values. After ten samples I can calculate the average. I think I'm dealing with strings and using mydat=(content.substring(10,15)); I am able to get the data, but its a static value and does not up date as expected.

All of the examples suggest "Char", but then I get the errors. A little guidance would be appreciated.

retexas: Apparently I'm in to something that I do not fully understand.

We don't fully understand either.

I have an instrument that I connect with using serial1.

What kind of instrument?

A little guidance would be appreciated.

Guidance for using these forums: Please post your entire sketch and not just a snippet. We can't fix what we can't see.

Something simple that might get the job done. Note that I used some old modified code and used a ; as the data delimiter.

//zoomkat 11-12-13 String capture and parsing  
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// ; is used as the data string delimiter

String readString; //main captured String 
String decimal; //data String
String number; //data String

int ind1; // . location
int n;  // for integer conversion

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like OK00,CxxH 0.363;

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ';') {
      //do stuff

      Serial.println();
      Serial.print("captured String is : "); 
      Serial.println(readString); //prints string to serial port out

      ind1 = readString.indexOf('.');  //finds location of first ,

      decimal = readString.substring(ind1);   //captures first data String
      Serial.print("captured decimal number = ");
      Serial.println(decimal); 

      number = readString.substring(ind1+1);   //captures first data String
      Serial.print("captured number = ");
      Serial.println(number);
      
      n = number.toInt();  //convert readString into a number
      Serial.print("new interger = ");
      Serial.println(n); //so you can see the integer

      readString=""; //clears variable for new input
      decimal="";
      number="";

    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

retexas: A little guidance would be appreciated.

If you have tried to use the examples in Serial Input Basics (which do work) then post your best effort and explain what it actually does and what you want it to do and I will try to help.

...R

retexas: I think I'm dealing with strings and using mydat=(content.substring(10,15));

How about

int value;

  sscanf(mydat.c_str(), "OK00,CxxH 0.%d", &value);
  Serial.println(value);