简体   繁体   中英

Arduino making a serial read string split and Then convert to Int

I've been trying split a string in arduino using many different ways, but nothing seems to work. My latest attempt:

String inData = "";
    int cPosition;
    String data[2];
    int count = 1;

    if (Serial.available() > 0) {

            int ssize = Serial.available();
            for(int i=0;i<ssize;i++){
              inData += (char)Serial.read(); 
            }

            do{
               cPosition =  inData.indexOf(':');
                if(cPosition != -1)
               {
                 data[count] = inData.substring(0,cPosition);
                 count++;
               } 
             }while(cPosition >=0);


            Serial.println(data[1]);

    }

I have two values coming in as "00:00" and I want to split them, but also to convert the split values to Int, as they will always be integers between 00 and 100.

Hey have you tried x=Serial.parseInt(); this read the next data if the an Integer it will find for exampel "fahijah123234fjrkaljf" come in in x will be 123234 same to next integer value

//incommeing: jsj123456asklfj654321faskl
int x,y;
x=Serial.parseInt();
y=Serial.parseInt();

Serial.println(x);//123456
Serial.println(y);//654321

to merege them use strcat or sprintf

http://arduino.cc/en/Serial/ParseInt , http://arduino.cc/en/Serial/ParseFloat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM