Displaying various values from serial string


Recommended Posts

I've read what I can about parsing various values from a string of unpolled serial data and after thinking I've almost worked it out, realised I havent. I'm also not clear on whether the parsing should be done at the protocol or with a sequence. The stream is below from the com. monitor

I need to grab the values after Dm= but before D

also after Sm= but before N

also after Sx= but before N

and fire them into seperate channels. I've modified a few examples from the forum once I though I was clear on it, but I'm just getting a lot of NaN.

Can you help please.

Rx: 0r1,Dn=035D,Dm=038D,Dx=040D,Sn=14.4N,Sm=14.7N,Sx=14.9NN~G130


Rx: 0r1,Dn=028D,Dm=031D,Dx=034D,Sn=16.0N,Sm=16.3N,Sx=16.8NKWy130


Rx: 0r1,Dn=032D,Dm=034D,Dx=036D,Sn=16.6N,Sm=17.2N,Sx=17.6NFbe130


Rx: 0r5,Th=13.8C,Vh=0.0#,Vs=14.1V,Vr=3.496VEO]130


Rx: 0r1,Dn=035D,Dm=035D,Dx=036D,Sn=17.7N,Sm=18.1N,Sx=18.7NMfy130


Rx: 0r1,Dn=032D,Dm=034D,Dx=035D,Sn=19.3N,Sm=19.6N,Sx=20.0NAWS130

Link to comment
Share on other sites

Its usually easier to use a sequence. Only use protocols once you have it figured out, and then only if you need to reuse the protocol on multiple applications.

The easiest way to parse this is:

1) when reading the data, use device.mydevice.readUntil(10), make sure and purge() before you get into the loop

2) the result will be a single line. Store the result in a string variable, then call parse() on it to split it by commas:

private string p1 = parse(datain, -1, ",")

3) p1 now is an array of strings. Something like: {"0r1","Dn=035D", "Dm=038D",...} Now you just need to find the desired values. If its always the same order, you could just index into that array, but its probably better to actually search for the desired value just in case the ordering changes:

private loc = search(left(p1,2) == "Dm")

4) then you just need to parse again, this time on "=", and get just the second part:

private string p2 = parse(p1[loc], 1, "=")

5) p2 now has "038D", so you just need to convert it to a number. StrToDouble() will do this, and conveniently enough, it will stop converting as soon as it gets to an invalid character, in this case "D":

private result = strToDouble(p2)

result now has the number 38 which you can shove into a channel using AddValue(). Repeat steps 3-5 for Sm and Sx...

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.