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.
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
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))
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