You are here: Tips and Tricks > How to Implement a Repeating Counter

How to Implement a Repeating Counter

Sometimes it is nice to know which iteration of a repeating group the decoder is currently executing. This can be done in the following way:

FIELD rep_count (Fixed 1) (Decimal) "Number of repeats"

FIELD zero_counter ;

GROUP rep REPEAT COUNT (FromField Times rep_count)

{

FIELD print_counter (Fixed 0) RETRIEVE (StoreField counter) (decimal) "Counter"

FIELD increment_counter ;

}

END_MAIN_PATH

/* Add one to the count for the next iteration */

FIELD increment_counter (Fixed 0) RETRIEVE (StoreField counter) (Hex) SUPPRESS_DETAIL STORE counter RETRIEVE (AddInteger 1)

 

/* Initialize the counter to zero */

FIELD zero_counter (Fixed 0) (Hex) SUPPRESS_DETAIL STORE counter RETRIEVE (StoreInteger 0)

Output for a frame that begins with the byte 0x03:

Number of repeats: 3

Counter: 0

Counter: 1

Counter:2

An important point is that the increment_counter field MUST have SUPPRESS_DETAIL and not have IN_SUMMARY. Otherwise, the field is encountered twice and the count increases by two.