1 /*----------------------------------------------------------------------------
2 Name: NmeaTest.cs
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Test cases for GPS NMEA reading from serial line
6 Author: "Marcel Ruff" <xmlBlaster@marcelruff.info>
7 Date: 01/2007
8 -----------------------------------------------------------------------------*/
9 using System;
10 using System.Collections.Generic;
11 using System.Text;
12 using NUnit.Framework;
13 using System.Reflection;
14 using System.Collections;
15
16 namespace org.xmlBlaster.client
17 {
18 [TestFixture]
19 public class NmeaTest
20 {
21 private StringBuilder buf = new StringBuilder(1024);
22 private static readonly string[] EMPTY_ARR = new string[0];
23
24 [Test]
25 public void CheckSerialInput()
26 {
27 string[] sentences = null;
28
29 sentences = getSentences(buf);
30 Assert.AreEqual(0, sentences.Length);
31
32 buf.Append("746.92418,N,910.024934,E,000.0,000.0,080107,,*39,blabdie sdkfh,,sdkf\r\n$GPRMC,095637.01,A,4743.230636,N,00903.623845,E,071.7,302.0,080107,,*35\r\n$GPGSV,3,1,12,1");
33 sentences = getSentences(buf);
34 Assert.AreEqual(1, sentences.Length);
35 Console.WriteLine("'" + sentences[0] + "'");
36 Assert.AreEqual("$GPRMC,095637.01,A,4743.230636,N,00903.623845,E,071.7,302.0,080107,,*35", sentences[0]);
37
38 sentences = getSentences(buf);
39 Assert.AreEqual(0, sentences.Length);
40
41 buf.Append("5,80,095,,16,63,206,31,03,53,095,,21,49,060,32*7C\r\n");
42 sentences = getSentences(buf);
43 Assert.AreEqual(1, sentences.Length);
44 Console.WriteLine("'" + sentences[0] + "'");
45 Assert.AreEqual("$GPGSV,3,1,12,15,80,095,,16,63,206,31,03,53,095,,21,49,060,32*7C", sentences[0]);
46
47 buf.Append("$GPGSV,3,1,12,15,80,095,,16,63,206,31,03,53,095,,21,49,060,32*7C");
48 sentences = getSentences(buf);
49 Assert.AreEqual(0, sentences.Length);
50
51 buf.Append("\r\n");
52 sentences = getSentences(buf);
53 Assert.AreEqual(1, sentences.Length);
54 Console.WriteLine("'" + sentences[0] + "'");
55 Assert.AreEqual("$GPGSV,3,1,12,15,80,095,,16,63,206,31,03,53,095,,21,49,060,32*7C", sentences[0]);
56
57 buf.Append(",16,63,206,31,03,53,095,,21,49,060,32*7C\r\n");
58 buf.Append("$GPGSV,3,1,12,15,80,095,,16,63,206,31,03,53,095,,21,49,060,32*7C\r\n");
59 buf.Append("$GPGSV,3,2,12,18,46,110,25,22,32,154,,19,24,286,24,07,18,105,*78\r\n");
60 buf.Append("$GPGSV,3,3,12,27,10,324,,29,06,038,,06,06,105,,26,05,050,*7D\r\n");
61 buf.Append("$GPGGA,123158.86,4746.922579,N,00910.0");
62 sentences = getSentences(buf);
63 Assert.AreEqual(3, sentences.Length);
64 for (int i = 0; i < sentences.Length; i++)
65 Console.WriteLine(i + ": '" + sentences[i] + "'");
66 Assert.AreEqual("$GPGSV,3,1,12,15,80,095,,16,63,206,31,03,53,095,,21,49,060,32*7C", sentences[0]);
67 Assert.AreEqual("$GPGSV,3,2,12,18,46,110,25,22,32,154,,19,24,286,24,07,18,105,*78", sentences[1]);
68 Assert.AreEqual("$GPGSV,3,3,12,27,10,324,,29,06,038,,06,06,105,,26,05,050,*7D", sentences[2]);
69 Console.WriteLine("DONE");
70 }
71
72 public string[] getSentences(StringBuilder buf)
73 {
74 int curr = 0;
75 ArrayList arrayList = new ArrayList();
76 string sentence;
77 while ((sentence = getSentence(buf, ref curr)) != null) {
78 arrayList.Add(sentence);
79 }
80 if (arrayList.Count == 0)
81 return EMPTY_ARR;
82 buf.Remove(0, curr);
83 return (string[])arrayList.ToArray(typeof( string ));
84 }
85
86 public string getSentence(StringBuilder buf, ref int curr) {
87 int start = GetIndexOf(buf, curr, '$');
88 if (start == -1) return null;
89 //int end = GetIndexOf(buf, 0, '\n');
90 //if (end == -1) return EMPTY_ARR;
91 //string sentence = buf.
92 StringBuilder sentence = new StringBuilder(256);
93 bool isComplete = false;
94 int origCurr = curr;
95 for (curr = start; curr < buf.Length; curr++)
96 {
97 if (buf[curr] == '\r')
98 continue; // ignore
99 if (buf[curr] == '\n') {
100 isComplete = true;
101 break;
102 }
103 sentence.Append(buf[curr]);
104 }
105 if (!isComplete)
106 {
107 curr = origCurr;
108 return null;
109 }
110 return sentence.ToString();
111 }
112
113 private int GetIndexOf(StringBuilder buf, int start, char chr)
114 {
115 if (start < 0) start = 0;
116 for (int i = start; i < buf.Length; i++)
117 if (buf[i] == chr)
118 return i;
119 return -1;
120 }
121
122 /*
123 static void Main(string[] argv)
124 {
125 NmeaTest n = new NmeaTest();
126 n.CheckSerialInput();
127 }
128 */
129 }
130
131 }
syntax highlighted by Code2HTML, v. 0.9.1