For purposes of a comfortable working with serial interfaces, I wrote some template of a state machine, which is easy to modify for any given application. It could one of the good way to work with some processes.
https://gist.github.com/an-dr/f8f76cfc6498ab4b04ffd31c4b3dd31d