This statement starts with the keyword BRANCH followed by a call to a Branch Method that obtains a reference to the statement to be branched to. The only commonly used Branch Method is FromTable. The target of a BRANCH may be either a FIELD statement or a GROUP (discussed in the next section).
The FromTable method finds the entry in a given table that matches a given value and returns the branch item from it. The first parameter (tableCommands) is the name of the table; the second (command_code) is a fieldname. The value of that field serves as the index. If the matching entry has no branch item, the method returns a value that suppresses a branch.
The fact that we can refer to the command_code field in our BRANCH reveals one crucial fact about how the protocol analyzer works: when a field is decoded, its value is not merely extracted, formatted and displayed but it is also saved. And that saved value may be used by subsequent DecoderScript statements. This is absolutely necessary in situations where the value in one field determines the length of another, and can be very handy in several other circumstances. Field values are automatically saved for the duration of the decoding of the layer.
You can also save data to be used by other layers in the same frame (intra-frame) or by other frames (inter- frame). These data persist for longer than the decoding of the layer. See the section on Intra-frame and Inter- frame Data for information on how to do this.
We will explain later what information you need to add to the Command table to allow the branch to occur. For now, assume that information is there. Because of the branch, we can remove our Set Actuator and Read Sensor groups from the main path of the decoder, and put them outside the main section. As the protocol analyzer moves through the decoder, the branch will cause it to jump to the appropriate group, decode that group, and then return to the main path at the same point as before. After altering the decoder, it looks like this:
FIELD command_code (Fixed 1 Byte) (Table tableCommands) IN_SUMMARY "Command/Response" 150 "Command"
BRANCH (FromTable tableCommands command_code)
FIELD checksum (Fixed 1 Byte) (Hex) IN_SUMMARY "C'sum" 40 "Checksum" VERIFY (FPChecksum)
END_MAIN_PATH
GROUP cmdSetActuator
{
FIELD actuator_number (Fixed 1 Byte) (Decimal) "Actuator Number" VERIFY (FieldIsBetween 1 32)
FIELD actuator_value (Fixed 2 Bytes) (Decimal) "Value to set"
}
GROUP cmdReadSensor
{
FIELD sensor_number (Fixed 1 Byte) (Decimal) "Sensor Number" VERIFY (FieldIsBetween 1 32)
}
The cmdSetActuator and cmdReadSensor groups have been moved after the END_MAIN_PATH keyword, and the BRANCH statement inserted after the Command field. The last piece of information is how to alter the Command Table.