Byte 1 SOH Byte 2 Packet number (0-255) Byte 3 Inverse packet number (255 - packet number) Byte 4-131 128 bytes of data Byte 132-133 16 bit CRC
TxMsg$ = TxMsg$ + XCRC$(TxMsg)
IF XCRC$(RxMsg$) = CHR$(0) + CHR$(0) THEN ' CRC is OK ' Process the message ELSE ' Ignore the message or send a NAK END IF
FUNCTION XCRC$(a$) LOCAL CRC% = &H0000, n%, j% FOR n% = 1 TO LEN(A$) CRC% = (CRC% AND &HFFFF) XOR (ASC(MID$(a$, n%, 1)) << 8) FOR j% = 1 TO 8 IF (CRC% AND &H8000) <> 0 THEN CRC% = (CRC% << 1) XOR &H1021 ELSE CRC% = CRC% << 1 END IF NEXT j% NEXT n% XCRC$ = CHR$(CRC% >> 8 AND &HFF) + CHR$(CRC% AND &HFF) END FUNCTION