Khepera III Toolbox/The Toolbox/Modules/nmea

The nmea module parses a stream of NMEA 0183 messages.

Synopsis edit

// A hook to process NMEA messages
void process_message(struct sNMEAMessage *m, int withchecksum) {
    int i;
    printf("New '%s' message received\n", m->command);
    for (i = 0; i < m->argument_count; i++) {
        printf("  Argument %d: %s\n", i, m->argument[i]);
    }
}


// NMEA parser
struct sNMEAParser parser;

// Initialize a new parser
nmea_parser_init(&parser);

// Set the hook for received messages
parser.hook_process_message = process_message;

// Read and parse until there is nothing available any more
while (1) {
    len = read(filehandle, buffer, 128);  // Filehandle can be any open file or socket
    if (len < 1) {
        return;
    }
    nmea_parser_process_data(&parser, buffer, len);  // This calls the hook for each complete message received
}

Description edit

nmea_parser_init initializes a new NMEA parser. After initialization, the following hook functions can be set in the parser structure:

  • hook_process_message: Called whenever a complete and correct message is received.
  • hook_process_message_checksum_error: Called whenever a complete message with a wrong checksum is received. Such messages should be discarded.
  • hook_process_unrecognized_char: Called whenever a char which does not belong to a message is received.

Data must then be passed to nmea_parser_process_data, which parses the stream and calls the appropriate hook function.

Sending NMEA Messages edit

Sending NMEA messages is usually done by printing the corresponding values directly with printf. However, there may be situations in which you want to send an NMEA message structure as correctly formatted NMEA message. This is done as follows:

// The hook to send a message.
void hook_nmea_send(const unsigned char *buffer, int len) {
	write(filehandle, buffer, len);  // filehandle is the handle of an open file or socket
}

// An NMEA message
struct sNMEAMessage message;

// When initializing the parser, set the send hook:
...
parser.hook_send = hook_nmea_send;
...

// Initialize the message and fill it with the values
nmea_message_init(&message);
message->command = "TEST"
message->argument[0] = "1017";
message->argument[1] = "944";
message->argument_count = 2;

// Send the message
nmea_parser_send(&parser, &message);