crc16 calculator
Jan 26, 2017 15:09:15 GMT 1
Post by pebau on Jan 26, 2017 15:09:15 GMT 1
'Check your calculated CRC:
'https://www.lammertbies.nl/comm/info/crc-calculation.html
'How to compile:
'bacon64>./bacon -l -L. crc16_26.1.2017.bac
'Converting 'crc16_26.1.2017.bac'... done, 62 lines were processed in 0.067 seconds.
'Compiling 'crc16_26.1.2017.bac'... cc -c crc16_26.1.2017.bac.c
'cc -o crc16_26.1.2017 crc16_26.1.2017.bac.o -lbacon -lm -L. -lm -L.
'Done, program 'crc16_26.1.2017' ready.
'
'Test:
'bacon64>./crc16_26.1.2017
'CRC Input:123456789
'Result(dec): 10673
'Result(hex): 29B1
'Result(hex/inverted): D64E
'CRC: D64E
'https://www.lammertbies.nl/comm/info/crc-calculation.html
GLOBAL index%
hex_telegram$= ""
OPTION MEMTYPE char
'--------------------------------
GLOBAL ch TYPE uint16_t
GLOBAL data_pointer TYPE int
'-------------------------------------------'
' fuction definitions ---------------------'
'-------------------------------------------'
FUNCTION crc16$(STRING param$)
LOCAL crc TYPE uint16_t
LOCAL mask TYPE uint16_t
LOCAL poly TYPE uint16_t
crc = DEC("FFFF")
mask = DEC("FFFF")
LOCAL crc$=""
length=LEN(param$)
data_pointer=1
poly = DEC("1021")
PRINT "CRC Input:",param$
WHILE (length >0)
ch=ASC(MID$(param$,data_pointer,1))
ch = ch << 8
'PRINT "ch: ", HEX$(ch)
FOR i=0 TO 7
IF ((crc ^ ch) & DEC("8000")) > 0 THEN
xor_flag=1
'PRINT "XOR=1"
ELSE
xor_flag=0
'PRINT "XOR=0"
ENDIF
crc = crc << 1
IF (xor_flag) THEN
crc = crc ^ poly
ENDIF
ch = ch << 1
NEXT i
data_pointer=data_pointer+1
length=length-1
WEND
PRINT "Result(dec): ",crc
PRINT "Result(hex): ",HEX$(crc)
PRINT "Result(hex/inverted): ",HEX$(DEC("0xFFFF") ^crc)
PRINT (mask ^ crc) FORMAT "%04X" TO crc$
RETURN crc$
END FUNCTION
'----------------------------------------------------------'
' main program '
'----------------------------------------------------------'
crc_input$="123456789"
crc_result$=crc16$(crc_input$)
PRINT "CRC: ",crc_result$
'How to compile:
'bacon64>./bacon -l -L. crc16_26.1.2017.bac
'Converting 'crc16_26.1.2017.bac'... done, 62 lines were processed in 0.067 seconds.
'Compiling 'crc16_26.1.2017.bac'... cc -c crc16_26.1.2017.bac.c
'cc -o crc16_26.1.2017 crc16_26.1.2017.bac.o -lbacon -lm -L. -lm -L.
'Done, program 'crc16_26.1.2017' ready.
'
'Test:
'bacon64>./crc16_26.1.2017
'CRC Input:123456789
'Result(dec): 10673
'Result(hex): 29B1
'Result(hex/inverted): D64E
'CRC: D64E