I have mangled this and Mangled This...I cannot figure out what I'm doing wrong.
I'm writing software to read and write commands to a device. The device has a message format of:
<start><command><length><Data1><Datan><BCC CheckSum><end>
and this is an actual string from the device:
024A033030317B03
So 02 is the start bit, 4A is the command, 03 is the length of the data, the next 3 bytes are the data, then 7B is the checksum and then the end bit.
The instructions from the documentation for checksum say:
Block Checksum Character: XOR of all Characters from start to last character of data.
Here is the code I have now which is close but no cigar:
Checksum is 123
This code produces:
Computed Checksum 4B (75)
What am I doing wrong?!!
I'm writing software to read and write commands to a device. The device has a message format of:
<start><command><length><Data1><Datan><BCC CheckSum><end>
and this is an actual string from the device:
024A033030317B03
So 02 is the start bit, 4A is the command, 03 is the length of the data, the next 3 bytes are the data, then 7B is the checksum and then the end bit.
The instructions from the documentation for checksum say:
Block Checksum Character: XOR of all Characters from start to last character of data.
Here is the code I have now which is close but no cigar:
B4X:
Sub GetCheckSum(MyData As String) As String
Dim CRC = 0 As Int
If Not(MyData.StartsWith("02")) Then
MyData = "02" & MyData
End If
Try
Dim CRC = 0 As Int
Do While MyData.Length >= 2
CRC = Bit.Xor(CRC, Bit.ParseInt(MyData.SubString2(0, 2), 16))
MyData = MyData.SubString(2)
Loop
Dim tmp(1) As Byte
tmp(0) = CRC
Dim tmpStr = conv.HexFromBytes(tmp) As String
Return tmpStr
Catch
Log("Couldn't convert! Error was: " & LastException.Message)
Return("00")
End Try
End Sub
Checksum is 123
This code produces:
Computed Checksum 4B (75)
What am I doing wrong?!!