ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Interpreting some code in C (https://www.excelbanter.com/excel-programming/398076-interpreting-some-code-c.html)

Laurence Lombard

Interpreting some code in C
 

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 calculates the checksum
(It looks like the relevant section). The full code can be found he
http://gpsbabel.cvs.sourceforge.net/...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");
}
}
}
}




All times are GMT +1. The time now is 10:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com