That getgps routine is designed to be called multiple times in the course of reading a gps sentence. It builds up the string in the buffer until it sees a end of line and then parses it. The problem is that as you are switching between soft serial ports, there is opportunity to miss data. You need to restructure this so that you listen on one serial port until you have a complete sentence ($ to '\n') and then you can parse it. Then switch to the other serial port and repeat.