:: Home :: Code Snippets :: Drivers :: Projects :: About ::
 Main Menu
  Home
  Code Snippets
  About

 Open Drivers
 PIC32 mcompat - PIC32 mcompat
 _build - Embedded build information
 qTask - Queued Task Manager
 EA-DOGM - LCD Display Driver

 Open Projects
 oLogic - Oscilloscope Logic Viewer
 oLogic 1.4 - NEW Logic Viewer
 SSX32 - Serial Servo Driver



 Sources
Prototyping
  PicStuff
  Sparkfun

Equipment
  Saelig
  Tequipment

Supply House
  Jameco
  Mouser
  Digikey
  Allied Electronics
  Clearwater Technologies

Miscellaneous
  BCM
  N34D Blog

NMEA Checksums - NMEA Checksums, Validate and Generate

This will allow you to validate a strings checksum, as well as allow you to generate your own checksums.

 Code Snippet
/vhost/mculabs/snippet_db/nmea_checksum/nmea_chk.c
    1 /*
    2           file: nmea_chk.c
    3        project: open functions
    4    description: NMEA checksum validate and generate
    5       compiler: CCS C
    6    written by : Michael Bradley
    7 
    8 
    9   2 Functions:
   10   ------------------------------------------------------
   11   nmea_validateChecksum(char *strPtr);
   12     pass a string to validate data against checksum
   13     returns either TRUE (1) or FALSE (0)
   14 
   15   nmea_generateChecksum(char *strPtr);
   16     pass a complete user generated string that does not contain '*xx'
   17     returns binary byte that is the checksum, you must convert to hex.
   18 
   19 	simple way to print it out:
   20 	int8 chk;
   21 	chk = nmea_generateChecksum(origString);
   22 	printf("%s*%2X\r\n",origString,chk);
   23 
   24 */
   25 
   26 
   27 
   28 // this takes a nmea gps string, and validates it againts the checksum
   29 // at the end if the string. (requires first byte to be $)
   30 int8 nmea_validateChecksum(char *strPtr)
   31 {
   32    int p;
   33    char c;
   34    unsigned int8 chksum;
   35    unsigned int8 nmeaChk;
   36    int8 flagValid;
   37    char hx[5] = "0x00";
   38 
   39    flagValid = TRUE; // we start true, and make it false if things are not right
   40 
   41    if (strPtr[0] != '$' ) { flagValid = FALSE; }
   42 
   43    // if we are still good, test all bytes
   44    if (flagValid == TRUE)
   45      {
   46      c = strPtr[1]; // get first chr
   47      chksum = c;
   48      p = 2;
   49      while ( ( c != '*' ) && ( p < SERIAL_STR_SIZE ) )
   50        {
   51        c = strPtr[p]; // get next chr
   52        if ( c != '*' ) { chksum = chksum ^ c; }
   53        p++;
   54        }
   55      // at this point we are either at * or at end of string
   56      hx[2] = strPtr[p];
   57      hx[3] = strPtr[p+1];
   58      hx[4] = 0x00;
   59      nmeaChk = atoi(hx);
   60      if ( chksum != nmeaChk ) { flagValid = FALSE; }
   61      }
   62 
   63    return flagValid;
   64 }
   65 
   66 
   67 // this returns a single binary byte that is the checksum
   68 // you must convert it to hex if you are going to print it or send it
   69 unsigned int8 nmea_generateChecksum(char *strPtr)
   70 {
   71    int p;
   72    char c;
   73    unsigned int8 chksum;
   74 
   75    c = strPtr[0]; // get first chr
   76    chksum = c;
   77    p = 1;
   78    while ( c != 0x00 )
   79      {
   80      c = strPtr[p]; // get next chr
   81      if ( c != 0x00 ) { chksum = chksum ^ c; }
   82      p++;
   83      }
   84 
   85    return chksum;
   86 }
   87 






:: Home :: Resources :: PIC Microcontrollers ::
:: 10F :: 12F :: 16F :: 18F :: 24F :: 24H :: 30F :: 33F ::
(C) Copyright 2009 mculabs.com - contact: info@mculabs.com
0.03876805305481