mmbasic:modbus_crc_cfunction_for_micromite
Modbus CRC CFunction For Micromite
This CRC (Cyclic Redundancy Check) is for the Modbus RTU protocol as used in RS232 and RS485 communications. The CRC is a 16 bit word that is calculated from all of the data in a message. It cannot correct any data corruption but it will indicate if a message has been corrupted in some way. This program for a Micromite uses a CFunction for a to calculate the CRC.
This page is based on this TBS post, courtesy @GerryL and @Turbo46
Micromite Modbus CRC CFunction
' MODBUS CRC CFunction for the Micrmite Mk2 test and demo CFUNCTION ModbusCRC 00000000 90870000 18E00012 3402FFFF 00803021 00873821 2405A001 90C30001 00431026 24030008 30440001 10800003 00021042 00451026 3042FFFF 2463FFFF 1460FFFA 30440001 24C60001 54C7FFF4 90C30001 03E00008 00001821 END CFUNCTION ' Build the message msg$ = CHR$(01)+CHR$(03)+CHR$(00)+CHR$(00)+CHR$(00)+CHR$(02) ' Get the CRC MBCRC% = ModbusCRC(msg$) ' Add the CRC to the message - reverse low byte and highbyte msg$ = msg$ + chr$(MBCRC% and &HFF) + CHR$((MBCRC%>>8) and &HFF) print "The message with CRC printstr(msg$) ' Test the recieved message: ' The CRC of the message including the recieved CRC should be zero print: print "Test a recieved message includind CRC" print HEX$(MBCRC% = ModbusCRC(msg$) , 4); " Should be zero" ' Print a string as HEX numbers sub PrintStr(b$) for i = 1 to len(b$) print Hex$(asc(mid$(b$,i,1)),2); ", "; next i print end sub
mmbasic/modbus_crc_cfunction_for_micromite.txt · Last modified: by Gerry
