Unable to Extract Data from a String and Save to Variables
jmmb392
Posts: 22
Dear forum,
I am having problems extracting data from a string. For example, I have the following string:
Where every value between a comma "," is a reading from a sensor and I have to store it in variables so it would be like
sensor1 := 486
Temp1 := 21
sensor2 := 786
.
.
sensorn := 7666
Now, any suggestions in how to extract the values from the string and save them into variables?
This is the code that I have so far:
I am attaching the entire project as a zip file.
Thank you for your collaboration guys!!
I am having problems extracting data from a string. For example, I have the following string:
input := string"486,21,786,45561,.....,5444,12,7666"
Where every value between a comma "," is a reading from a sensor and I have to store it in variables so it would be like
sensor1 := 486
Temp1 := 21
sensor2 := 786
.
.
sensorn := 7666
Now, any suggestions in how to extract the values from the string and save them into variables?
This is the code that I have so far:
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR byte databuffer[100] OBJ pst : "Parallax Serial Terminal" nums : "Simple_Numbers" str : "STRINGS2" PUB Start | index, index2, input, length, packet_length pst.start(9600) ''Example string input := string("1254,23,7854,2111,112,") length := strsize(input) packet_length := length pst.Str(String("Input string format = ")) pst.str(input) pst.newline databuffer[length] := "," repeat index from length to 0 databuffer[index - 1] := get_char(input, index - 1) repeat while (index2 < packet_length) pst.char(databuffer[index2++]) waitcnt(clkfreq / 10 + cnt) pst.newline pub get_char(p_str, n) '' Returns nth character from str at p_str if (n < strsize(p_str)) return byte[p_str][n] else return "?"
I am attaching the entire project as a zip file.
Thank you for your collaboration guys!!
Comments
You might consider running through the string replacing each comma as it is detected with a null ($00) to delimit it as the leftmost string at a "current string address" (see CLIB101 in the OBEX for a set of semi-standard C string manipulation functions), then decimalize it (see Simple Numbers also in the OBEX). Thereafter update the "current string address" by the length of the string last processed and repeat until out of data.
Not sophisticated but it will work.
Dear JonnyMac,
Thank you for your response, it was really helpful Let me show you my approach, I created a loop so that I can receive n amount of variables, as long as it is within 100 bytes. I am using two objects that contain methods for string manipulation, but when I save the values, I get the wrong results. Please check the code below, I am also attaching my entire project. Thank you for your help!
Hi JonnyMac,
I have been out because I was hospitalized due to a UTI, but I guess I am used to it since I am quadriplegic. I wanted to take the time to thank you for your code, I included it in my code and it worked perfectly, I have problems when I transfer float values, but it is ok. I shared my code with you not because I wanted you to correct it, but because I wanted to share my beginner's work. Now, I was finally capable of:
- Reading the infrared pulses from a power meter.
- Measure current, voltage, rms current, rms voltage and power factor of the 3 phases of the power meter (mcp3208 a2d converter for the sensors)
- Time stamp the data with a ds1307.
- Store values in a SD card.
- Finally send the data wirelessly thru a Zigbee API mesh network, and receive it in the coordinator side.
One again, thank you for your collaboration!!