Link to home
Start Free TrialLog in
Avatar of tim freese
tim freeseFlag for United States of America

asked on

LRC Computation Help

Good day.

I am searching for a function in VB.NET to get the LRC for a HEX result.

For instance, I have the following HEX values (I included spaces for reability only):

01 30 02 42 20 21 20 03 The LRC result is 53

what kind of VB.NET function can be written to computer this LRC?

Thank you for your help.




Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

If you explain LRC, we'll find something :)
Avatar of GivenRandy
GivenRandy

Are your hex values in a string? An array? A file?
Avatar of tim freese

ASKER

LRC = Longitudinal Redundancy Check

The hex values are in a string which are received from a COM port.
Something like the following (but I get 33 as a result):

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 01 30 02 42 20 21 20 03 The LRC result is 53
        Dim MyString As String
        MyString = Chr(1)
        MyString &= Chr(30)
        MyString &= Chr(2)
        MyString &= Chr(42)
        MyString &= Chr(20)
        MyString &= Chr(21)
        MyString &= Chr(20)
        MyString &= Chr(3)
        MessageBox.Show("Result is: " & CalculateLRC(MyString))
    End Sub

    Public Function CalculateLRC(ByVal S As String) As Byte
        CalculateLRC = 0
        For Index As Integer = 1 To Len(S)
            CalculateLRC = CByte(CalculateLRC Xor Asc(Mid(S, Index, 1)))
        Next
    End Function
hmmmm...

00000001 (01)
00110000 (30)
00000010 (02)
01000010 (42)
00100000 (20)
00100001 (21)
00100000 (20)
00000011 (03)
-----------
01010011 = 53
Try this...

Function getLRC(message As String) As String
    Dim i As Integer
    Dim mlength As Integer
    Dim lrc As Integer
    Dim cchar As String
    lrc = 0
    i = 1 'set start position
   
    mlenght = Len(message)
   
    Do While i <= mlenght
        cchar = (Mid$(message, i, 1))
        lrc = Asc(cchar) Xor lrc        
        i = i + 1
    Loop
   
    getLRC = Chr$(lrc)
End Function

Funny what Google turns up... https://www.experts-exchange.com/questions/20732543/MSComm-xor-LRC.html
I think you should be starting with 00000000 and THEN xor'ing 00000001, right?
Strike that, duh. Let me see why it's different.
i tried the one you have, Chaosian, and it returns an exclamation mark.  i tried it last night it couldn't get it to work.

GivenRandy:  I tried your function against another string and it was off by 20 again.
Duh, because it's hex. More like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 01 30 02 42 20 21 20 03 The LRC result is 53
        Dim MyString As String
        MyString = Chr(&H1)
        MyString &= Chr(&H30)
        MyString &= Chr(&H2)
        MyString &= Chr(&H42)
        MyString &= Chr(&H20)
        MyString &= Chr(&H21)
        MyString &= Chr(&H20)
        MyString &= Chr(&H3)
        MessageBox.Show("Result is: " & CalculateLRC(MyString))
    End Sub

    Public Function CalculateLRC(ByVal S As String) As Byte
        CalculateLRC = 0
        For Index As Integer = 1 To Len(S)
            CalculateLRC = CByte(CalculateLRC Xor Asc(Mid(S, Index, 1)))
        Next
    End Function
It returns 83, which is 53H. The CalculateLRC() function is unchanged. I just had the wrong input values going into it.
If you wanted the hex conversion:

        MessageBox.Show("Result is: " & Hex(CalculateLRC(MyString)) & " hexadecimal")
ASKER CERTIFIED SOLUTION
Avatar of GivenRandy
GivenRandy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
fantastic.

the one item i added was

Return Hex(CalculateLRC) at the end of the calculateLRC function as i need to include this in the string i send....

i appreciate the quick response and clean code.

have a good one.

well, thgat one's returning a string. Wonder if ! is Chr(53)?
sometimes when the answer is obvious, it's overlooked, Chaosian.