I build a state machine in structured text where the state machine moves one step forward and backwards by setting a tag to 1. I use a large case and switch to do this, that way I can idle on a step until a message completes. I usually have 2 steps per message. The first step to prep the message and send it, and the second step to wait for the message to complete or error, if it completes then I parse the result if there is one.
Then I can build the case and switch with as many steps as I need to sequence the messages. I will also add a done output bit, and a reset input bit. I also have an error bit to indicate a failure, and use the step number as the error code. On more complex systems that have multiple possible errors per step I will use the step number * 100, then use 0 to 99 within the step number as the error code for that step.