odinhg Posted October 12, 2009 Report Share Posted October 12, 2009 (edited) Hi, I'm doing some serial communication with a device which sends a string in return. This string contains different values that I want to split into separate variables. The returned string can look something like this: 1: "1,HS,OK,12550,423,0.40,0,0\r" 2: "1,HS,OK,35000,568,1.32,11,14.4\r" So what I want to do is to get the values something like this: Int, String, String, Int, Int, Double, Int, Double I was thinking regexp is what I'm looking for, but unfortunately I'm not very good with regexp, and maybe there is some other ways which is more efficient. I would be very grateful for any tips to how to solve this! Thanks in advance Edited October 12, 2009 by odinhg Quote Link to comment
ShaunR Posted October 12, 2009 Report Share Posted October 12, 2009 Well. There are a few ways. If the message string is always in a fixed order and fixed length and messages are not concatenated, you can use the "Scan From String" which will give you the results straight away in the format you require. If its not, then you can use the "Spreadsheet String To Array" to break up the string at the delimiters then convert to whatever formats you like. 1 Quote Link to comment
odinhg Posted October 12, 2009 Author Report Share Posted October 12, 2009 Thanks for the quick reply ShaunR! The message string does always have the same number of values but its values aren't padded so the total length of the string can differ from time to time. Using "1,HS,%2s,%d,%d,%d,%d,%f" as format string did work when using the "Scan from string", except the last float, which just gave me an integer (for example 1.4 became 1 after the scan). When changing the 6th parameter to float, "1,HS,%2s,%d,%d,%f,%d,%f", I get an error saying that the input string wasn't in the expected format. The actual input string when the error message shows up is "1,HS,OK,0,21,0,0,1\r". Thanks again! Quote Link to comment
ShaunR Posted October 12, 2009 Report Share Posted October 12, 2009 Thanks for the quick reply ShaunR! The message string does always have the same number of values but its values aren't padded so the total length of the string can differ from time to time. Using "1,HS,%2s,%d,%d,%d,%d,%f" as format string did work when using the "Scan from string", except the last float, which just gave me an integer (for example 1.4 became 1 after the scan). When changing the 6th parameter to float, "1,HS,%2s,%d,%d,%f,%d,%f", I get an error saying that the input string wasn't in the expected format. The actual input string when the error message shows up is "1,HS,OK,0,21,0,0,1\r". Thanks again! Thats because the number of format specifiers have to be exactly the same as the fields you are trying to extract. From your last example, the last digit is 1. If you are viewing the value in a digital indicator and "hide trailing 0" is set, you will only see the decimal places IF the digits are non-zero. If thats not the reason and you want that to be 1.00 I would suggest using %.2f as the format specifier. Quote Link to comment
Ton Plomp Posted October 12, 2009 Report Share Posted October 12, 2009 Thats because the number of format specifiers have to be exactly the same as the fields you are trying to extract. From your last example, the last digit is 1. If you are viewing the value in a digital indicator and "hide trailing 0" is set, you will only see the decimal places IF the digits are non-zero. If thats not the reason and you want that to be 1.00 I would suggest using %.2f as the format specifier. It could also mean you have a different decimal sign ('.' vs ','). use %.; in the start of your formatter to specify a dot as the decimal sign. Ton 1 Quote Link to comment
odinhg Posted October 14, 2009 Author Report Share Posted October 14, 2009 I finally got it working. It's looks a little 'hacky' and it's most likely not the most efficient way to do it. But it's good enough for me atm. Thanks again Shaun and Ton Quote Link to comment
ShaunR Posted October 14, 2009 Report Share Posted October 14, 2009 I finally got it working. It's looks a little 'hacky' and it's most likely not the most efficient way to do it. But it's good enough for me atm. Thanks again Shaun and Ton Cool. Glad its working Looks like you've been through the string pallet and now know every function . (You can replace everything up to the "Array to Cluster" with a "Spreadsheet String To Array") Quote Link to comment
odinhg Posted October 14, 2009 Author Report Share Posted October 14, 2009 Yes, I learned some new things about the string manipulation functions now. And thanks for the tip, I'll try and see if I gain any performance in doing it that way. Quote Link to comment
smenjoulet Posted October 14, 2009 Report Share Posted October 14, 2009 I'm not trying to get anyone to change their implementation once they have it working, but I fail to see how the "top" is clearer and more efficient than the "bottom". Both give the same result. If this is always the format of your data (header, string, int, int, float, int, float) then the bottom "scan" should work fine. I do agree that you certainly get to know the string palette well. -Scott 1 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.