data = b''
BIG_MSG = 45 #a size bigger than the longest msg we care to decode
PREAMBLE = 6 #this is the length of the preamble 0x55 plus the length and the message type bytes.
GPSTimeOfWeek = 0
while True:
data += serialPort.read(100)
while True:
index = data.find(b"\x55") #the location of the first preamble byte
#this line checks to see if there is enough data that it makes sense to even attempt to process the message.
if len(data) < index + BIG_MSG or index == -1:
continue
length = data[index+5] #the length of the actual message
output = ''
msgType = data[index+1:index+3] #you can get these magic numbers from the SBP message specification
#print("time: %s length: %s index: %s" % (struct.unpack('<i',data[index+6:index+10])[0], len(data), index))
if msgType == b"\x0c\x02": #Rover NED's
output = {
'GPS_TimeOfWeek' : (struct.unpack('<i',data[index+6:index+10])[0]),#in milliseconds
'north' : struct.unpack('<i',data[index+10:index+14])[0]*0.001,
'east' : struct.unpack('<i',data[index+14:index+18])[0]*0.001,
#FIXFLAG is an enumeration for readability in the log... you can just put the number if you wish.
'fixmode' : FIXFLAG(data[index+27]).name
}
data = data[index+length+PREAMBLE:] #chop the data chunk we've already used.