Is this a BUG?

Status
Not open for further replies.

SwingeyP

Member
Hello.

I have this code -

Code:
'$$MØTVU,,,,,,Ø,,,,,,,,,  = 0x36f3 (no $$)
Dim data_byte(22) As Byte
Dim data_word As Word
Dim crc As Word
Dim bt As Byte
Dim tmp As Word
Dim i As Byte
Dim len As Byte
Dim polynomial As Word

polynomial = 0x1021  '0x1021 -

Hseropen 9600

len = 4
data_byte(0) = "M"  'M 77 1001101
data_byte(1) = "0"
data_byte(2) = "T"
data_byte(3) = "V"
data_byte(4) = "U"
'data_byte(5) = ","
'data_byte(6) = ","
'data_byte(7) = ","
'data_byte(8) = ","
'data_byte(9) = ","
'data_byte(10) = ","

'For len = 0 To 4  '20
'data_byte(len) = LookUp("MØTVU"), len  '$$ will be added for send
'Next len




'-----------------------------------------
'This would be a function
'-----------------------------------------
crc = 0xffff
For i = 0 To len
data_word = data_byte(i)
data_word = ShiftLeft(data_word, 8)  'shift the data left 8
crc = crc Xor data_word  'XOR the data with the CRC
For bt = 0 To 7
	tmp = crc And 0x8000  'and the CRC with 0x8000
	If tmp <> 0 Then  'if this vale <> 0 then will be either 8000 or 0
		crc = ShiftLeft(crc, 1)  'shift the CRC left 1
		crc = crc Xor polynomial  'XOR the CRC with polynomial
	Else
		crc = ShiftLeft(crc, 1)  'else just shift the crc left 1
	Endif
Next bt
Next i
Hserout "CRC = ", #crc, CrLf  'M = 0x7899 -  or maybe 0x55f5 -

End                                               

'M  = 30873 - 0x7899
'M0  = 20684 - 0x50CC
'M0T  = 35972 - 0x8C84
'M0TV = 60983 - 0xee37
'M0TVU = 8624 - ox21b0

'0123456789 = 0x7d61

If you run it as is and compare the CRC value against this site https://www.lammertbies.nl/comm/info/crc-calculation.html

the CRC will match.

Ok so now comment out all of the 'manual' array loads and un comment the lookup look which loads the array - Surely the same thing - Watch variables are reporting array variables identically.

Like this ...

Code:
'$$MØTVU,,,,,,Ø,,,,,,,,,  = 0x36f3 (no $$)
Dim data_byte(22) As Byte
Dim data_word As Word
Dim crc As Word
Dim bt As Byte
Dim tmp As Word
Dim i As Byte
Dim len As Byte
Dim polynomial As Word

polynomial = 0x1021  '0x1021 -

Hseropen 9600

'len = 4
'data_byte(0) = "M"  'M 77 1001101
'data_byte(1) = "0"
'data_byte(2) = "T"
'data_byte(3) = "V"
'data_byte(4) = "U"
'data_byte(5) = ","
'data_byte(6) = ","
'data_byte(7) = ","
'data_byte(8) = ","
'data_byte(9) = ","
'data_byte(10) = ","

For len = 0 To 4  '20
data_byte(len) = LookUp("MØTVU"), len  '$$ will be added for send
Next len




'-----------------------------------------
'This would be a function
'-----------------------------------------
crc = 0xffff
For i = 0 To len
data_word = data_byte(i)
data_word = ShiftLeft(data_word, 8)  'shift the data left 8
crc = crc Xor data_word  'XOR the data with the CRC
For bt = 0 To 7
	tmp = crc And 0x8000  'and the CRC with 0x8000
	If tmp <> 0 Then  'if this vale <> 0 then will be either 8000 or 0
		crc = ShiftLeft(crc, 1)  'shift the CRC left 1
		crc = crc Xor polynomial  'XOR the CRC with polynomial
	Else
		crc = ShiftLeft(crc, 1)  'else just shift the crc left 1
	Endif
Next bt
Next i
Hserout "CRC = ", #crc, CrLf  'M = 0x7899 -  or maybe 0x55f5 -

End                                               

'M  = 30873 - 0x7899
'M0  = 20684 - 0x50CC
'M0T  = 35972 - 0x8C84
'M0TV = 60983 - 0xee37
'M0TVU = 8624 - ox21b0

'0123456789 = 0x7d61
Run that and compare the CRC - Different answer!

How weird. Is it a bug or am I doing something stupid as usual.

Just noticed that I should have len = len - 1 to bring len back to 4 and the one that took ages to finf was the '0' - I had cut and pasted and of course it's a different character.. - Been a long day sorry.

Regards - Paul
 
Last edited:
It is hard to read because you're posting snippets of your code. But on the second snippet you may be using len as a global variable; in which case if I read it correctly will have a value of 5 when it enters the CRC function, whereas it has a hardcoded value of 4 one the first snippet.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…