This should make it to the next SDK, a SplitToArray method in our Util class.
The method takes a byte array containing comma separated values (CSV) or values separated by any mark, like ‘|’ or tab! Then the method decodes the values into a float array. So a source array containing “23.5,6.0.4” will result in 3 float values 23.5 and 6.0 and 0.4
Mainly, this was added to decode GPS data but this is perfect for many other purposes, like when transferring data from a PC to a NETMF device.
Any suggestions on improving this? Looking at code below, I decoded some GPS data without a single object allocation. GC will simply never run if you decode GPS this way and it will use MUCH less of the system resources.
using System;
using Microsoft.SPOT;
namespace GPSTester
{
public class Program
{
public static void Main()
{
// some GPS data ... I purposely broke the sentences to make it look like real data stream!
string gpss =
".3372,W,1,8,1.03,61.7,M,55.2,M,,*76" +
"$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76" +
"$GPGGA,092750.000,5321.6802,N";
byte[] gps = System.Text.UTF8Encoding.UTF8.GetBytes(gpss);//we are faking GPS stream for this test
byte[] CSVLine = new byte[256];//our buffer to help a line CSV comma separated values
float[] values = new float[100];
///////////////// GPS code //////////////////
// find the start and end of the line we care about
int lineStart = Array.IndexOf(gps, '$');
int firstComma = Array.IndexOf(gps, ',', lineStart);
firstComma++;//skip the comma
int lineEnd = Array.IndexOf(gps, '*', lineStart);
int length = lineEnd - firstComma;
Array.Copy(gps, firstComma, CSVLine, 0, length);
// "CSVLine" now holds a single line of the length "length"...looks like this
// 092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,
// GHI will add this
// a comma separated values CSV will be extracted and put in the float array
// in this example it use ',' but this can be anything!
int floatCount = GHI.Premium.System.Util.SplitToArray(CSVLine, length, ',', values);
/* from values above it will be
* values[0] = 092750.000;
* values[1] = 5321.6802;
* values[2] = (float)'N';
* ...etc.
*/
///////////////////////////////////////////////////
///debug test
/////show the byte array
string extracted = new string(System.Text.UTF8Encoding.UTF8.GetChars(CSVLine));
//show the decoded values
for (int i = 0; i < floatCount; i++)
{
Debug.Print(values[i].ToString());
}
Debug.Print(extracted);
System.Threading.Thread.Sleep(-1);
}
}
}