code explanation 
Author Message
 code explanation

Hi,

I was wondering if someone could explain this code for me.

LRCRES = 0
LRC = 0
For I = 2 To (Len(Header$) - 1)
LRC = LRC Xor Asc(Mid$(Header$, I, 1))
Next I
LRCRES = Hex$(LRC)
If Len(LRCRES) < 2 Then LRCRES = "0" + LRCRES

The end result of this code is a Longitudinal Redundancy Check.  I know how
to manually calculate the LRC.  But this code calculates it automatically
and I was wondering how it works.  I am pretty new to all of this so any
information would be greatly appreciated.

Thanks



Mon, 09 May 2005 07:26:25 GMT  
 code explanation

Quote:
> Hi,

> I was wondering if someone could explain this code for me.

> The end result of this code is a Longitudinal Redundancy Check.  I know how
> to manually calculate the LRC.

Can you outline how to do it manually?  Perhaps someone can then take what you
know and relate it to the code because, what the code is doing is fairly straight
forward.  It is Xor'ing the ASCII value of every letter in the Header string.

LFS



Mon, 09 May 2005 09:00:03 GMT  
 code explanation

Quote:
> For I = 2 To (Len(Header$) - 1)
> LRC = LRC Xor Asc(Mid$(Header$, I, 1))
> Next I

Oops.  I should have copied the code, it doesn't include the first
or last characters....

<g>
LFS



Mon, 09 May 2005 09:02:05 GMT  
 code explanation
Actually you just helped me a lot.  I guess what i'm looking for is a line
by line explanation.  For some reason I can't completely grasp this code.
To find the LRC manually you take the hex ascii value of each byte.  Convert
to binary.  then Exclusive OR the binary value and convert to hex ascii.
IE.

30 34 30 31 34 31 30 31 30 31 39 30 30 31
each value is converted to binary ie 30 = 00110000
the total number of 1's are added up for each bit ie B4 = 14 (this is for
the 4th bit of all the values)
Then its Exclusive Or'd.
Then converted to hex ascii.

Thanks again for the great help.

--
Confidentiality Note: The information contained in this email and document
(s) attached are for the exclusive use of the addressee and may contain
confidential, privileged and non-disclosable information. If the recipient
of this email is not the addressee, such recipient is strictly prohibited
from reading, photocopying, distributing or otherwise using this email or
its contents in any way.

Quote:

> > Hi,

> > I was wondering if someone could explain this code for me.

> > The end result of this code is a Longitudinal Redundancy Check.  I know
how
> > to manually calculate the LRC.

> Can you outline how to do it manually?  Perhaps someone can then take what
you
> know and relate it to the code because, what the code is doing is fairly
straight
> forward.  It is Xor'ing the ASCII value of every letter in the Header
string.

> LFS



Mon, 09 May 2005 09:15:30 GMT  
 code explanation

Quote:
> Actually you just helped me a lot.  I guess what i'm looking for is a line
> by line explanation.  For some reason I can't completely grasp this code.
> To find the LRC manually you take the hex ascii value of each byte.  Convert
> to binary.  then Exclusive OR the binary value and convert to hex ascii.
> IE.

> 30 34 30 31 34 31 30 31 30 31 39 30 30 31
> each value is converted to binary ie 30 = 00110000
> the total number of 1's are added up for each bit ie B4 = 14 (this is for
> the 4th bit of all the values)
> Then its Exclusive Or'd.
> Then converted to hex ascii.

> Thanks again for the great help.

Your outline needs a bit more clarification.  You say the bits are totaled,
yet I don't see that in the code.  But the difficult part is this statement:

Quote:
> Then its Exclusive Or'd.

XOR is an operator, it needs two values to operate on.  What is XOR'ed with what?

Assuming you know how a For/Next loop works, and what the assignment operator
does (A = 5)  the only complicated part is the one line inside the loop.  To help
explain it, I broke it into two different commands:

' Assignments
LRCRES = 0
LRC = 0

' Loops from 2 to length of Header - 1 (omits first and last characters)
For I = 2 To (Len(Header$) - 1)

' Simplified for explanation
' Assigns the ASCII value of the letter at position I
CHAR = Asc(Mid$(Header$, I, 1))

' XOR operation assigned back to operand
LRC = LRC Xor CHAR

' End of loop
Next I

' Convert to Hexidecimal string
LRCRES = Hex$(LRC)

' Adds a leading 0 if less than 2 characters.
If Len(LRCRES) < 2 Then LRCRES = "0" + LRCRES

A value is a value, whether you look at it in Base 10 (decimal), base 16 (hexidecimal)
or base 2 (binary).  That is why you would not need to convert it to do any type
of operation on it.  The time you need to convert it is when you want it displayed,
that is why you see no conversion to binary.  I assume, from looking at the code, that
LRCRES will be used to display the resulting value because that variable is assigned
a (hexidecimal) string value, suitable for printing to the screen or a file.

HTH
LFS



Mon, 09 May 2005 12:38:16 GMT  
 code explanation
http://www.methaneblower.com/lrc.htm

This link is to a page from the manual that describes how to calculate the
LRC.  And the full code of the working example is below. The code below
communicates out the comm port to a controller and reads a memory address.

This code is from the Module:

Option Explicit
Public Settings$, SlaveAddr$, Datatype$, Dataaddr$, Completeblk$, Partblk$,
Slaveenq$
Public Header$, Masteraddr$, Nenq$, Enq$, Ack$, Soh$, Stx$, Etb$, Nak$,
Etx$, Eot$
Public Red$, Dta$, Offsetaddr$, Result$, Enqres1$, Enqres2$, Enqres3$,
Looping, FirstEOT$
Public Hdrresponse$, Hdrres1$, Hdrres2$, Hdrres3$, Hdrres4$, Hdrres5$,
Hdrres6$, Hdrres7$
Public LRCRES, LRC, I, LoopCount

Public Sub Build_String()

Header$ = 0
Datatype$ = Chr$(&H37)
Dataaddr$ = Chr$(&H30) + Chr$(&H34) + Chr$(&H30) + Chr$(&H45)
Completeblk$ = Chr$(&H30) + Chr$(&H30)
Partblk$ = Chr$(&H30) + Chr$(&H34)
Masteraddr$ = Chr$(&H30) + Chr$(&H30)
Nenq$ = Chr$(&H4E)
If Len(SlaveAddr$) < 2 Then SlaveAddr$ = "0" + SlaveAddr$
Offsetaddr$ = Chr$(&H20 + SlaveAddr$)
Enq$ = Chr$(&H5)
Ack$ = Chr$(&H6)
Soh$ = Chr$(&H1)
Stx$ = Chr$(&H2)
Etb$ = Chr$(&H17)
Nak$ = Chr$(&H15)
Etx$ = Chr$(&H3)
Eot$ = Chr$(&H4)
Red$ = Chr$(&H30)
Dta$ = ""
Slaveenq$ = Nenq$ + Offsetaddr$ + Enq$
Header$ = Soh$ + SlaveAddr$ + Red$ + Datatype$ + Dataaddr$
Header$ = Header$ + Completeblk$ + Partblk$ + Masteraddr$ + Etb$
LRCRES = 0
LRC = 0
For I = 2 To (Len(Header$) - 1)
LRC = LRC Xor Asc(Mid$(Header$, I, 1))
Next I
LRCRES = Hex$(LRC)
If Len(LRCRES) < 2 Then LRCRES = "0" + LRCRES

End Sub

This code is from the form.

Private Sub Command1_Click()
Call SlaveAddr_Change
Call LoopTimes_Change
For X = 1 To LoopCount
  Looping = X
    MSComm1.CommPort = 1
  MSComm1.Settings = "9600,O,8,1"
  MSComm1.InputLen = 0
  MSComm1.PortOpen = True
  MSComm1.InBufferCount = 0
  MSComm1.Output = Slaveenq$
  Do
    DoEvents
    Loop Until MSComm1.InBufferCount = 4
    Result$ = MSComm1.Input
  Call Text1_Change

  If Asc(Mid(Result$, 3, 1)) = 6 Then
     MSComm1.InBufferCount = 0
     MSComm1.Output = Header$ + LRCRES

  Do
    DoEvents
    Loop Until MSComm1.InBufferCount > 10
    Hdrresponse$ = MSComm1.Input
  Call Text2_Change

    MSComm1.InBufferCount = 0
    MSComm1.Output = Ack$

  Do
    DoEvents
    Loop Until MSComm1.InBufferCount > 1

    FirstEOT$ = MSComm1.Input
    Text3 = Asc(Mid(FirstEOT$, 1, 1))
  If Asc(Mid(FirstEOT$, 1, 1)) = 4 Then
    MSComm1.InBufferCount = 0
    MSComm1.Output = Eot$

    MSComm1.PortOpen = False

End If
End If
Next X
Looping = "Done"

End Sub

Private Sub SlaveAddr_Change()

SlaveAddr$ = Hex$(SlaveAddress.Text)
Call Build_String

End Sub

Private Sub Form_Load()
SlaveAddress.Text = "1"
LoopTimes.Text = "20"

End Sub

Private Sub LoopTimes_Change()
LoopCount = LoopTimes.Text
End Sub

Private Sub StopLooping_Click()

  X = LoopCount - 1

End Sub

Private Sub Text1_Change()

  Enqres1$ = Hex$(Asc(Mid(Result$, 1, 1)))
  Enqres2$ = Hex$(Asc(Mid(Result$, 2, 1)))
  Enqres3$ = Hex$(Asc(Mid(Result$, 3, 1)))

Text1 = Enqres1$ + " " + Enqres2$ + " " + Enqres3$

End Sub

Private Sub Text2_Change()

  Hdrres1$ = Hex$(Asc(Mid(Hdrresponse$, 1, 1)))
  Hdrres2$ = Hex$(Asc(Mid(Hdrresponse$, 3, 1)))
  Hdrres3$ = Hex$(Asc(Mid(Hdrresponse$, 4, 1)))
  Hdrres4$ = Hex$(Asc(Mid(Hdrresponse$, 5, 1)))
  Hdrres5$ = Hex$(Asc(Mid(Hdrresponse$, 6, 1)))
  Hdrres6$ = Hex$(Asc(Mid(Hdrresponse$, 7, 1)))
  Hdrres7$ = Hex$(Asc(Mid(Hdrresponse$, 8, 1)))
Text2 = Hdrres1$ + " " + Hdrres2$ + " " + Hdrres3$ + " " + Hdrres4$ + " " +
Hdrres5$ + " " + Hdrres6$ + " " + Hdrres7$

End Sub

--
Confidentiality Note: The information contained in this email and document
(s) attached are for the exclusive use of the addressee and may contain
confidential, privileged and non-disclosable information. If the recipient
of this email is not the addressee, such recipient is strictly prohibited
from reading, photocopying, distributing or otherwise using this email or
its contents in any way.

Quote:

> > Actually you just helped me a lot.  I guess what i'm looking for is a
line
> > by line explanation.  For some reason I can't completely grasp this
code.
> > To find the LRC manually you take the hex ascii value of each byte.
Convert
> > to binary.  then Exclusive OR the binary value and convert to hex ascii.
> > IE.

> > 30 34 30 31 34 31 30 31 30 31 39 30 30 31
> > each value is converted to binary ie 30 = 00110000
> > the total number of 1's are added up for each bit ie B4 = 14 (this is
for
> > the 4th bit of all the values)
> > Then its Exclusive Or'd.
> > Then converted to hex ascii.

> > Thanks again for the great help.

> Your outline needs a bit more clarification.  You say the bits are
totaled,
> yet I don't see that in the code.  But the difficult part is this
statement:

> > Then its Exclusive Or'd.

> XOR is an operator, it needs two values to operate on.  What is XOR'ed
with what?

> Assuming you know how a For/Next loop works, and what the assignment
operator
> does (A = 5)  the only complicated part is the one line inside the loop.
To help
> explain it, I broke it into two different commands:

> ' Assignments
> LRCRES = 0
> LRC = 0

> ' Loops from 2 to length of Header - 1 (omits first and last characters)
> For I = 2 To (Len(Header$) - 1)

> ' Simplified for explanation
> ' Assigns the ASCII value of the letter at position I
> CHAR = Asc(Mid$(Header$, I, 1))

> ' XOR operation assigned back to operand
> LRC = LRC Xor CHAR

> ' End of loop
> Next I

> ' Convert to Hexidecimal string
> LRCRES = Hex$(LRC)

> ' Adds a leading 0 if less than 2 characters.
> If Len(LRCRES) < 2 Then LRCRES = "0" + LRCRES

> A value is a value, whether you look at it in Base 10 (decimal), base 16
(hexidecimal)
> or base 2 (binary).  That is why you would not need to convert it to do
any type
> of operation on it.  The time you need to convert it is when you want it
displayed,
> that is why you see no conversion to binary.  I assume, from looking at
the code, that
> LRCRES will be used to display the resulting value because that variable
is assigned
> a (hexidecimal) string value, suitable for printing to the screen or a
file.

> HTH
> LFS



Mon, 09 May 2005 16:36:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Explanation of CDO code please

2. One last explanation - apology to the group

3. explanation

4. Definition & Explanation of Reflection

5. Instr Function Explanation Anyone ???

6. DAO Recordset explanation needed

7. VB5 & Informix (explanation)

8. Long Explanation, But A Basic VB6/Winsock/SMTP Question

9. Where do I find explanation about classes I can use from VB

10. text on a bitmap-clearer explanation

11. Setup.lst explanation

12. Help with overflow error 6 - any logical explanations ?

 

 
Powered by phpBB® Forum Software