/Users/brianlane/projs/aisparser/c/src/nmea.c File Reference


Detailed Description

NMEA 0183 Sentence Parser Module.

Author:
Copyright 2006-2008 by Brian C. Lane <bcl@brianlane.com>, All Rights Reserved
Version:
1.8
This module provides utility functions for handling NMEA 0183 data streams. The stream data is 8 bit ASCII with comma separated data fields. The functions in this module can be used to generate and verify the checksum, find the start of the sentence, find the next field, convert a field to an integer and copy a field to a destination buffer.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "portable.h"
#include "nmea.h"

Functions

char __stdcall ahextobin (char *c)
 Convert a single ASCII Hex digit (0-9 A-F or a-f) to a binary value.
char *__stdcall nmea_next_field (char *p)
 Return a pointer to the next field in the string.
unsigned int __stdcall nmea_uint (char *p)
 Convert the current field to an unsigned integer.
char *__stdcall nmea_copy_field (char *dest, char *src, int len)
 Copy len bytes from a field to a destination buffer.
char *__stdcall find_nmea_start (char *buffer)
 Find the start of a NMEA sentence '!' or '$' character.
unsigned int __stdcall nmea_checksum (char *buffer, unsigned char *checksum)
 Calculate the checksum of a NMEA 0183 sentence.
unsigned int __stdcall check_nmea_checksum (char *buffer, unsigned char *checksum)
 Check and return the checksum of a NMEA 0183 sentence.


Function Documentation

char __stdcall ahextobin ( char *  c  ) 

Convert a single ASCII Hex digit (0-9 A-F or a-f) to a binary value.

Parameters:
c ASCII character to convert
Returns:

Example:

    ahextobin( 'D' ) == 0x0D

unsigned int __stdcall check_nmea_checksum ( char *  buffer,
unsigned char *  checksum 
)

Check and return the checksum of a NMEA 0183 sentence.

Parameters:
buffer pointer to a 0 terminated buffer
checksum pointer to variable where checksum will be stored
Returns:

This function checks the checksum of a string and returns the result. The string is expected to start with a ! or $ and end after the 2 hex checksum characters. Trailing CR and/or LF are optional. The actual checksum is returned in the variable pointed to by checksum.

Example:

    char buf[255];
    unsigned char checksum;
    
    strcpy( (char *) buf, "!AIVDM,1,1,,A,15N;<J0P00Jro1<H>bAP0?vL00Rb,0*1B\n" );
    if( check_nmea_checksum( buf, &checksum ) != 0 )
    {
        fprintf( stderr, "Checksum failed\n" );
    }

    checksum == 0x1B

char* __stdcall find_nmea_start ( char *  buffer  ) 

Find the start of a NMEA sentence '!' or '$' character.

Parameters:
buffer pointer to a 0 terminated string buffer
Returns:

The NMEA sentence may not always start at the beginning of the buffer, use this routine to make sure the start of the sentence has been found.

Example:

    char buf[255];
    char *p;

    strcpy( (char *) buf, "678,4343,123,585*FF\n$GPGGA,32,121,676,29394,29234*AA\n" );
    p = find_nmea_start( buf ) );

    *p == "$GPGGA,..."

unsigned int __stdcall nmea_checksum ( char *  buffer,
unsigned char *  checksum 
)

Calculate the checksum of a NMEA 0183 sentence.

Parameters:
buffer pointer to a 0 terminated buffer
checksum pointer to variable where checksum will be stored
Returns:

This function will calculate the checksum of the string by skipping the ! or $ character and stopping at the * character or end of string.

Example:

    char buf[255];
    unsigned char checksum;
    
    strcpy( (char *) buf, "!AIVDM,1,1,,A,15N;<J0P00Jro1<H>bAP0?vL00Rb,0*1B\n" );
    nmea_checksum( buf, &checksum )

    checksum == 0x1B

char* __stdcall nmea_copy_field ( char *  dest,
char *  src,
int  len 
)

Copy len bytes from a field to a destination buffer.

Parameters:
dest pointer to destination buffer
src pointer to 0 terminated source buffer
len maximum number of characters to copy
Returns:

This routine stops copying when it hits the end of the field, end of the string or len.

Example:

    char buf1[255];
    char buf2[255];
    char *p;
    
    strcpy( buf1, "!AIVDM,1,1,,A,19NWoq000Wrt<RJHEuuqiWlN061d,0*5F" );
    buf2[0] = 0;
    
    p = buf1;
    nmea_copy_field( buf2, p, 254 );

    buf2 == buf1

char* __stdcall nmea_next_field ( char *  p  ) 

Return a pointer to the next field in the string.

Parameters:
p Pointer to the NMEA 0183 field
Returns:

Example:

    char buf[255];
    char *p;
    
    strcpy( buf, "!AIVDM,1,1,,A,19NWoq000Wrt<RJHEuuqiWlN061d,0*5F" );
    p = nmea_next_field( p );

    *p == '1'

unsigned int __stdcall nmea_uint ( char *  p  ) 

Convert the current field to an unsigned integer.

Parameters:
p Pointer to the NMEA 0183 field
Returns:

Example:

    char buf[255];
    char *p;
    unsigned int i;
    
    strcpy( buf, "!AIVDM,1,1,,A,19NWoq000Wrt<RJHEuuqiWlN061d,0*5F" );
    p = nmea_next_field( p );   
    i = nmea_uint( p );
    
    i == 1


Generated on Tue Aug 26 13:57:50 2008 for AIS Parser by  doxygen 1.5.2