May-10-2021, 07:30 PM
If I had to do this myself, I would first split on newlines, then split on commas. You can then ignore any lines that don't have the $GPGGA as the message.
rxData = b'$GPRMC,111956.00,A,4030.32925,N,00353.76292,W,0.208,,100521,,,A*61\r\n$GPVTG,,T,,M,0.208,N,0.384,K,A*26\r\n$GPGGA,111956.00,4030.32925,N,00353.76292,W,1,05,1.55,718.1,M,50.2,M,,*4C\r\n$GPGSA,A,3,31,29,25,26,18,,,,,,,,3.81,1.55,3.48*05\r\n$GPGSV,3,1,11,04,08,319,,0' rxData = rxData.decode() for line in rxData.splitlines(): data = line.split(",") if data[0] == "$GPGGA": print(data)
Output:['$GPGGA', '111956.00', '4030.32925', 'N', '00353.76292', 'W', '1', '05', '1.55', '718.1', 'M', '50.2', 'M', '', '*4C']
But there are modules out there that can read your stream directly and parse the NMEA sentences. I don't have your raw stream, so I can't demonstrate that. But I can use pynmeagps to parse that line.from pynmeagps import NMEAReader rxData = b'$GPRMC,111956.00,A,4030.32925,N,00353.76292,W,0.208,,100521,,,A*61\r\n$GPVTG,,T,,M,0.208,N,0.384,K,A*26\r\n$GPGGA,111956.00,4030.32925,N,00353.76292,W,1,05,1.55,718.1,M,50.2,M,,*4C\r\n$GPGSA,A,3,31,29,25,26,18,,,,,,,,3.81,1.55,3.48*05\r\n$GPGSV,3,1,11,04,08,319,,0' rxData = rxData.decode() for line in rxData.splitlines(): data = line.split(",") if data[0] == "$GPGGA": print(NMEAReader.parse(line))
Output:<NMEA(GPGGA, time=11:19:56, lat=40.505488, NS=N, lon=-3.896049, EW=W, quality=1, numSV=5, HDOP=1.55, alt=718.1, altUnit=M, sep=50.2, sepUnit=M, diffAge=, diffStation=)>