Hi Henrik.
How did you declare MB_i variable? Byte or Word?
Also instead of this:
try this one:Code:MB_CRC = 65535 MB_k = MB_Buffer_Ptr - 3 ' Find out where in the array to stop. FOR MB_i = 0 to MB_k ' Iterate thru the frame, stop before the last two bytes which are the actual CRC. MB_CRC = MB_CRC ^ MB_Buffer[MB_i] ' Bitwise EXOR the current low byte of the CRC "value" with the byte in the buffer For MB_j = 0 to 7 ' Cycle thru the low byte of the CRC value If MB_CRC.Bit0 THEN ' If the LSB is set MB_CRC = MB_CRC >> 1 ' We shift CRC on bit to the right.... MB_CRC = $A001 ^ MB_CRC ' and EXOR it with out polynomial for MODBUS CRC-16 ($A001) ELSE MB_CRC = MB_CRC >> 1 ' If the LSB is not set we simply shift CRC one bit to the right. ENDIF Next ' Next bit in the byte NEXT ' Next byte in the frame RETURN
IoannisCode:MB_CRC = 65535 MB_k = MB_Buffer_Ptr - 3 ' Find out where in the array to stop. FOR MB_i = 0 to MB_k ' Iterate thru the frame, stop before the last two bytes which are the actual CRC. MB_CRC = MB_CRC ^ MB_Buffer[MB_i] ' Bitwise EXOR the current low byte of the CRC "value" with the byte in the buffer For MB_j = 0 to 7 ' Cycle thru the low byte of the CRC value MB_CRC = MB_CRC >> 1 If MB_CRC.Bit0 THEN MB_CRC = $A001 ^ MB_CRC ' If the LSB is set ' and EXOR it with out polynomial for MODBUS CRC-16 ($A001) Next ' Next bit in the byte NEXT ' Next byte in the frame RETURN




Bookmarks