View previous topic :: View next topic |
Author |
Message |
nithinkrishnanrs
Joined: 01 Jun 2016 Posts: 4
|
how to extract serial data |
Posted: Wed Jun 01, 2016 11:09 pm |
|
|
how to extract serial data
$ Temp:37.88;PH:2.16;DO:10.88;CO:12.45 %
i need to extract the value and store the value like
p1=37.88
p2=2.16
p3=10.88
p4=12.45
Thanks in advance. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Jun 01, 2016 11:27 pm |
|
|
Assuming you have this as a string.
Search for ':' (use strchr). Then use atof, to read the number at this location.
Then look forward for the next ':', and use atof again for the next number. etc. etc..
Have a look in the code library, where there is example code for reading values from a GPS, which uses similar techniques. |
|
|
nithinkrishnanrs
Joined: 01 Jun 2016 Posts: 4
|
|
Posted: Wed Jun 01, 2016 11:37 pm |
|
|
thank you for your valuable information. |
|
|
nithinkrishnanrs
Joined: 01 Jun 2016 Posts: 4
|
code |
Posted: Thu Jun 02, 2016 1:11 am |
|
|
can you show an example |
|
|
nithinkrishnanrs
Joined: 01 Jun 2016 Posts: 4
|
working code |
Posted: Thu Jun 02, 2016 11:13 pm |
|
|
Code: | void str1()
{
strcpy(string,"$ Temp:37.88;PH:2.16;DO:10.88;CO:12.45; %");
//strcpy(string,"one,two,three;");
strcpy(term,":;");
i=0;
ptr = strtok(string, term);
while(ptr!=0) {
if (i==1)
{
puts(ptr);
}
if (i==3)
{
puts(ptr);
}
if (i==5)
{
puts(ptr);
}
if (i==7)
{
puts(ptr);
}
ptr = strtok(0, term);
i++;
}
|
prints
>> 37.88
>>2.16
>>10.88
>>12.45 |
|
|
|