Skip to content

Commit c1ffdf8

Browse files
committed
Example 6 & 7: Add parser to dump NMEA and RTCM messages as hex & ASCII
1 parent d7dfc0b commit c1ffdf8

File tree

2 files changed

+212
-7
lines changed

2 files changed

+212
-7
lines changed

examples/Example6_AverageBase/Example6_AverageBase.ino

+106-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ int pin_UART1_TX = 4;
2626
int pin_UART1_RX = 13;
2727

2828
#include <SparkFun_Unicore_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_Unicore_GNSS
29+
#include <SparkFun_Extensible_Message_Parser.h> //http://librarymanager/All#SparkFun_Extensible_Message_Parser
30+
31+
#define NMEA_PARSER_INDEX 0
32+
#define RTCM_PARSER_INDEX 1
33+
34+
// Build the table listing all of the parsers
35+
SEMP_PARSE_ROUTINE const parserTable[] =
36+
{
37+
sempNmeaPreamble,
38+
sempRtcmPreamble,
39+
};
40+
const int parserCount = sizeof(parserTable) / sizeof(parserTable[0]);
41+
42+
const char * const parserNames[] =
43+
{
44+
"NMEA Parser",
45+
"RTCM Parser",
46+
};
47+
const int parserNameCount = sizeof(parserNames) / sizeof(parserNames[0]);
48+
49+
SEMP_PARSE_STATE *parse; // State of the parsers
2950

3051
UM980 myGNSS;
3152

@@ -84,13 +105,94 @@ void setup()
84105

85106
myGNSS.saveConfiguration(); //Save the current configuration into non-volatile memory (NVM)
86107

87-
Serial.println("Output will be a mix of NMEA and binary RTCM non-visible characters");
108+
// Initialize the parser
109+
parse = sempBeginParser(parserTable, parserCount,
110+
parserNames, parserNameCount,
111+
0, 3000, processMessage, "Example 17 Parser");
112+
if (!parse)
113+
while (1)
114+
{
115+
Serial.println("HALTED: Failed to initialize the parser!");
116+
sleep(15);
117+
}
118+
119+
Serial.println("Output will be a mix of NMEA and binary RTCM");
88120
}
89121

90122
void loop()
91123
{
92-
//Read in NMEA from the UM980
93-
//RTCM is binary and will appear as random characters.
124+
// Read the raw data one byte at a time and update the parser state
125+
// based on the incoming byte
94126
while (SerialGNSS.available())
95-
Serial.write(SerialGNSS.read());
127+
{
128+
// Read the byte from the UM980
129+
uint8_t incoming = SerialGNSS.read();
130+
131+
// Parse this byte
132+
sempParseNextByte(parse, incoming);
133+
}
134+
}
135+
136+
// Call back from within parser, for end of message
137+
// Process a complete message incoming from parser
138+
void processMessage(SEMP_PARSE_STATE *parse, uint16_t type)
139+
{
140+
SEMP_SCRATCH_PAD *scratchPad = (SEMP_SCRATCH_PAD *)parse->scratchPad;
141+
char *typeName;
142+
143+
// Display the raw message
144+
// The type value is the index into the raw data array
145+
Serial.println();
146+
Serial.printf("Valid %s message: 0x%04x (%d) bytes\r\n",
147+
parserNames[type], parse->length, parse->length);
148+
dumpBuffer(parse->buffer, parse->length);
149+
}
150+
151+
// Display the contents of a buffer
152+
void dumpBuffer(const uint8_t *buffer, uint16_t length)
153+
{
154+
int bytes;
155+
const uint8_t *end;
156+
int index;
157+
char line[128];
158+
uint16_t offset;
159+
160+
end = &buffer[length];
161+
offset = 0;
162+
while (buffer < end)
163+
{
164+
// Determine the number of bytes to display on the line
165+
bytes = end - buffer;
166+
if (bytes > (16 - (offset & 0xf)))
167+
bytes = 16 - (offset & 0xf);
168+
169+
// Display the offset
170+
sprintf(line, "0x%08lx: ", offset);
171+
172+
// Skip leading bytes
173+
for (index = 0; index < (offset & 0xf); index++)
174+
sprintf(&line[strlen(line)], " ");
175+
176+
// Display the data bytes
177+
for (index = 0; index < bytes; index++)
178+
sprintf(&line[strlen(line)], "%02x ", buffer[index]);
179+
180+
// Separate the data bytes from the ASCII
181+
for (; index < (16 - (offset & 0xf)); index++)
182+
sprintf(&line[strlen(line)], " ");
183+
sprintf(&line[strlen(line)], " ");
184+
185+
// Skip leading bytes
186+
for (index = 0; index < (offset & 0xf); index++)
187+
sprintf(&line[strlen(line)], " ");
188+
189+
// Display the ASCII values
190+
for (index = 0; index < bytes; index++)
191+
sprintf(&line[strlen(line)], "%c", ((buffer[index] < ' ') || (buffer[index] >= 0x7f)) ? '.' : buffer[index]);
192+
Serial.println(line);
193+
194+
// Set the next line of data
195+
buffer += bytes;
196+
offset += bytes;
197+
}
96198
}

examples/Example7_FixedBase/Example7_FixedBase.ino

+106-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ int pin_UART1_TX = 4;
2525
int pin_UART1_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
29+
30+
#define NMEA_PARSER_INDEX 0
31+
#define RTCM_PARSER_INDEX 1
32+
33+
// Build the table listing all of the parsers
34+
SEMP_PARSE_ROUTINE const parserTable[] =
35+
{
36+
sempNmeaPreamble,
37+
sempRtcmPreamble,
38+
};
39+
const int parserCount = sizeof(parserTable) / sizeof(parserTable[0]);
40+
41+
const char * const parserNames[] =
42+
{
43+
"NMEA Parser",
44+
"RTCM Parser",
45+
};
46+
const int parserNameCount = sizeof(parserNames) / sizeof(parserNames[0]);
47+
48+
SEMP_PARSE_STATE *parse; // State of the parsers
2849

2950
UM980 myGNSS;
3051

@@ -82,12 +103,94 @@ void setup()
82103

83104
myGNSS.saveConfiguration(); //Save the current configuration into non-volatile memory (NVM)
84105

85-
Serial.println("Output will be a mix of NMEA and binary RTCM non-visible characters");
106+
// Initialize the parser
107+
parse = sempBeginParser(parserTable, parserCount,
108+
parserNames, parserNameCount,
109+
0, 3000, processMessage, "Example 17 Parser");
110+
if (!parse)
111+
while (1)
112+
{
113+
Serial.println("HALTED: Failed to initialize the parser!");
114+
sleep(15);
115+
}
116+
117+
Serial.println("Output will be a mix of NMEA and binary RTCM");
86118
}
87119

88120
void loop()
89121
{
90-
//Read in NMEA and RTCM from the UM980
122+
// Read the raw data one byte at a time and update the parser state
123+
// based on the incoming byte
91124
while (SerialGNSS.available())
92-
Serial.write(SerialGNSS.read());
125+
{
126+
// Read the byte from the UM980
127+
uint8_t incoming = SerialGNSS.read();
128+
129+
// Parse this byte
130+
sempParseNextByte(parse, incoming);
131+
}
132+
}
133+
134+
// Call back from within parser, for end of message
135+
// Process a complete message incoming from parser
136+
void processMessage(SEMP_PARSE_STATE *parse, uint16_t type)
137+
{
138+
SEMP_SCRATCH_PAD *scratchPad = (SEMP_SCRATCH_PAD *)parse->scratchPad;
139+
char *typeName;
140+
141+
// Display the raw message
142+
// The type value is the index into the raw data array
143+
Serial.println();
144+
Serial.printf("Valid %s message: 0x%04x (%d) bytes\r\n",
145+
parserNames[type], parse->length, parse->length);
146+
dumpBuffer(parse->buffer, parse->length);
147+
}
148+
149+
// Display the contents of a buffer
150+
void dumpBuffer(const uint8_t *buffer, uint16_t length)
151+
{
152+
int bytes;
153+
const uint8_t *end;
154+
int index;
155+
char line[128];
156+
uint16_t offset;
157+
158+
end = &buffer[length];
159+
offset = 0;
160+
while (buffer < end)
161+
{
162+
// Determine the number of bytes to display on the line
163+
bytes = end - buffer;
164+
if (bytes > (16 - (offset & 0xf)))
165+
bytes = 16 - (offset & 0xf);
166+
167+
// Display the offset
168+
sprintf(line, "0x%08lx: ", offset);
169+
170+
// Skip leading bytes
171+
for (index = 0; index < (offset & 0xf); index++)
172+
sprintf(&line[strlen(line)], " ");
173+
174+
// Display the data bytes
175+
for (index = 0; index < bytes; index++)
176+
sprintf(&line[strlen(line)], "%02x ", buffer[index]);
177+
178+
// Separate the data bytes from the ASCII
179+
for (; index < (16 - (offset & 0xf)); index++)
180+
sprintf(&line[strlen(line)], " ");
181+
sprintf(&line[strlen(line)], " ");
182+
183+
// Skip leading bytes
184+
for (index = 0; index < (offset & 0xf); index++)
185+
sprintf(&line[strlen(line)], " ");
186+
187+
// Display the ASCII values
188+
for (index = 0; index < bytes; index++)
189+
sprintf(&line[strlen(line)], "%c", ((buffer[index] < ' ') || (buffer[index] >= 0x7f)) ? '.' : buffer[index]);
190+
Serial.println(line);
191+
192+
// Set the next line of data
193+
buffer += bytes;
194+
offset += bytes;
195+
}
93196
}

0 commit comments

Comments
 (0)