11# Checks the integrity of u-blox binary files 
22
33# Written by: Paul Clark 
4- # Last update: February 21st , 2023 
4+ # Last update: May 10th , 2023 
55
66# Reads a UBX file and checks the integrity of UBX, NMEA and RTCM data 
77# Will rewind and re-sync if an error is found 
88# Will create a repaired file if desired 
9+ # Will print any GNTXT messages if desired 
910
1011# SparkFun code, firmware, and software is released under the MIT License (http://opensource.org/licenses/MIT) 
1112# 
@@ -106,6 +107,13 @@ def crc24q(byte, sum):
106107    else :
107108        repairFilename  =  filename  +  '.repair' 
108109
110+ # Ask user if GNTXT is to be printed 
111+ printGNTXT  =  False 
112+ if  containsNMEA  ==  True :
113+     response  =  input ('Do you want to print any GNTXT messages found? (y/N): ' ) # Get the response 
114+     if  (response  ==  'Y' ) or  (response  ==  'y' ):
115+         printGNTXT  =  True 
116+ 
109117print ()
110118print ('Processing' ,filename )
111119print ()
@@ -262,11 +270,13 @@ def crc24q(byte, sum):
262270            if  (c  ==  0xB5 ): # Have we found Sync Char 1 (0xB5) if we were expecting one? 
263271                if  (ubx_nmea_state  ==  sync_lost ):
264272                    print ("UBX Sync Char 1 (0xB5) found at byte " + str (processed ))
273+                     print ()
265274                ubx_nmea_state  =  looking_for_62  # Now look for Sync Char 2 (0x62) 
266275                message_start_byte  =  processed  # Record the message start byte for resync reporting 
267276            elif  (c  ==  0x24 ) and  (containsNMEA  ==  True ): # Have we found an NMEA '$' if we were expecting one? 
268277                if  (ubx_nmea_state  ==  sync_lost ):
269278                    print ("NMEA $ found at byte " + str (processed ))
279+                     print ()
270280                ubx_nmea_state  =  looking_for_asterix  # Now keep going until we receive an asterix 
271281                nmea_length  =  0  # Reset nmea_length then use it to check for excessive message length 
272282                nmea_csum  =  0  # Reset the nmea_csum. Update it as each character arrives 
@@ -276,9 +286,13 @@ def crc24q(byte, sum):
276286                nmea_char_4  =  0x30 
277287                nmea_char_5  =  0x30 
278288                message_start_byte  =  processed  # Record the message start byte for resync reporting 
289+                 nmea_string  =  None 
290+                 if  printGNTXT  ==  True :
291+                     nmea_string  =  '$' 
279292            elif  (c  ==  0xD3 ) and  (containsRTCM  ==  True ): # Have we found 0xD3 if we were expecting one? 
280293                if  (ubx_nmea_state  ==  sync_lost ):
281294                    print ("RTCM 0xD3 found at byte " + str (processed ))
295+                     print ()
282296                ubx_nmea_state  =  looking_for_RTCM_len1  # Now keep going until we receive the checksum 
283297                rtcm_expected_csum  =  0  # Reset the RTCM csum with a seed of 0. Update it as each character arrives 
284298                rtcm_expected_csum  =  crc24q (c , rtcm_expected_csum ) # Update expected checksum 
@@ -287,8 +301,10 @@ def crc24q(byte, sum):
287301                #print("Was expecting Sync Char 0xB5, NMEA $ or RTCM 0xD3 but did not receive one!") 
288302                if  (c  ==  0x24 ):
289303                    print ("Warning: * found at byte " + str (processed )+ "! Are you sure this file does not contain NMEA messages?" )
304+                     print ()
290305                if  (c  ==  0xD3 ):
291306                    print ("Warning: 0xD3 found at byte " + str (processed )+ "! Are you sure this file does not contain RTCM messages?" )
307+                     print ()
292308                sync_lost_at  =  processed 
293309                ubx_nmea_state  =  sync_lost 
294310
@@ -301,6 +317,7 @@ def crc24q(byte, sum):
301317            else :
302318                print ("Panic!! Was expecting Sync Char 2 (0x62) but did not receive one!" )
303319                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
320+                 print ()
304321                sync_lost_at  =  processed 
305322                resync_in_progress  =  True 
306323                ubx_nmea_state  =  sync_lost 
@@ -344,6 +361,7 @@ def crc24q(byte, sum):
344361            if  ((ubx_expected_checksum_A  !=  ubx_checksum_A ) or  (ubx_expected_checksum_B  !=  ubx_checksum_B )):
345362                print ("Panic!! UBX checksum error!" )
346363                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync." )
364+                 print ()
347365                sync_lost_at  =  processed 
348366                resync_in_progress  =  True 
349367                ubx_nmea_state  =  sync_lost 
@@ -377,12 +395,16 @@ def crc24q(byte, sum):
377395        # NMEA messages 
378396        elif  (ubx_nmea_state  ==  looking_for_asterix ):
379397            nmea_length  =  nmea_length  +  1  # Increase the message length count 
398+             if  nmea_string  is  not   None :
399+                 nmea_string  +=  chr (c )
380400            if  (nmea_length  >  max_nmea_len ): # If the length is greater than max_nmea_len, something bad must have happened (sync_lost) 
381401                print ("Panic!! Excessive NMEA message length!" )
382402                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
403+                 print ()
383404                sync_lost_at  =  processed 
384405                resync_in_progress  =  True 
385406                ubx_nmea_state  =  sync_lost 
407+                 nmea_string  =  None 
386408                continue 
387409            # If this is one of the first five characters, store it 
388410            if  (nmea_length  <=  5 ):
@@ -400,6 +422,8 @@ def crc24q(byte, sum):
400422                    message_type  =  chr (nmea_char_1 ) +  chr (nmea_char_2 ) +  chr (nmea_char_3 ) +  chr (nmea_char_4 ) +  chr (nmea_char_5 ) # Record the message type 
401423                    if  (message_type  ==  "PUBX," ): # Remove the comma from PUBX 
402424                        message_type  =  "PUBX" 
425+                     if  (message_type  !=  "GNTXT" ): # Reset nmea_string if this is not GNTXT 
426+                         nmea_string  =  None 
403427            # Now check if this is an '*' 
404428            if  (c  ==  0x2A ):
405429                # Asterix received 
@@ -420,6 +444,8 @@ def crc24q(byte, sum):
420444        elif  (ubx_nmea_state  ==  looking_for_csum1 ):
421445            # Store the first NMEA checksum character 
422446            nmea_csum1  =  c 
447+             if  nmea_string  is  not   None :
448+                 nmea_string  +=  chr (c )
423449            ubx_nmea_state  =  looking_for_csum2 
424450        elif  (ubx_nmea_state  ==  looking_for_csum2 ):
425451            # Store the second NMEA checksum character 
@@ -429,30 +455,40 @@ def crc24q(byte, sum):
429455                # The checksum does not match so sync_lost 
430456                print ("Panic!! NMEA checksum error!" )
431457                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
458+                 print ()
432459                sync_lost_at  =  processed 
433460                resync_in_progress  =  True 
434461                ubx_nmea_state  =  sync_lost 
462+                 nmea_string  =  None 
435463            else :
436464                # Checksum was valid so wait for the terminators 
465+                 if  nmea_string  is  not   None :
466+                     nmea_string  +=  chr (c )
437467                ubx_nmea_state  =  looking_for_term1 
438468        elif  (ubx_nmea_state  ==  looking_for_term1 ):
439469            # Check if this is CR 
440470            if  (c  !=  0x0D ):
441471                print ("Panic!! NMEA CR not found!" )
442472                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
473+                 print ()
443474                sync_lost_at  =  processed 
444475                resync_in_progress  =  True 
445476                ubx_nmea_state  =  sync_lost 
477+                 nmea_string  =  None 
446478            else :
479+                 if  nmea_string  is  not   None :
480+                     nmea_string  +=  chr (c )
447481                ubx_nmea_state  =  looking_for_term2 
448482        elif  (ubx_nmea_state  ==  looking_for_term2 ):
449483            # Check if this is LF 
450484            if  (c  !=  0x0A ):
451485                print ("Panic!! NMEA LF not found!" )
452486                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
487+                 print ()
453488                sync_lost_at  =  processed 
454489                resync_in_progress  =  True 
455490                ubx_nmea_state  =  sync_lost 
491+                 nmea_string  =  None 
456492            else :
457493                # Valid NMEA message was received. Check if we have seen this message type before 
458494                if  message_type  in  messages :
@@ -461,6 +497,11 @@ def crc24q(byte, sum):
461497                    messages [message_type ] =  1  # if we have not, set its count to 1 
462498                if  (nmea_length  >  longest_NMEA ): # Update the longest NMEA message length 
463499                    longest_NMEA  =  nmea_length 
500+                 # Print GNTXT 
501+                 if  nmea_string  is  not   None  and  printGNTXT  ==  True :
502+                     nmea_string  +=  chr (c )
503+                     print (nmea_string )
504+                     nmea_string  =  None 
464505                # LF was received so go back to looking for B5 or a $ 
465506                ubx_nmea_state  =  looking_for_B5_dollar_D3 
466507                rewind_in_progress  =  False  # Clear rewind_in_progress 
@@ -542,6 +583,7 @@ def crc24q(byte, sum):
542583            if  (rtcm_expected_csum  !=  rtcm_actual_csum ):
543584                print ("Panic!! RTCM checksum error!" )
544585                print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync." )
586+                 print ()
545587                sync_lost_at  =  processed 
546588                resync_in_progress  =  True 
547589                ubx_nmea_state  =  sync_lost 
@@ -587,6 +629,7 @@ def crc24q(byte, sum):
587629                keepGoing  =  False 
588630            else :
589631                print ("Sync has been lost. Currently processing byte " + str (processed )+ ". Rewinding to byte " + str (rewind_to ))
632+                 print ()
590633                fi .seek (rewind_to ) # Rewind the file 
591634                processed  =  rewind_to  -  1  # Rewind processed too! (-1 is needed as processed is incremented at the start of the loop) 
592635                rewind_in_progress  =  True  # Flag that a rewind is in progress 
0 commit comments