473,769 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Translating c code help required

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,L AT,HEMI,LON,HEM I,ALT,UNIT,NAME ,MSG,ICON,CHKSU M,
%META,ASCII
$PMGNWPL,3137.5 4374,S,01914.37 622,E,0,M,WPT00 1,,a*23
$PMGNWPL,3122.1 8567,S,01906.50 683,E,0,M,WPT00 2,,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/...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(co nst char * const buf)
{
int csum = 0;
const char *p;

for(p = buf; *p; p++) {
csum ^= *p;
}

return csum;
}
static unsigned int
mag_pchecksum(c onst char * const buf, int len)
{
int csum = 0;
const char *p = buf;
for (; len ; len--) {
csum ^= *p++;
}
return csum;
}

static void
mag_writemsg(co nst char * const buf)
{
unsigned int osum = mag_checksum(bu f);
int retry_cnt = 5;
int i;
char obuf[1000];

if (debug_serial) {
warning("WRITE: $%s*%02X\r\n",b uf, osum);
}

retry:

i = sprintf(obuf, "$%s*%02X\r\n", buf, osum);
termwrite(obuf, i);
if (magrxstate == mrs_handon || magrxstate == mrs_awaiting_ac k) {
magrxstate = mrs_awaiting_ac k;
mag_readmsg(trk data);
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");
}
}
}
}

Sep 26 '07 #1
9 1945
In article <11************ **********@57g2 000hsv.googlegr oups.com>,
lombardm <lo******@mweb. co.zawrote:
>I understand everything except how the "a*23" at the end of the
line
is arrived at.
>$PMGNWPL,3137. 54374,S,01914.3 7622,E,0,M,WPT0 01,,a*23
>Can someone please explain in plain text (or VBA) how
the checksum is calculated.
int csum = 0;
Start the checksum at 0.
const char *p;
for(p = buf; *p; p++) {
Loop over every character in the buffer until you get to the end
of the string (which is marked by a character which is binary 0)
csum ^= *p;
xor each character into the checksum.

Because xor never creates new "on" bits where everything used
to be "off", this the result will never be more than one character
wide. That one character is later printed in hexadecimal.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Sep 26 '07 #2
On Tue, 25 Sep 2007 23:01:09 -0700, lombardm wrote:
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,L AT,HEMI,LON,HEM I,ALT,UNIT,NAME ,MSG,ICON,CHKSU M,
%META,ASCII
$PMGNWPL,3137.5 4374,S,01914.37 622,E,0,M,WPT00 1,,a*23
$PMGNWPL,3122.1 8567,S,01906.50 683,E,0,M,WPT00 2,,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/...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(co nst char * const buf)
{
int csum = 0;
const char *p;

for(p = buf; *p; p++) {
csum ^= *p;
}

return csum;
}
This is the bitwise exclusive-or of the values of characters in
buf.
static unsigned int
mag_pchecksum(c onst char * const buf, int len)
{
int csum = 0;
const char *p = buf;
for (; len ; len--) {
(I don't get why on earth len is signed; what happens if is
negative? What's wrong with while (len--) where len is a size_t?
csum ^= *p++;
}
return csum;
Ditto as above, but it stops after len characters instead of at
the first null.
}

static void
mag_writemsg(co nst char * const buf)
{
unsigned int osum = mag_checksum(bu f);
int retry_cnt = 5;
int i;
char obuf[1000];

if (debug_serial) {
warning("WRITE: $%s*%02X\r\n",b uf, osum);
}

retry:

i = sprintf(obuf, "$%s*%02X\r\n", buf, osum);
It writes "$", the contents of buf, osum as two hex digits, a
carriage return and a line feed to obuf.
termwrite(obuf, i);
I guess it writes obuf somewhere.
if (magrxstate == mrs_handon || magrxstate == mrs_awaiting_ac k) {
Without knowing what these are I can't help...
magrxstate = mrs_awaiting_ac k;
mag_readmsg(trk data);
if (last_rx_csum != osum) {
if (debug_serial) {
warning("COMM ERROR: Expected %02x, got %02x",
osum, last_rx_csum);
}
if (retry_cnt--)
goto retry;
(*cough cough* Hasn't anybody told them 'bout do {} while()? )
else {
mag_handoff();
fatal(MYNAME
": Too many communication errors.\n");
}
}
}
}
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 26 '07 #3
Sorry folks - I know NOTHING about C and all your replies are Greek to
me.

I hoping for a bit of pseudo VBA code like the following (in my
example code the Ascii cvalues of the individual characters of
InputLine are added together and converted to hexadecimal)

Checksum = 0
For i = 1 to length(Inputlin e)
Checksum = checksum + Chr(Mid(InputLi ne(i,1))
Next i
HChecksum = Hex(Checksum)

Thanks
Laurence
Army1987 wrote:
On Tue, 25 Sep 2007 23:01:09 -0700, lombardm wrote:
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,L AT,HEMI,LON,HEM I,ALT,UNIT,NAME ,MSG,ICON,CHKSU M,
%META,ASCII
$PMGNWPL,3137.5 4374,S,01914.37 622,E,0,M,WPT00 1,,a*23
$PMGNWPL,3122.1 8567,S,01906.50 683,E,0,M,WPT00 2,,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/...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(co nst char * const buf)
{
int csum = 0;
const char *p;

for(p = buf; *p; p++) {
csum ^= *p;
}

return csum;
}
This is the bitwise exclusive-or of the values of characters in
buf.
static unsigned int
mag_pchecksum(c onst char * const buf, int len)
{
int csum = 0;
const char *p = buf;
for (; len ; len--) {
(I don't get why on earth len is signed; what happens if is
negative? What's wrong with while (len--) where len is a size_t?
csum ^= *p++;
}
return csum;
Ditto as above, but it stops after len characters instead of at
the first null.
}

static void
mag_writemsg(co nst char * const buf)
{
unsigned int osum = mag_checksum(bu f);
int retry_cnt = 5;
int i;
char obuf[1000];

if (debug_serial) {
warning("WRITE: $%s*%02X\r\n",b uf, osum);
}

retry:

i = sprintf(obuf, "$%s*%02X\r\n", buf, osum);
It writes "$", the contents of buf, osum as two hex digits, a
carriage return and a line feed to obuf.
termwrite(obuf, i);
I guess it writes obuf somewhere.
if (magrxstate == mrs_handon || magrxstate == mrs_awaiting_ac k) {
Without knowing what these are I can't help...
magrxstate = mrs_awaiting_ac k;
mag_readmsg(trk data);
if (last_rx_csum != osum) {
if (debug_serial) {
warning("COMM ERROR: Expected %02x, got %02x",
osum, last_rx_csum);
}
if (retry_cnt--)
goto retry;
(*cough cough* Hasn't anybody told them 'bout do {} while()? )
else {
mag_handoff();
fatal(MYNAME
": Too many communication errors.\n");
}
}
}
}

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.
Sep 26 '07 #4
lombardm <lo******@mweb. co.zawrites:
Army1987 wrote:
>On Tue, 25 Sep 2007 23:01:09 -0700, lombardm wrote:
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,L AT,HEMI,LON,HEM I,ALT,UNIT,NAME ,MSG,ICON,CHKSU M,
%META,ASCII
$PMGNWPL,3137.5 4374,S,01914.37 622,E,0,M,WPT00 1,,a*23
$PMGNWPL,3122.1 8567,S,01906.50 683,E,0,M,WPT00 2,,a*21

Below is an extract of the code for a program that generates lines
like the above and calculates the checksum
<snip>
unsigned int
mag_checksum(co nst char * const buf)
{
int csum = 0;
const char *p;

for(p = buf; *p; p++) {
csum ^= *p;
}

return csum;
}
This is the bitwise exclusive-or of the values of characters in
buf.
<snip>
>
Sorry folks - I know NOTHING about C and all your replies are Greek to
me.
Please don't top post. You should interleave you reply and trim any
material not required.
I hoping for a bit of pseudo VBA code like the following (in my
example code the Ascii cvalues of the individual characters of
InputLine are added together and converted to hexadecimal)
The reply you got said it in English. Surely you can turn that into
VBA? If not, then this is not the right place to ask. Take the
snipped of C and the line that says what it does ("exclusive or" is the
key bit) and post that to a VBA group. They can then do the translation.

.... although it is possible that:
Checksum = 0
For i = 1 to length(Inputlin e)
Checksum = checksum + Chr(Mid(InputLi ne(i,1))
replace with:
Checksum = checksum Xor Chr(Mid(InputLi ne(i,1))
Next i
HChecksum = Hex(Checksum)
does it.

--
Ben.
Sep 26 '07 #5
Please don't top post. You should interleave you reply and trim any
material not required.
Thanks for your reply. My apologies for not adhering to protocol.
Laurence

Sep 26 '07 #6
Walter Roberson wrote:
lombardm <lo******@mweb. co.zawrote:
.... snip ...
>
>Can someone please explain in plain text (or VBA) how
the checksum is calculated.

Start the checksum at 0.
>const char *p;
>for(p = buf; *p; p++) {

Loop over every character in the buffer until you get to the end
of the string (which is marked by a character which is binary 0)
> csum ^= *p;

xor each character into the checksum.

Because xor never creates new "on" bits where everything used
to be "off", this the result will never be more than one character
wide. That one character is later printed in hexadecimal.
Or other mechanisms.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 26 '07 #7
lombardm wrote:
>
Sorry folks - I know NOTHING about C and all your replies are
Greek to me.

I hoping for a bit of pseudo VBA code like the following (in my
example code the Ascii cvalues of the individual characters of
InputLine are added together and converted to hexadecimal)
Don't top-post. That has lost all the history. So I (and others)
have no idea what the thread is really about.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 26 '07 #8
lombardm wrote:
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,L AT,HEMI,LON,HEM I,ALT,UNIT,NAME ,MSG,ICON,CHKSU M,
%META,ASCII
$PMGNWPL,3137.5 4374,S,01914.37 622,E,0,M,WPT00 1,,a*23
$PMGNWPL,3122.1 8567,S,01906.50 683,E,0,M,WPT00 2,,a*21
These are NMEA 0183 messages. If you do a web search for NMEA checksum
VB you should find examples how to calculate them using VB.

Kevin.
Sep 26 '07 #9

Kevin Bagust wrote:
lombardm wrote:
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,L AT,HEMI,LON,HEM I,ALT,UNIT,NAME ,MSG,ICON,CHKSU M,
%META,ASCII
$PMGNWPL,3137.5 4374,S,01914.37 622,E,0,M,WPT00 1,,a*23
$PMGNWPL,3122.1 8567,S,01906.50 683,E,0,M,WPT00 2,,a*21

These are NMEA 0183 messages. If you do a web search for NMEA checksum
VB you should find examples how to calculate them using VB.
Thank you very much - this is exactly what I am looking for - found
the solution right away and it works. Thought the information was out
there - just need to find it!
Thanks once again
>
Kevin.
Sep 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1659
by: Charles Law | last post by:
Hi guys A bit of curve ball here ... I have a document (Word) that contains a series of instructions in sections and subsections (and sub-subsections). There are 350 pages of them. I need to translate these instructions into something that can be processed automatically, so I have used the Command pattern to set up a set of commands that correspond to the various instructions in the document.
3
1547
by: Idris Farooqi | last post by:
Could someone please tell me, how easy or difficult , it is to transfer code written in C++ to C#. I wrote a program for FTP ( in C++), and need to translate that code in C#. As a beginner of C# I'm not sure how much work is required, and I wanted to get a feel of it before I started. If someone could advise. Thanks
5
5508
by: Jonathan Seidner | last post by:
Hi, I have a program which contains a textbox, in this text box the user types this string: "\ub7c6\ub9d1\ubbd9\ubdc8\ubfcb\uc1b2\uc3a7\uc586\uc7a9\uc9b0\ucbfb" this is NOT a unicode string because the textbox obviously wraps the text so it does not include any escape characters, if i were to put it in code of the program like this: string s = "\ub7c6\ub9d1\ubbd9" it would have represented a unicode text.
3
1855
by: Gabe Covert | last post by:
I'm a new C# developer, and am developing an application which will utilize a COM library from a third party. I have two following SDK calls from the 3rd-party SDK which I can't get to work under C#: SDK declaration: VARIANT_BOOL ReadMemory(unsigned char* Data, long DataSize); tlbimp result:
0
1121
by: Dylan Phillips | last post by:
I'm interested in how other participants in this new group are implementing SQL Full-Text Search on their Web Sites. How are you translating the user search string: "DirectX managed code" into the contains statement syntax ' "DirectX" AND "managed" AND "code" '? I started writing a query parsers as an intermediary to handle control characters like , but frankly it's becoming a rather pain in the a$$. If anyone has any suggestions as...
6
2533
by: Jumping Matt Flash | last post by:
The code i'm writing is using VB .NET and is for a web service returning a dataset, which is in turn to be used by an ASP .NET application displaying a datagrid. I'm currently populating a datagrid using a "select top 1 column 1, column2, column3 from tblTable" statement. As there will only ever be one row returned I want to be able to to switch the column names to be row1 and the column values to be row 2 i.e. select house, street,...
13
2052
by: marcwentink | last post by:
Dear people, The code below is compiling: #define BUF_SZ 16383 ..... strcat(ConnectString, "Content-Length: BUF_SZ\n"); but it does not work since it give me:
1
1867
by: timn | last post by:
Translating Access SQL queries into SQL subqueries. -------------------------------------------------------------------------------- I have a query in Access that uses a subquery, I would like some help on translating it into SQL as a subquery so I can use it in an application. the queries in Access are the subquery... (query4) SELECT Movement.DateTime, Movement.StudentID, Movement.Subject, Movement.Teacher, Movement.Destination,...
1
2095
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: Can Some body help me translating the following code from classic Vb to Vb.Net Option Explicit Private Sub Command1_Click() On Error GoTo command_error
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.