Why does function with "For" loop break my serial connection?

Quote: "I wanted to try explain my problem better but couldn't do anything as every time I tried to troubleshoot more my program had been doing something else. ".

That tells us you did not begin testing your program as you wrote it, section by section. That would have let you discover the problems as they were created and then you have a chance to fix them before they multiply.

Paul