Contoh Code Parsing Data string Serial Arduino

 

String readString; //main captured String

String data1; //data String
String data2;
String data3;
String data4;
String data5;

//format data diterima   dari pc atau arduino lainya : 123a123b123c123d123*

void setup()
{
Serial1.begin(9600);

}

void loop()
{
TerimaData();
}//loop

void TerimaData(void)
{

if( Serial1.available())
{

//=================
char c = Serial1.read();           //gets one byte from serial buffer
if (c == ‘*’)
{
ind1 = readString.indexOf(‘a’); //finds location of first ,
data1 = readString.substring(0, ind1); //captures first data String

ind2 = readString.indexOf(‘b’, ind1+1 ); //finds location of second ,
data2 = readString.substring(ind1+1, ind2+1); //captures second data String

ind3 = readString.indexOf(‘c’, ind2+1 );
data3 = readString.substring(ind2+1, ind3+1);

ind4 = readString.indexOf(‘d’, ind3+1 );
baris4 = readString.substring(ind3+1); //captures remain part of data after last ,

ind5 = readString.indexOf(‘*’, ind4+1 );
data5 = readString.substring(ind4+1); //captures remain part of data after last ,

readString=””; //clears variable for new input

data1=””;
data2=””;
data3=””;
data4=””;
data5=””;
}
else
{
readString += c;     //  data serial yg dibaca gabungkan
}

}

Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis untuk pemula.

Posted on 13/04/2018, in Arduino. Bookmark the permalink. Comments Off on Contoh Code Parsing Data string Serial Arduino.

Comments are closed.

%d bloggers like this: