Jump to content

How to calculate Longitudinal Redundancy Check (LRC)?


Jemboy
 Share

Recommended Posts

For some serial data check I have to check if the data was send ok by using LRC.
I am trying to convert the code below into Autoit however without any succes (the LRC does not match).

public static byte calculateLRC(byte[] bytes)
{
    byte LRC = 0;
    for (int i = 0; i < bytes.Length; i++)
    {
        LRC ^= bytes[i];
    }
    return LRC;
}

So far I create this code but the LRC does not match the expected value.

//Array Data is Hex values
$LongStr = "30 30 43 31 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 31 20 20 20 20 20 20 20 "
$LongStr = $LongStr & "20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 31 30 30 30 30 35 30 31 45 55 52"   ; Expected LRC = 06


$aArray = StringSplit($LongStr, " ", $STR_CHRSPLIT )

$lrc = 0
$b = 0
For $t=1 to $aArray[0]
    $b = Dec($aArray[$t])
    $lrc = BitXOR ($lrc,$b)
Next
MsgBox (0,"LRC","LRC (Dec) = " & $lrc & "      Hex: " & Hex ($lrc) )

Any help would be much appreciated 😉

Edited by Jemboy
Link to comment
Share on other sites

I tested your C# calculateLRC function  with your sample data and it returns 0x05.

 

Saludos

Link to comment
Share on other sites

It seems my translation from C# to Autoit does the same calculation.

I need the LRC as a checksum to determine if the RS232 communication to and
from our  pay terminal data was without errors.

Both the C# and Autoit calculate the same LRC value of:  0x05
Also with other data the LRC in both scripts have the same results.


IN THE ABOVE CASE, The pay terminal generatest LRC of: 0x06 (instead of  0x05)
Almost all my LRCs are 1,2 or 3 digits off. Mostly only 1.

I have to ivestigate further 😉

Edited by Jemboy
Link to comment
Share on other sites

I am getting some headache from this problem.😁


The pay terminal is configured to receive baudrate:9600. data:7, parity:e and stopbit: 1 from the serial port.
My data is 8 bit (althoug all bytes are < 127).


Could this have an effect on my data stream ?
Does anyone know if I would have to convert my data before sending it ?

Link to comment
Share on other sites

I tried it myself but it does not give the expected result.  Only thing I could think of, is that bits may be received in reverse order ?

Maybe you could check on Web for that specific machine, what is their algorithm ?

Link to comment
Share on other sites

I found out the problem and I must say I am a little bit ashamed 😚

The protocol to send  the data to the serial could be visualized like this:  <STX> <DATA> <ETX> <LRC>      (with STX=02  and ETX=03)
STX stands for Start of Text and ETX stands for End of Text.
I presumed the LRC should only be calculated using the <DATA>, but upon rereading the protocol I saw that the <ETX> also needed to be included.

Now, when I add the ETX to the array, the LRC indeed becomes: 0x06.
I have added the 03 to the array (see below) and my LRC is now as expected.

At least if some one needs a LRC implementation for Autoit, there is one working implementation in this forum. 😃

 

//Array Data is Hex values
$LongStr = "30 30 43 31 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 31 20 20 20 20 20 20 20 "
$LongStr = $LongStr & "20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 31 30 30 30 30 35 30 31 45 55 52 03"   ; Expected LRC = 06


$aArray = StringSplit($LongStr, " ", $STR_CHRSPLIT )

$lrc = 0
$b = 0
For $t=1 to $aArray[0]
    $b = Dec($aArray[$t])
    $lrc = BitXOR ($lrc,$b)
Next
MsgBox (0,"LRC","LRC (Dec) = " & $lrc & "      Hex: " & Hex ($lrc) )

 

 

Edited by Jemboy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...