View Full Version : CRC16 Checksum calculator
Manser
March 26th, 2007, 06:36 AM
It's maybe not the right place here but have someone already done a crc16
calculator in lua?
I have this c-source code and the same as java but I'm not so sure in implementation this in lua.
In fact I'm getting crazy with this.
I need this for a driver for a building control system which I want to use with NR + Girder as the visualization interface.
Promixis
March 27th, 2007, 06:59 AM
this will look something like....
local crcModTab = {
math.hextodec ('0000'), math.hextodec ('535b'), math.hextodec ('a6b6'), math.hextodec ('f5ed'), math.hextodec ('1f36'), math.hextodec ('4c6d'), math.hextodec ('b980'), math.hextodec ('eadb'),
math.hextodec ('3e6c'), math.hextodec ('6d37'), math.hextodec ('98da'), math.hextodec ('cb81'), math.hextodec ('215a'), math.hextodec ('7201'), math.hextodec ('87ec'), math.hextodec ('d4b7'),
math.hextodec ('7cd8'), math.hextodec ('2f83'), math.hextodec ('da6e'), math.hextodec ('8935'), math.hextodec ('63ee'), math.hextodec ('30b5'), math.hextodec ('c558'), math.hextodec ('9603'),
math.hextodec ('42b4'), math.hextodec ('11ef'), math.hextodec ('e402'), math.hextodec ('b759'), math.hextodec ('5d82'), math.hextodec ('0ed9'), math.hextodec ('fb34'), math.hextodec ('a86f'),
math.hextodec ('abeb'), math.hextodec ('f8b0'), math.hextodec ('0d5d'), math.hextodec ('5e06'), math.hextodec ('b4dd'), math.hextodec ('e786'), math.hextodec ('126b'), math.hextodec ('4130'),
math.hextodec ('9587'), math.hextodec ('c6dc'), math.hextodec ('3331'), math.hextodec ('606a'), math.hextodec ('8ab1'), math.hextodec ('d9ea'), math.hextodec ('2c07'), math.hextodec ('7f5c'),
math.hextodec ('d733'), math.hextodec ('8468'), math.hextodec ('7185'), math.hextodec ('22de'), math.hextodec ('c805'), math.hextodec ('9b5e'), math.hextodec ('6eb3'), math.hextodec ('3de8'),
math.hextodec ('e95f'), math.hextodec ('ba04'), math.hextodec ('4fe9'), math.hextodec ('1cb2'), math.hextodec ('f669'), math.hextodec ('a532'), math.hextodec ('50df'), math.hextodec ('0384'),
math.hextodec ('058c'), math.hextodec ('56d7'), math.hextodec ('a33a'), math.hextodec ('f061'), math.hextodec ('1aba'), math.hextodec ('49e1'), math.hextodec ('bc0c'), math.hextodec ('ef57'),
math.hextodec ('3be0'), math.hextodec ('68bb'), math.hextodec ('9d56'), math.hextodec ('ce0d'), math.hextodec ('24d6'), math.hextodec ('778d'), math.hextodec ('8260'), math.hextodec ('d13b'),
math.hextodec ('7954'), math.hextodec ('2a0f'), math.hextodec ('dfe2'), math.hextodec ('8cb9'), math.hextodec ('6662'), math.hextodec ('3539'), math.hextodec ('c0d4'), math.hextodec ('938f'),
math.hextodec ('4738'), math.hextodec ('1463'), math.hextodec ('e18e'), math.hextodec ('b2d5'), math.hextodec ('580e'), math.hextodec ('0b55'), math.hextodec ('feb8'), math.hextodec ('ade3'),
math.hextodec ('ae67'), math.hextodec ('fd3c'), math.hextodec ('08d1'), math.hextodec ('5b8a'), math.hextodec ('b151'), math.hextodec ('e20a'), math.hextodec ('17e7'), math.hextodec ('44bc'),
math.hextodec ('900b'), math.hextodec ('c350'), math.hextodec ('36bd'), math.hextodec ('65e6'), math.hextodec ('8f3d'), math.hextodec ('dc66'), math.hextodec ('298b'), math.hextodec ('7ad0'),
math.hextodec ('d2bf'), math.hextodec ('81e4'), math.hextodec ('7409'), math.hextodec ('2752'), math.hextodec ('cd89'), math.hextodec ('9ed2'), math.hextodec ('6b3f'), math.hextodec ('3864'),
math.hextodec ('ecd3'), math.hextodec ('bf88'), math.hextodec ('4a65'), math.hextodec ('193e'), math.hextodec ('f3e5'), math.hextodec ('a0be'), math.hextodec ('5553'), math.hextodec ('0608'),
math.hextodec ('5943'), math.hextodec ('0a18'), math.hextodec ('fff5'), math.hextodec ('acae'), math.hextodec ('4675'), math.hextodec ('152e'), math.hextodec ('e0c3'), math.hextodec ('b398'),
math.hextodec ('672f'), math.hextodec ('3474'), math.hextodec ('c199'), math.hextodec ('92c2'), math.hextodec ('7819'), math.hextodec ('2b42'), math.hextodec ('deaf'), math.hextodec ('8df4'),
math.hextodec ('259b'), math.hextodec ('76c0'), math.hextodec ('832d'), math.hextodec ('d076'), math.hextodec ('3aad'), math.hextodec ('69f6'), math.hextodec ('9c1b'), math.hextodec ('cf40'),
math.hextodec ('1bf7'), math.hextodec ('48ac'), math.hextodec ('bd41'), math.hextodec ('ee1a'), math.hextodec ('04c1'), math.hextodec ('579a'), math.hextodec ('a277'), math.hextodec ('f12c'),
math.hextodec ('f2a8'), math.hextodec ('a1f3'), math.hextodec ('541e'), math.hextodec ('0745'), math.hextodec ('ed9e'), math.hextodec ('bec5'), math.hextodec ('4b28'), math.hextodec ('1873'),
math.hextodec ('ccc4'), math.hextodec ('9f9f'), math.hextodec ('6a72'), math.hextodec ('3929'), math.hextodec ('d3f2'), math.hextodec ('80a9'), math.hextodec ('7544'), math.hextodec ('261f'),
math.hextodec ('8e70'), math.hextodec ('dd2b'), math.hextodec ('28c6'), math.hextodec ('7b9d'), math.hextodec ('9146'), math.hextodec ('c21d'), math.hextodec ('37f0'), math.hextodec ('64ab'),
math.hextodec ('b01c'), math.hextodec ('e347'), math.hextodec ('16aa'), math.hextodec ('45f1'), math.hextodec ('af2a'), math.hextodec ('fc71'), math.hextodec ('099c'), math.hextodec ('5ac7'),
math.hextodec ('5ccf'), math.hextodec ('0f94'), math.hextodec ('fa79'), math.hextodec ('a922'), math.hextodec ('43f9'), math.hextodec ('10a2'), math.hextodec ('e54f'), math.hextodec ('b614'),
math.hextodec ('62a3'), math.hextodec ('31f8'), math.hextodec ('c415'), math.hextodec ('974e'), math.hextodec ('7d95'), math.hextodec ('2ece'), math.hextodec ('db23'), math.hextodec ('8878'),
math.hextodec ('2017'), math.hextodec ('734c'), math.hextodec ('86a1'), math.hextodec ('d5fa'), math.hextodec ('3f21'), math.hextodec ('6c7a'), math.hextodec ('9997'), math.hextodec ('cacc'),
math.hextodec ('1e7b'), math.hextodec ('4d20'), math.hextodec ('b8cd'), math.hextodec ('eb96'), math.hextodec ('014d'), math.hextodec ('5216'), math.hextodec ('a7fb'), math.hextodec ('f4a0'),
math.hextodec ('f724'), math.hextodec ('a47f'), math.hextodec ('5192'), math.hextodec ('02c9'), math.hextodec ('e812'), math.hextodec ('bb49'), math.hextodec ('4ea4'), math.hextodec ('1dff'),
math.hextodec ('c948'), math.hextodec ('9a13'), math.hextodec ('6ffe'), math.hextodec ('3ca5'), math.hextodec ('d67e'), math.hextodec ('8525'), math.hextodec ('70c8'), math.hextodec ('2393'),
math.hextodec ('8bfc'), math.hextodec ('d8a7'), math.hextodec ('2d4a'), math.hextodec ('7e11'), math.hextodec ('94ca'), math.hextodec ('c791'), math.hextodec ('327c'), math.hextodec ('6127'),
math.hextodec ('b590'), math.hextodec ('e6cb'), math.hextodec ('1326'), math.hextodec ('407d'), math.hextodec ('aaa6'), math.hextodec ('f9fd'), math.hextodec ('0c10'), math.hextodec ('5f4b
} ;
function Build_CRC ( s )
{
local crc = 0
local short j
for i = 1,string.len (s) do
j = crc ^ string.sub (s,i)
crc = crcModTab [j&math.hextodec ('00FF')] ^ (math.bshiftr (crc,8))
end
return crc
Rob H
March 27th, 2007, 10:46 AM
That string.sub should be string.byte I think.
Manser
March 29th, 2007, 02:39 AM
Hello Mike and Rob
Thank you very much for our support. I will try it out now.
One point I'm not sure is the "^ operator" which means in c Xor and in Lua we use it for exponentiation. Do I have to write a function who manage this xor operation or is there an operator in lua?
Rob H
March 29th, 2007, 03:21 AM
use math.bxor() for a bitwise XOR
Manser
March 29th, 2007, 06:10 AM
Thanks for that,
Ok so that's what I did until now, there is a result, but not the one I
expect. Did I miss something or do I misunderstand the example?
Protocoll looks like that:
0x02 / 0xA1 /0x00 / 0xB0 0x7E / 0x03
STX /F_Reset /Sequenz /CRC L,H / ETX
0x02 / 0x11 /0x01 / 0x08 0x00 / 0x04 0x01 / 0x05 / 0x00 0x01 0x02 0x03 0x04 / /0x03
STX /job /Sequenz,nr / lenghtL,H / Dataregister/Howmuch/ data / crc / etx
local crcModTab = {
math.hextodecimal ('0000'), math.hextodecimal ('535b'), math.hextodecimal ('a6b6'), math.hextodecimal ('f5ed'), math.hextodecimal ('1f36'), math.hextodecimal ('4c6d'), math.hextodecimal ('b980'), math.hextodecimal ('eadb'),
math.hextodecimal ('3e6c'), math.hextodecimal ('6d37'), math.hextodecimal ('98da'), math.hextodecimal ('cb81'), math.hextodecimal ('215a'), math.hextodecimal ('7201'), math.hextodecimal ('87ec'), math.hextodecimal ('d4b7'),
math.hextodecimal ('7cd8'), math.hextodecimal ('2f83'), math.hextodecimal ('da6e'), math.hextodecimal ('8935'), math.hextodecimal ('63ee'), math.hextodecimal ('30b5'), math.hextodecimal ('c558'), math.hextodecimal ('9603'),
math.hextodecimal ('42b4'), math.hextodecimal ('11ef'), math.hextodecimal ('e402'), math.hextodecimal ('b759'), math.hextodecimal ('5d82'), math.hextodecimal ('0ed9'), math.hextodecimal ('fb34'), math.hextodecimal ('a86f'),
math.hextodecimal ('abeb'), math.hextodecimal ('f8b0'), math.hextodecimal ('0d5d'), math.hextodecimal ('5e06'), math.hextodecimal ('b4dd'), math.hextodecimal ('e786'), math.hextodecimal ('126b'), math.hextodecimal ('4130'),
math.hextodecimal ('9587'), math.hextodecimal ('c6dc'), math.hextodecimal ('3331'), math.hextodecimal ('606a'), math.hextodecimal ('8ab1'), math.hextodecimal ('d9ea'), math.hextodecimal ('2c07'), math.hextodecimal ('7f5c'),
math.hextodecimal ('d733'), math.hextodecimal ('8468'), math.hextodecimal ('7185'), math.hextodecimal ('22de'), math.hextodecimal ('c805'), math.hextodecimal ('9b5e'), math.hextodecimal ('6eb3'), math.hextodecimal ('3de8'),
math.hextodecimal ('e95f'), math.hextodecimal ('ba04'), math.hextodecimal ('4fe9'), math.hextodecimal ('1cb2'), math.hextodecimal ('f669'), math.hextodecimal ('a532'), math.hextodecimal ('50df'), math.hextodecimal ('0384'),
math.hextodecimal ('058c'), math.hextodecimal ('56d7'), math.hextodecimal ('a33a'), math.hextodecimal ('f061'), math.hextodecimal ('1aba'), math.hextodecimal ('49e1'), math.hextodecimal ('bc0c'), math.hextodecimal ('ef57'),
math.hextodecimal ('3be0'), math.hextodecimal ('68bb'), math.hextodecimal ('9d56'), math.hextodecimal ('ce0d'), math.hextodecimal ('24d6'), math.hextodecimal ('778d'), math.hextodecimal ('8260'), math.hextodecimal ('d13b'),
math.hextodecimal ('7954'), math.hextodecimal ('2a0f'), math.hextodecimal ('dfe2'), math.hextodecimal ('8cb9'), math.hextodecimal ('6662'), math.hextodecimal ('3539'), math.hextodecimal ('c0d4'), math.hextodecimal ('938f'),
math.hextodecimal ('4738'), math.hextodecimal ('1463'), math.hextodecimal ('e18e'), math.hextodecimal ('b2d5'), math.hextodecimal ('580e'), math.hextodecimal ('0b55'), math.hextodecimal ('feb8'), math.hextodecimal ('ade3'),
math.hextodecimal ('ae67'), math.hextodecimal ('fd3c'), math.hextodecimal ('08d1'), math.hextodecimal ('5b8a'), math.hextodecimal ('b151'), math.hextodecimal ('e20a'), math.hextodecimal ('17e7'), math.hextodecimal ('44bc'),
math.hextodecimal ('900b'), math.hextodecimal ('c350'), math.hextodecimal ('36bd'), math.hextodecimal ('65e6'), math.hextodecimal ('8f3d'), math.hextodecimal ('dc66'), math.hextodecimal ('298b'), math.hextodecimal ('7ad0'),
math.hextodecimal ('d2bf'), math.hextodecimal ('81e4'), math.hextodecimal ('7409'), math.hextodecimal ('2752'), math.hextodecimal ('cd89'), math.hextodecimal ('9ed2'), math.hextodecimal ('6b3f'), math.hextodecimal ('3864'),
math.hextodecimal ('ecd3'), math.hextodecimal ('bf88'), math.hextodecimal ('4a65'), math.hextodecimal ('193e'), math.hextodecimal ('f3e5'), math.hextodecimal ('a0be'), math.hextodecimal ('5553'), math.hextodecimal ('0608'),
math.hextodecimal ('5943'), math.hextodecimal ('0a18'), math.hextodecimal ('fff5'), math.hextodecimal ('acae'), math.hextodecimal ('4675'), math.hextodecimal ('152e'), math.hextodecimal ('e0c3'), math.hextodecimal ('b398'),
math.hextodecimal ('672f'), math.hextodecimal ('3474'), math.hextodecimal ('c199'), math.hextodecimal ('92c2'), math.hextodecimal ('7819'), math.hextodecimal ('2b42'), math.hextodecimal ('deaf'), math.hextodecimal ('8df4'),
math.hextodecimal ('259b'), math.hextodecimal ('76c0'), math.hextodecimal ('832d'), math.hextodecimal ('d076'), math.hextodecimal ('3aad'), math.hextodecimal ('69f6'), math.hextodecimal ('9c1b'), math.hextodecimal ('cf40'),
math.hextodecimal ('1bf7'), math.hextodecimal ('48ac'), math.hextodecimal ('bd41'), math.hextodecimal ('ee1a'), math.hextodecimal ('04c1'), math.hextodecimal ('579a'), math.hextodecimal ('a277'), math.hextodecimal ('f12c'),
math.hextodecimal ('f2a8'), math.hextodecimal ('a1f3'), math.hextodecimal ('541e'), math.hextodecimal ('0745'), math.hextodecimal ('ed9e'), math.hextodecimal ('bec5'), math.hextodecimal ('4b28'), math.hextodecimal ('1873'),
math.hextodecimal ('ccc4'), math.hextodecimal ('9f9f'), math.hextodecimal ('6a72'), math.hextodecimal ('3929'), math.hextodecimal ('d3f2'), math.hextodecimal ('80a9'), math.hextodecimal ('7544'), math.hextodecimal ('261f'),
math.hextodecimal ('8e70'), math.hextodecimal ('dd2b'), math.hextodecimal ('28c6'), math.hextodecimal ('7b9d'), math.hextodecimal ('9146'), math.hextodecimal ('c21d'), math.hextodecimal ('37f0'), math.hextodecimal ('64ab'),
math.hextodecimal ('b01c'), math.hextodecimal ('e347'), math.hextodecimal ('16aa'), math.hextodecimal ('45f1'), math.hextodecimal ('af2a'), math.hextodecimal ('fc71'), math.hextodecimal ('099c'), math.hextodecimal ('5ac7'),
math.hextodecimal ('5ccf'), math.hextodecimal ('0f94'), math.hextodecimal ('fa79'), math.hextodecimal ('a922'), math.hextodecimal ('43f9'), math.hextodecimal ('10a2'), math.hextodecimal ('e54f'), math.hextodecimal ('b614'),
math.hextodecimal ('62a3'), math.hextodecimal ('31f8'), math.hextodecimal ('c415'), math.hextodecimal ('974e'), math.hextodecimal ('7d95'), math.hextodecimal ('2ece'), math.hextodecimal ('db23'), math.hextodecimal ('8878'),
math.hextodecimal ('2017'), math.hextodecimal ('734c'), math.hextodecimal ('86a1'), math.hextodecimal ('d5fa'), math.hextodecimal ('3f21'), math.hextodecimal ('6c7a'), math.hextodecimal ('9997'), math.hextodecimal ('cacc'),
math.hextodecimal ('1e7b'), math.hextodecimal ('4d20'), math.hextodecimal ('b8cd'), math.hextodecimal ('eb96'), math.hextodecimal ('014d'), math.hextodecimal ('5216'), math.hextodecimal ('a7fb'), math.hextodecimal ('f4a0'),
math.hextodecimal ('f724'), math.hextodecimal ('a47f'), math.hextodecimal ('5192'), math.hextodecimal ('02c9'), math.hextodecimal ('e812'), math.hextodecimal ('bb49'), math.hextodecimal ('4ea4'), math.hextodecimal ('1dff'),
math.hextodecimal ('c948'), math.hextodecimal ('9a13'), math.hextodecimal ('6ffe'), math.hextodecimal ('3ca5'), math.hextodecimal ('d67e'), math.hextodecimal ('8525'), math.hextodecimal ('70c8'), math.hextodecimal ('2393'),
math.hextodecimal ('8bfc'), math.hextodecimal ('d8a7'), math.hextodecimal ('2d4a'), math.hextodecimal ('7e11'), math.hextodecimal ('94ca'), math.hextodecimal ('c791'), math.hextodecimal ('327c'), math.hextodecimal ('6127'),
math.hextodecimal ('b590'), math.hextodecimal ('e6cb'), math.hextodecimal ('1326'), math.hextodecimal ('407d'), math.hextodecimal ('aaa6'), math.hextodecimal ('f9fd'), math.hextodecimal ('0c10'), math.hextodecimal ('5f4b')
} ;
function Build_CRC (s)
print ("string ",s)
local crc = 0
local short, j
for i = 1,string.len (s) do
j = math.bxor(crc, string.byte (s,i))
print ("crc", crc)
print ("j ",j)
crc = math.bxor(crcModTab [math.band(j, 255)], (math.bshiftr (crc,8)))
print("zelle ",math.band(j, 255))
--crc = math.bxor(j + 255, (math.bshiftr (crc,8)))
print("crc ", crc)
end
return crc
end
Build_CRC ('A100') --should return "B0 7E" "45182"
--Build_CRC ('A300') --should return "B6 49" ""
Rob H
March 29th, 2007, 06:40 AM
As far as I can remember, C arrays are indexed starting at 0 while Lua arrays are indexed starting at 1, so when you do the table lookup add 1 to the index. ie
crc = math.bxor(crcModTab [math.band(j, 255) + 1], (math.bshiftr (crc,8)))
Manser
March 29th, 2007, 06:55 AM
That's a good point, but still give me a wrong result.
local crcModTab = {
math.hextodecimal ('0000'), math.hextodecimal ('535b'), math.hextodecimal ('a6b6'), math.hextodecimal ('f5ed'), math.hextodecimal ('1f36'), math.hextodecimal ('4c6d'), math.hextodecimal ('b980'), math.hextodecimal ('eadb'),
math.hextodecimal ('3e6c'), math.hextodecimal ('6d37'), math.hextodecimal ('98da'), math.hextodecimal ('cb81'), math.hextodecimal ('215a'), math.hextodecimal ('7201'), math.hextodecimal ('87ec'), math.hextodecimal ('d4b7'),
math.hextodecimal ('7cd8'), math.hextodecimal ('2f83'), math.hextodecimal ('da6e'), math.hextodecimal ('8935'), math.hextodecimal ('63ee'), math.hextodecimal ('30b5'), math.hextodecimal ('c558'), math.hextodecimal ('9603'),
math.hextodecimal ('42b4'), math.hextodecimal ('11ef'), math.hextodecimal ('e402'), math.hextodecimal ('b759'), math.hextodecimal ('5d82'), math.hextodecimal ('0ed9'), math.hextodecimal ('fb34'), math.hextodecimal ('a86f'),
math.hextodecimal ('abeb'), math.hextodecimal ('f8b0'), math.hextodecimal ('0d5d'), math.hextodecimal ('5e06'), math.hextodecimal ('b4dd'), math.hextodecimal ('e786'), math.hextodecimal ('126b'), math.hextodecimal ('4130'),
math.hextodecimal ('9587'), math.hextodecimal ('c6dc'), math.hextodecimal ('3331'), math.hextodecimal ('606a'), math.hextodecimal ('8ab1'), math.hextodecimal ('d9ea'), math.hextodecimal ('2c07'), math.hextodecimal ('7f5c'),
math.hextodecimal ('d733'), math.hextodecimal ('8468'), math.hextodecimal ('7185'), math.hextodecimal ('22de'), math.hextodecimal ('c805'), math.hextodecimal ('9b5e'), math.hextodecimal ('6eb3'), math.hextodecimal ('3de8'),
math.hextodecimal ('e95f'), math.hextodecimal ('ba04'), math.hextodecimal ('4fe9'), math.hextodecimal ('1cb2'), math.hextodecimal ('f669'), math.hextodecimal ('a532'), math.hextodecimal ('50df'), math.hextodecimal ('0384'),
math.hextodecimal ('058c'), math.hextodecimal ('56d7'), math.hextodecimal ('a33a'), math.hextodecimal ('f061'), math.hextodecimal ('1aba'), math.hextodecimal ('49e1'), math.hextodecimal ('bc0c'), math.hextodecimal ('ef57'),
math.hextodecimal ('3be0'), math.hextodecimal ('68bb'), math.hextodecimal ('9d56'), math.hextodecimal ('ce0d'), math.hextodecimal ('24d6'), math.hextodecimal ('778d'), math.hextodecimal ('8260'), math.hextodecimal ('d13b'),
math.hextodecimal ('7954'), math.hextodecimal ('2a0f'), math.hextodecimal ('dfe2'), math.hextodecimal ('8cb9'), math.hextodecimal ('6662'), math.hextodecimal ('3539'), math.hextodecimal ('c0d4'), math.hextodecimal ('938f'),
math.hextodecimal ('4738'), math.hextodecimal ('1463'), math.hextodecimal ('e18e'), math.hextodecimal ('b2d5'), math.hextodecimal ('580e'), math.hextodecimal ('0b55'), math.hextodecimal ('feb8'), math.hextodecimal ('ade3'),
math.hextodecimal ('ae67'), math.hextodecimal ('fd3c'), math.hextodecimal ('08d1'), math.hextodecimal ('5b8a'), math.hextodecimal ('b151'), math.hextodecimal ('e20a'), math.hextodecimal ('17e7'), math.hextodecimal ('44bc'),
math.hextodecimal ('900b'), math.hextodecimal ('c350'), math.hextodecimal ('36bd'), math.hextodecimal ('65e6'), math.hextodecimal ('8f3d'), math.hextodecimal ('dc66'), math.hextodecimal ('298b'), math.hextodecimal ('7ad0'),
math.hextodecimal ('d2bf'), math.hextodecimal ('81e4'), math.hextodecimal ('7409'), math.hextodecimal ('2752'), math.hextodecimal ('cd89'), math.hextodecimal ('9ed2'), math.hextodecimal ('6b3f'), math.hextodecimal ('3864'),
math.hextodecimal ('ecd3'), math.hextodecimal ('bf88'), math.hextodecimal ('4a65'), math.hextodecimal ('193e'), math.hextodecimal ('f3e5'), math.hextodecimal ('a0be'), math.hextodecimal ('5553'), math.hextodecimal ('0608'),
math.hextodecimal ('5943'), math.hextodecimal ('0a18'), math.hextodecimal ('fff5'), math.hextodecimal ('acae'), math.hextodecimal ('4675'), math.hextodecimal ('152e'), math.hextodecimal ('e0c3'), math.hextodecimal ('b398'),
math.hextodecimal ('672f'), math.hextodecimal ('3474'), math.hextodecimal ('c199'), math.hextodecimal ('92c2'), math.hextodecimal ('7819'), math.hextodecimal ('2b42'), math.hextodecimal ('deaf'), math.hextodecimal ('8df4'),
math.hextodecimal ('259b'), math.hextodecimal ('76c0'), math.hextodecimal ('832d'), math.hextodecimal ('d076'), math.hextodecimal ('3aad'), math.hextodecimal ('69f6'), math.hextodecimal ('9c1b'), math.hextodecimal ('cf40'),
math.hextodecimal ('1bf7'), math.hextodecimal ('48ac'), math.hextodecimal ('bd41'), math.hextodecimal ('ee1a'), math.hextodecimal ('04c1'), math.hextodecimal ('579a'), math.hextodecimal ('a277'), math.hextodecimal ('f12c'),
math.hextodecimal ('f2a8'), math.hextodecimal ('a1f3'), math.hextodecimal ('541e'), math.hextodecimal ('0745'), math.hextodecimal ('ed9e'), math.hextodecimal ('bec5'), math.hextodecimal ('4b28'), math.hextodecimal ('1873'),
math.hextodecimal ('ccc4'), math.hextodecimal ('9f9f'), math.hextodecimal ('6a72'), math.hextodecimal ('3929'), math.hextodecimal ('d3f2'), math.hextodecimal ('80a9'), math.hextodecimal ('7544'), math.hextodecimal ('261f'),
math.hextodecimal ('8e70'), math.hextodecimal ('dd2b'), math.hextodecimal ('28c6'), math.hextodecimal ('7b9d'), math.hextodecimal ('9146'), math.hextodecimal ('c21d'), math.hextodecimal ('37f0'), math.hextodecimal ('64ab'),
math.hextodecimal ('b01c'), math.hextodecimal ('e347'), math.hextodecimal ('16aa'), math.hextodecimal ('45f1'), math.hextodecimal ('af2a'), math.hextodecimal ('fc71'), math.hextodecimal ('099c'), math.hextodecimal ('5ac7'),
math.hextodecimal ('5ccf'), math.hextodecimal ('0f94'), math.hextodecimal ('fa79'), math.hextodecimal ('a922'), math.hextodecimal ('43f9'), math.hextodecimal ('10a2'), math.hextodecimal ('e54f'), math.hextodecimal ('b614'),
math.hextodecimal ('62a3'), math.hextodecimal ('31f8'), math.hextodecimal ('c415'), math.hextodecimal ('974e'), math.hextodecimal ('7d95'), math.hextodecimal ('2ece'), math.hextodecimal ('db23'), math.hextodecimal ('8878'),
math.hextodecimal ('2017'), math.hextodecimal ('734c'), math.hextodecimal ('86a1'), math.hextodecimal ('d5fa'), math.hextodecimal ('3f21'), math.hextodecimal ('6c7a'), math.hextodecimal ('9997'), math.hextodecimal ('cacc'),
math.hextodecimal ('1e7b'), math.hextodecimal ('4d20'), math.hextodecimal ('b8cd'), math.hextodecimal ('eb96'), math.hextodecimal ('014d'), math.hextodecimal ('5216'), math.hextodecimal ('a7fb'), math.hextodecimal ('f4a0'),
math.hextodecimal ('f724'), math.hextodecimal ('a47f'), math.hextodecimal ('5192'), math.hextodecimal ('02c9'), math.hextodecimal ('e812'), math.hextodecimal ('bb49'), math.hextodecimal ('4ea4'), math.hextodecimal ('1dff'),
math.hextodecimal ('c948'), math.hextodecimal ('9a13'), math.hextodecimal ('6ffe'), math.hextodecimal ('3ca5'), math.hextodecimal ('d67e'), math.hextodecimal ('8525'), math.hextodecimal ('70c8'), math.hextodecimal ('2393'),
math.hextodecimal ('8bfc'), math.hextodecimal ('d8a7'), math.hextodecimal ('2d4a'), math.hextodecimal ('7e11'), math.hextodecimal ('94ca'), math.hextodecimal ('c791'), math.hextodecimal ('327c'), math.hextodecimal ('6127'),
math.hextodecimal ('b590'), math.hextodecimal ('e6cb'), math.hextodecimal ('1326'), math.hextodecimal ('407d'), math.hextodecimal ('aaa6'), math.hextodecimal ('f9fd'), math.hextodecimal ('0c10'), math.hextodecimal ('5f4b')
} ;
function Build_CRC (s)
local crc = 0
local short, j
for i = 1,string.len (s) do
j = math.bxor(crc, string.byte (s,i))
crc = math.bxor(crcModTab [math.band(j, 255) +1], (math.bshiftr (crc,8)))
end
return crc
end
Build_CRC ('A100') --should return "B0 7E" "45182"
--Build_CRC ('A300') --should return "B6 49" ""
Promixis
March 29th, 2007, 12:11 PM
You will have to look more closely at each bit operation to be sure the numbers remain 8 bit. Lua numbers are not defined like C so they may contain a result different from a C binary operation.
Rob H
March 30th, 2007, 03:30 AM
Looking back up, your input data to the Build_CRC function is wrong - the example has 0xA1 0x00 (2 bytes) but you are passing in 'A100' (4 bytes).
Try calling it like this
Build_CRC(math.hextobyte('A100'))
Manser
April 2nd, 2007, 03:00 AM
Thank you very much!
It's running now!
function Build_CRC (s)
local crc = 0
local short, j
for i = 1,string.len (s) do
j = math.bxor(crc, string.byte (s,i))
crc = math.bxor(crcModTab [math.band(j, 255)+1], (math.bshiftr (crc,8)))
end
return crc
end
checksum=Build_CRC(math.hextobyte('01020300040105' ))
print ("checksum ",math.decimaltohex(checksum))
Rob H
April 2nd, 2007, 05:01 AM
Great, glad to hear it.
Promixis
April 2nd, 2007, 07:26 AM
Simon, what controller is this for? Would you mind uploading your work when done?
Manser
April 2nd, 2007, 07:44 AM
hey mike
This is for a com driver for a swiss building automation system called Twiline, It's not very common outside of the German speaking part of Europe so i'm not sure if it is very useful for other users.
But there is a lot of work to do the hole communication stuff over serial and so on.
Powered by vBulletin® Version 4.1.8 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.