2121 Connect a dual or triband GNSS antenna: https://www.sparkfun.com/products/21801
2222*/
2323
24- int pin_UART1_TX = 4 ;
25- int pin_UART1_RX = 13 ;
24+ int pin_UART_TX = 4 ;
25+ int pin_UART_RX = 13 ;
2626
2727#include < SparkFun_Unicore_GNSS_Arduino_Library.h> // http://librarymanager/All#SparkFun_Unicore_GNSS
28+ #include < SparkFun_Extensible_Message_Parser.h> // http://librarymanager/All#SparkFun_Extensible_Message_Parser
2829
30+ // ----------------------------------------
31+ // Constants
32+ // ----------------------------------------
33+
34+ // Build the table listing all of the parsers
35+ SEMP_PARSE_ROUTINE const parserTable[] =
36+ {
37+ sempRtcmPreamble
38+ };
39+ const int parserCount = sizeof (parserTable) / sizeof (parserTable[0 ]);
40+
41+ const char * const parserNames[] =
42+ {
43+ " RTCM parser"
44+ };
45+ const int parserNameCount = sizeof (parserNames) / sizeof (parserNames[0 ]);
46+
47+ // ----------------------------------------
48+ // Locals
49+ // ----------------------------------------
50+
51+ SEMP_PARSE_STATE *parse;
2952UM980 myGNSS;
3053
3154HardwareSerial SerialGNSS (1 ); // Use UART1 on the ESP32
@@ -37,8 +60,15 @@ void setup()
3760 Serial.println ();
3861 Serial.println (" SparkFun UM980 Example" );
3962
63+ // Initialize the parser
64+ parse = sempInitParser (parserTable, parserCount,
65+ parserNames, parserNameCount,
66+ 0 , 3000 , processMessage, " RTCM_Test" );
67+ if (!parse)
68+ reportFatalError (" Failed to initialize the parser" );
69+
4070 // We must start the serial port before using it in the library
41- SerialGNSS.begin (115200 , SERIAL_8N1, pin_UART1_RX, pin_UART1_TX );
71+ SerialGNSS.begin (115200 , SERIAL_8N1, pin_UART_RX, pin_UART_TX );
4272
4373 myGNSS.enableDebugging (); // Print all debug to Serial
4474
@@ -62,11 +92,99 @@ void setup()
6292
6393 myGNSS.saveConfiguration (); // Save the current configuration into non-volatile memory (NVM)
6494
65- Serial.println (" RTCM is in binary so printing it will show random characters " );
95+ Serial.println (" RTCM messages are dumped in HEX if the CRC is correct " );
6696}
6797
6898void loop ()
6999{
70100 while (SerialGNSS.available ())
71- Serial.write (SerialGNSS.read ());
72- }
101+ // Update the parser state based on the incoming byte
102+ sempParseNextByte (parse, SerialGNSS.read ());
103+ }
104+
105+ // Output a line of text for the SparkFun Extensible Message Parser
106+ void sempExtPrintLineOfText (const char *string)
107+ {
108+ Serial.println (string);
109+ }
110+
111+ // Call back from within parser, for end of message
112+ // Process a complete message incoming from parser
113+ void processMessage (SEMP_PARSE_STATE *parse, uint8_t type)
114+ {
115+ SEMP_SCRATCH_PAD *scratchPad = (SEMP_SCRATCH_PAD *)parse->scratchPad ;
116+ static bool displayOnce = true ;
117+
118+ // Display the raw message
119+ Serial.println ();
120+ Serial.printf (" Valid RTCM message: 0x%04x (%d) bytes\r\n " , parse->length , parse->length );
121+ ex5DumpBuffer (parse->buffer , parse->length );
122+
123+ // Display the parser state
124+ if (displayOnce)
125+ {
126+ displayOnce = false ;
127+ Serial.println ();
128+ sempPrintParserConfiguration (parse);
129+ }
130+ }
131+
132+ // Display the contents of a buffer
133+ void ex5DumpBuffer (const uint8_t *buffer, uint16_t length)
134+ {
135+ int bytes;
136+ const uint8_t *end;
137+ int index;
138+ uint16_t offset;
139+
140+ end = &buffer[length];
141+ offset = 0 ;
142+ while (buffer < end)
143+ {
144+ // Determine the number of bytes to display on the line
145+ bytes = end - buffer;
146+ if (bytes > (16 - (offset & 0xf )))
147+ bytes = 16 - (offset & 0xf );
148+
149+ // Display the offset
150+ Serial.printf (" 0x%08lx: " , offset);
151+
152+ // Skip leading bytes
153+ for (index = 0 ; index < (offset & 0xf ); index++)
154+ Serial.printf (" " );
155+
156+ // Display the data bytes
157+ for (index = 0 ; index < bytes; index++)
158+ Serial.printf (" %02x " , buffer[index]);
159+
160+ // Separate the data bytes from the ASCII
161+ for (; index < (16 - (offset & 0xf )); index++)
162+ Serial.printf (" " );
163+ Serial.printf (" " );
164+
165+ // Skip leading bytes
166+ for (index = 0 ; index < (offset & 0xf ); index++)
167+ Serial.printf (" " );
168+
169+ // Display the ASCII values
170+ for (index = 0 ; index < bytes; index++)
171+ Serial.printf (" %c" , ((buffer[index] < ' ' ) || (buffer[index] >= 0x7f )) ? ' .' : buffer[index]);
172+ Serial.printf (" \r\n " );
173+
174+ // Set the next line of data
175+ buffer += bytes;
176+ offset += bytes;
177+ }
178+ }
179+
180+ // Print the error message every 15 seconds
181+ void reportFatalError (const char *errorMsg)
182+ {
183+ while (1 )
184+ {
185+ Serial.print (" HALTED: " );
186+ Serial.print (errorMsg);
187+ Serial.println ();
188+ sleep (15 );
189+ }
190+ }
0 commit comments