LRC Calculation method

Hello Everyone,

I am trying to implement in my Xojo Desktop Application an integration with a Bank POS machine. The protocol asks for a calculated LRC byte at the end.

STX + COMMAND + ETX + LRC

In my case I have the following string

Var requestBody as String = Chr(&h30)+Chr(&h38)+Chr(&h30)+Chr(&h30)+Chr(&h23)+Chr(&h39)+Chr(&h32)+Chr(&h23)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h23)+Chr(&h46) +Chr(&h03)

And I must calculate the LRC for this but I cannot understand how to do this.

The LRC of this string is expected to be 65.

If anyone has already worked with LRC, it would help me very much.
Thank you in advance.

I have never worked with LRC but I think your code could be simplified like this

Var requestbody as String = &u30 + &u38...
1 Like

Longitudinal Redundancy Check is computed “XORing” all bytes of the message.

Now we need to define where the message starts and ends…

I guess in your example it does not include <STX> but includes <ETX>

Var requestBody As String = “0800#92#000000#F”+&u3

The problem with this string is that I got a simple XOR LRC = 101, and ISO 1155 LRC = 47

None 65.


Public Function LRC(m As MemoryBlock) As Integer
  Var xorer As Integer = 0
  For i As Integer = 0 to m.Size-1
    xorer = xorer Xor m.UInt8Value(i)
  Next
  Return xorer
End Function

Public Function LRC_ISO1155(m As MemoryBlock) As Integer
  Var xorer As Integer = 0
  For i As Integer = 0 to m.Size-1
    xorer = (xorer + m.UInt8Value(i)) And &h0FF
    xorer = ((xorer xor &h0FF)+1) And &h0ff
  Next
  Return xorer
End Function

Double check your string or the Bank POS manual

1 Like

Is the result in hex? 101 decimal = 65 hex

2 Likes

Good catch. Maybe he forgot the &h.

so

Public Function LRC(m As MemoryBlock) As Integer
  Var xorer As Integer = 0
  For i As Integer = 0 to m.Size-1
    xorer = xorer Xor m.UInt8Value(i)
  Next
  Return xorer
End Function

Var requestBody as String = Chr(&h30)+Chr(&h38)+Chr(&h30)+Chr(&h30)+Chr(&h23)+Chr(&h39)+Chr(&h32)+Chr(&h23)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h30)+Chr(&h23)+Chr(&h46) +Chr(&h03)

requestBody = requestBody + Chr(LRC(requestBody))

1 Like

Similar to Andrew and Rick, the manual calculation seems to be 101 in Decimal.

LRC

1 Like

Thank you all for your replies,

Sorry for answering a little bit late. I finally found out that LRC calculate the string including ETX
My code that finally calculates correctly LRC is the above:

Var lvArray() as String = pvData.Split(“”)
Var response as string

if lvArray.Count>=1 then
Var lrc as Integer = lvArray(0).Asc

for i as Integer = 1 to lvArray.Count-1
lrc = lrc XOR lvArray(i).Asc
next
response = Chr(lrc)
end if

That’s incorrect. That’s why I opted to use a MemoryBlock.
You introduced a random possibility of fail.

To make your option to work, change pvData.Split(“”) to pvData.SplitBytes(“”)

Why? Check:

Var bytes As string = DecodeHex("C5 B4 C5 99 C5 8F C5 84 C4 A1") // 10 bytes

Var bytesSplit() As String

bytesSplit = bytes.Split("")        // Unexpected  array of 5 chars
break

bytesSplit = bytes.SplitBytes("")   // Correct. Those 10 "char" bytes
break
1 Like

Hello Rick,

I will follow your advice. Thank you very much.

1 Like

Also use the complimentary byte methods AscByte and ChrByte for the rest.