I’m running the example shown at https://www.ghielectronics.com/community/forum/topic?id=11534&page=1 with my attempt at using this utility tacked on at the end. I’m not getting the response I expect nor does it look like the GHI code gives the expected results.
using System;
using Microsoft.SPOT;
using System.Text;
namespace GHISplitToArray
{
public class Program
{
public static void Main()
{
Debug.Print("Program Started");
//Start of example from GHI Forum
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.Utilities.Arrays.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));
Debug.Print("GHI forum example: https: //www.ghielectronics.com/community/forum/topic?id=11534&page=1");
//show the decoded values
for (int i = 0; i < floatCount; i++)
{
Debug.Print(values[i].ToString());
}
Debug.Print(extracted);
//End of GHI example from forum
Debug.Print("\r\nNow my turn");
String stringArray = "1.0,2.0,3.5,5.0,10.123";
byte[] byteArray = UTF8Encoding.UTF8.GetBytes(stringArray.ToString());
int returnValue = -1;
float[] splitValues = new float[32];
returnValue = GHI.Utilities.Arrays.SplitToArray(byteArray, stringArray.Length, ',', splitValues);
StringBuilder inputBytes = new StringBuilder(128);
inputBytes.Clear();
inputBytes.Append(UTF8Encoding.UTF8.GetChars(byteArray));
Debug.Print("Input byte array (as characters): " + inputBytes.ToString());
Debug.Print("Return value = " + returnValue.ToString());
for (int i = 0; i < 5; i++)
Debug.Print(splitValues[1].ToString());
}
}
}
Here’s what shows up in the output window
Program Started
GHI forum example: https: //www.ghielectronics.com/community/forum/topic?id=11534&page=1
10.1230927
5321.68018
78
630.337219
87
1
8
1.02999997
61.7000008
77
55.2000008
77
nan
nan
092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,
Now my turn
Input byte array (as characters): 1.0,2.0,3.5,5.0,10.123
Return value = 4
2
2
2
2
2
The thread ‘’ (0x1) has exited with code 0 (0x0).
Done.
At the very least I’d expect the first value from the GHI code to be 92750.00.
What don’t I understand?