Notifications
Clear all

Parsing strings  

  RSS

queenidog
(@queenidog)
Active Member
Joined: 10 months ago
Posts: 10
2020-02-14 5:48 pm  

The article on the 433 MHz radio was great.  In the program a string containing humidity and temperature information is transmitted to the receiver, where it is put back together and result printed.  I understand (roughly) how it is done for TWO items but how would one code for THREE or more items.  I tried using the StringSplitter library but that was even more confusing since the example was all in one program file.  I'm using Arduinos UNOs (and C++).  Anyone?


Quote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 467
2020-02-14 7:15 pm  

I don't know what article on the 433 MHz radio you are referring to?
If I could see the relevant code example it would help.
Splitting strings isn't hard and I wouldn't bother with a library myself.
Looking at the StringSplitter example it looked simple enough to use.

 


ReplyQuote
mday
 mday
(@mday)
New Member
Joined: 1 year ago
Posts: 3
2020-02-14 9:51 pm  
 
Here is an untested snippet:
 
String str1;
String str2;
String str3;

// The entire message is in the String str_out
// Split string into three values

// First, find the offsets of the two commas
int comma1 = str_out.indexOf(",");
int comma2 = str_out.index(",", comma1+1);

// Then extract the substrings
str1 = str_out.substring(0, comma1); // before first comma
str2 = str_out.substring(comma1+1, comma2); // between the two commas
str3 = str_out.substring(comma2+1); // after the second comma
Hope that helps,
-Mark

 


ReplyQuote
dxj
 dxj
(@dxj)
Eminent Member
Joined: 6 months ago
Posts: 25
2020-02-15 1:00 am  

Something else to consider:

  int day, year;
char weekday[20], month[20], data[100];

strcpy(data,"Friday,February,14,2019");
sscanf(data, "%[^,],%[^,],%d,%d", weekday, month, &day, &year );
Spoiler
About [^],
The parser %[^], instructs the sscanf function to read characters other than comma until you get to a comma. This is not needed if the data is delimited by white-space - i.e. "%s %s %d %d" works otherwise.

 

This post was modified 5 months ago 4 times by dxj

ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 455
2020-02-15 11:11 am  

@mday

It looks fine, except for the line, which should be using the same function:

    int comma2 = str_out.indexOf(",", comma1+1);

If that's all you want to do then this is fine, and reasonably safe.

Cheers!


ReplyQuote
queenidog
(@queenidog)
Active Member
Joined: 10 months ago
Posts: 10
2020-02-15 1:54 pm  

@mday

Thanks Mark.   That's what I wanted.  I was close.  Your code looks simpler than original offering.   Haven't tried it yet, but it gives me ideas...


ReplyQuote
queenidog
(@queenidog)
Active Member
Joined: 10 months ago
Posts: 10
2020-02-15 7:52 pm  

@mday

Mark, your code works.   At first my last entry (volts) ended with a bunch(5 or 6) of odd characters, so I changed this line: uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];   to this line: uint8_t buf[16];, guessing that the buffer size was (3x5)+ 1 for null character.   This cleaned it up.  My question is why the first statement didn't work (taken from RadioHead library)?
Complete code here:


This post was modified 5 months ago by queenidog

ReplyQuote