2

I've been researching this everywhere and all the LRC implementation seems to not giving me the right answer. After spending few days on it, I decided to put my code here to see if anyone else can spot the problem.

Here's the code (C#)

        //Input Data = "31303030315E315E31303030325E315E31303030375E39395E31303032325E36353631335E"
        //LRC Answer = "30"
        private static string LRC(string Data)
        {
            int checksum = 0;
            foreach (char c in GetStringFromHex(Data))
            {
                checksum ^= Convert.ToByte(c);
            }


            string hex = checksum.ToString("X2");

            Console.WriteLine("Calculated LRC = " + hex);

            return hex;
        }





    //Supporting Function used in LRC function
    private static string GetStringFromHex(string s)
    {
        string result = "";
        string s2 = s.Replace(" ", "");
        for (int i = 0; i < s2.Length; i += 2)
        {
            result += Convert.ToChar(int.Parse(s2.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
        }
        return result;
    }

The current output shows "Calculated LRC = 33". However, the right answer is "30". Can anyone spot what's wrong with this?

Any help will be fantastic!

WorldWind
  • 311
  • 1
  • 5
  • 17
  • 1
    It would be good to spell out "LRC"... I presume it's meant to be some sort of check code, like a CRC, or convolutional error correction code (for example Reed-Solomon)? Your implementation looks like simple parity. – Ben Voigt Mar 15 '15 at 04:12
  • Ah... True, thanks for the reminder, it's Longitudinal Redundancy Check. – WorldWind Mar 15 '15 at 04:14
  • The wikipedia article suggests that it's traditional to return the two's complement (`-` in C#) of the sum (not XOR) of the bytes. But says some protocols use XOR. – Ben Voigt Mar 15 '15 at 04:17
  • I've implemented based on Wikipedia's pseudo code as well. It's also giving me wrong answer. – WorldWind Mar 15 '15 at 04:20
  • Do you have any documentation for the device you're communicating with? And it doesn't define the checksum better? – Ben Voigt Mar 15 '15 at 04:24
  • I know the answer is 30 in hex for sure. I've done this identical task 2-3 years ago with same documentations. The only difference is that this one is done in C# and previous one is done in C++ and I don't have access to the C++ source code that i've created in the past anymore... – WorldWind Mar 15 '15 at 04:24
  • BTW dehexed Data is printable ASCII -- '10001^1^10002^1^10007^99^10022^65613^' – Ben Voigt Mar 15 '15 at 04:27
  • The weird thing is that if you treat that data like C source code and do the XORs between those numbers (treating each number as decimal) you get `0x1001C`. Actually, if the last number is decimal and the rest are hex, they'd all be in the same range. – Ben Voigt Mar 15 '15 at 04:30
  • The Hex was generated via ASCII. Basically that's Name Value Pair, where it's OpCode^Value. Once it is converted into HEX then it'll be sent through RS232 connection to the device. This is so strange that I cannot get 30 for some reason.... – WorldWind Mar 15 '15 at 04:34
  • Sorry, I don't see any combination of the encoded bytes or pre-encoding opcodes or anything at all that gives 0x30 – Ben Voigt Mar 15 '15 at 04:48
  • Basically, the goal from the implementation documentation said they are expecting a LRC at the end. The sample answer they give is 30 after calculating LRC against the HEX number.... Unless they need to include STX and ETX, but i remember typically you don't include those two to calculate LRC. – WorldWind Mar 15 '15 at 04:53
  • Well... ETX is then exactly the difference between what you got and what you want. Is there only this one example? – Ben Voigt Mar 15 '15 at 04:57
  • that's true, but i don't think we should include ETX if we exclude STX though... Yes, that's the only one example (I know how annoying when there's not enough examples).... – WorldWind Mar 15 '15 at 05:44
  • Found an interesting link, is it true that we need to include ETX as part of the LRC calculation? ref: http://codeverge.com/embarcadero.delphi.winsock/pos-terminal-lrc-checksum-creation/1075353 – WorldWind Mar 15 '15 at 06:35
  • What type of device is this? Link to documentation? I've run through a few variations of LRC on your input and the only way to get 30 is as Ben says. – cory.todd Mar 15 '15 at 17:25
  • I guess now the question is down to standard LRC, do we include ETX and exclude STX during the LRC calculation? Any ideas where I can find some reputable standard guideline and practices? – WorldWind Mar 15 '15 at 17:30

1 Answers1

2

After several testing, it is confirmed LRC should include ETX and exclude STX during the LRC calculation.

WorldWind
  • 311
  • 1
  • 5
  • 17