> ---- Rick T <[email protected]> wrote: 
> > 
> > #define STX         0x02
> > #define ETX         0x03
> 
> it is a function of type integer called calculate_lrc. There is an 
> input pointer to a string called packet.
> 
> > short int calculate_lrc(char *packet)
> > {
> 
> define variables
> >     char *lrc_ptr;
> >     short int lrc = 0;
> 
 
> assign the lrc_ptr (pointer) to the first character of the packet 
> string 
> 
> >     lrc_ptr = packet;
> > 
> 
> If the first character is a STX (start transmission x02, return with a 
> failure value of -1
> >     if( ! (lrc_ptr = strchr (lrc_ptr, STX )) )
> >         return (-1);
> 
 
The strchr function in ANSI C does this:

The  strchr() function returns a pointer to the first occurrence of the
       character c in the string s.

So, I take this to mean that if STX does not occur anywhere within the string 
pointed to by lrc_ptr, then the condition is met and -1 is returned.


> Add one to the pointer
> 
> >     ++lrc_ptr;
> 
 
> start a do while loop
> >     do {
> 
> assign the lrc variable to the current pointer address plus one 
> character
> 
> >         lrc ^= *lrc_ptr++;


This statement is actually doing this:

lrc is assigned the value of 
(lrc   BitwiseXOR   whatever is stored at the _current_ address of lrc_ptr).  

Because the ++ is after lrc_ptr, it is a post-increment.

So, the ++ increments the pointer _after_ it is referenced in the statment.  
You could break that statement into 2 parts to see what is really happening:

lrc ^= *lrc_ptr;
lrc_ptr ++;



> 
 
> check to see if the next character is the end of transmission 
> character ETX (x03)
> 
> >     } while (*lrc_ptr != ETX);
> 
> drop out the do while loop when the current character pointed to by 
> the lrc_ptr is ETX
> 
> assign the lrc variable (perhaps "last received character" with the 
> char 
> pointed to by the current pointer position

I think this is actually performing a Longitudinal Redundancy check (see 
http://en.wikipedia.org/wiki/Longitudinal_redundancy_check).  It states:

Set LRC = 0
       For each character c in the string
       do
           Set LRC = LRC XOR c
       end do

An 8-bit LRC such as this is equivalent to a cyclic redundancy check using the 
polynomial x8+1, but the independence of the bit streams is less clear when 
looked at that way.

Many protocols use such an XOR-based longitudinal redundancy check character, 
including the IEC 62056-21 standard for electrical meter reading, and the 
ACCESS.bus protocol.


> 
> >     lrc ^= *lrc_ptr;
> 

Once again, ^= means lrc = (lrc   BitwiseXOR   whatever is stored at the 
_current_ address of lrc_ptr).  

> return the character
> 
> >     return lrc;
 
> 
> > } 
> 
 
> So my guess after 25+ years of fooling with this sort of thing is 
> that this function takes a string from the comm port (or other 
> UART device), lops off the control codes, and sends the last 
> character in the string to the caller. I am rusty at this and without 
> 
> context, it is tricky to give you an exact answer.

It took me a while to step through this too.  I miss C, I haven't been able to 
use it in a project in ages.

 
> Of course, I could be completely out to lunch too. No pun intended 
> as I swallow my burger.
 
> I miss the old days are reading teletype and paper tape codes. I 
> remember writing C code to do something like this when I was 
> communicating 
> with a teletype device back when microcomputers came out. It was like 
> 
> magic.
 
> Got any COBOL you want translated. ;) Good luck, Roger
> --
> http://www.linkedin.com/pub/roger-austin/8/a4/60
> http://twitter.com/RogerTheGeek
> http://www.misshunt.com/ Home of the Clean/Dirty Magnet
> http://cfinnc.com/ ColdFusion Conference in North Carolina Oct 17-18


Best of luck too.  I started pumping out my CF example, but I won't have time 
to doublecheck it for accuracy yet.

One caveat to this whole thing is that in the code above, the C code is 
treating each character as a char (1 byte).  I have _no_ idea what CF will 
treat that character in the string as....1 byte, 2 bytes, etc.

Are you able to say what application you will be using this for?

Allen Souliere 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326280
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to