L
lombardm
I am trying to decipher/translate some code that I have no experience
with.
I am trying to find out how the checksum is computed for a Megellan
Explorist GPS Waypoint (POI) file. Below is the first 3 lines of a
typical
file. I understand everything except how the "a*23" at the end of the
line
is arrived at.
$PMGNFMT,%WPL,LAT,HEMI,LON,HEMI,ALT,UNIT,NAME,MSG,ICON,CHKSUM,
%META,ASCII
$PMGNWPL,3137.54374,S,01914.37622,E,0,M,WPT001,,a*23
$PMGNWPL,3122.18567,S,01906.50683,E,0,M,WPT002,,a*21
Below is an extract of the code for a program that generates lines
like the above and calculates the checksum
(It looks like the relevant section). The full code can be found here:
http://gpsbabel.cvs.sourceforge.net/*checkout*/gpsbabel/gpsbabel/magproto.c?revision=1.155
The code is in C of which I understand nothing. My only experience at
this
stage is Excel VBA. Can someone please explain in plain text (or VBA)
how
the checksum is calculated.
Many thanks
Laurence
/*
* Given a protocol message, compute the checksum as needed by
* the Magellan protocol.
*/
unsigned int
mag_checksum(const char * const buf)
{
int csum = 0;
const char *p;
for(p = buf; *p; p++) {
csum ^= *p;
}
return csum;
}
static unsigned int
mag_pchecksum(const char * const buf, int len)
{
int csum = 0;
const char *p = buf;
for (; len ; len--) {
csum ^= *p++;
}
return csum;
}
static void
mag_writemsg(const char * const buf)
{
unsigned int osum = mag_checksum(buf);
int retry_cnt = 5;
int i;
char obuf[1000];
if (debug_serial) {
warning("WRITE: $%s*%02X\r\n",buf, osum);
}
retry:
i = sprintf(obuf, "$%s*%02X\r\n",buf, osum);
termwrite(obuf, i);
if (magrxstate == mrs_handon || magrxstate == mrs_awaiting_ack) {
magrxstate = mrs_awaiting_ack;
mag_readmsg(trkdata);
if (last_rx_csum != osum) {
if (debug_serial) {
warning("COMM ERROR: Expected %02x, got %02x",
osum, last_rx_csum);
}
if (retry_cnt--)
goto retry;
else {
mag_handoff();
fatal(MYNAME
": Too many communication errors.\n");
}
}
}
}
with.
I am trying to find out how the checksum is computed for a Megellan
Explorist GPS Waypoint (POI) file. Below is the first 3 lines of a
typical
file. I understand everything except how the "a*23" at the end of the
line
is arrived at.
$PMGNFMT,%WPL,LAT,HEMI,LON,HEMI,ALT,UNIT,NAME,MSG,ICON,CHKSUM,
%META,ASCII
$PMGNWPL,3137.54374,S,01914.37622,E,0,M,WPT001,,a*23
$PMGNWPL,3122.18567,S,01906.50683,E,0,M,WPT002,,a*21
Below is an extract of the code for a program that generates lines
like the above and calculates the checksum
(It looks like the relevant section). The full code can be found here:
http://gpsbabel.cvs.sourceforge.net/*checkout*/gpsbabel/gpsbabel/magproto.c?revision=1.155
The code is in C of which I understand nothing. My only experience at
this
stage is Excel VBA. Can someone please explain in plain text (or VBA)
how
the checksum is calculated.
Many thanks
Laurence
/*
* Given a protocol message, compute the checksum as needed by
* the Magellan protocol.
*/
unsigned int
mag_checksum(const char * const buf)
{
int csum = 0;
const char *p;
for(p = buf; *p; p++) {
csum ^= *p;
}
return csum;
}
static unsigned int
mag_pchecksum(const char * const buf, int len)
{
int csum = 0;
const char *p = buf;
for (; len ; len--) {
csum ^= *p++;
}
return csum;
}
static void
mag_writemsg(const char * const buf)
{
unsigned int osum = mag_checksum(buf);
int retry_cnt = 5;
int i;
char obuf[1000];
if (debug_serial) {
warning("WRITE: $%s*%02X\r\n",buf, osum);
}
retry:
i = sprintf(obuf, "$%s*%02X\r\n",buf, osum);
termwrite(obuf, i);
if (magrxstate == mrs_handon || magrxstate == mrs_awaiting_ack) {
magrxstate = mrs_awaiting_ack;
mag_readmsg(trkdata);
if (last_rx_csum != osum) {
if (debug_serial) {
warning("COMM ERROR: Expected %02x, got %02x",
osum, last_rx_csum);
}
if (retry_cnt--)
goto retry;
else {
mag_handoff();
fatal(MYNAME
": Too many communication errors.\n");
}
}
}
}