This is possible with a recursive approach and an array of commands to send / answers to expect but I’d say - in my humble opinion - that if you got to step 3 fine (got the OK answer) and step 4 fails, then you need to retry step 4, there are rarely needs to redo step 3, 2 or 1 if they have already been successful - and this actually might be counter productive. Repeating a few times a step that fails is ok to consider, hence the idea of the progressiveReset() function I mentioned in post #8 to which you pass the failed command. If after repeating a couple times it does not work, you are probably better off rebooting (either the slave device or the whole thing if it fails again - hence this idea of ‘progressive’)