Slave Address 18 Function Code 03 Start Address (Hi) 0B Start Address (Lo) B9 Number of points (Hi) 00 Number of points (Lo) 01 CRC (Lo) 55 CRC (Hi) C2
TxMsg$ = CHR$(&H18)+CHR$(&H03)+CHR$(&H0B)+CHR$(&HB9)+CHR$(&H00)+CHR$(&H01)
TxMsg$ = TxMsg$ + CRC16$(TxMsg$)
IF CRC16$(RxMsg$) = CHR$(0) + CHR$(0) THEN ' CRC is OK ' Process the response ELSE ' Bad CRC - DO nothing - don't respond END IF
DIM INTEGER crc_table(255) ' lookup table for CRC generation genCRCtable ' generate the lookup table '------------------------------------------------------------ 'generates a lookup table for fast crc calculation SUB genCRCtable LOCAL i%, j%, k%, crc_const% = &HA001 FOR i% = 0 TO 255 k% = i%}} FOR j% = 1 TO 8 IF k% AND 1 THEN k% = k% >> 1 k% = k% XOR crc_const% ELSE k% = k% >> 1 END IF NEXT j% crc_table(i%)=k% NEXT i% END SUB ' ' returns the CRC as a string FUNCTION CRC16$(a$) LOCAL CRC% = &HFFFF, n% FOR n% = 1 TO LEN(a$) CRC% = (CRC% >> 8) XOR crc_table(CRC% XOR ASC(MID$(a$, n%, 1)) AND &HFF) NEXT n% CRC16$ = CHR$(CRC% AND &HFF) + CHR$((CRC%>>8) AND &HFF) END FUNCTION '-------------------------------------------------------------