11# Checks the integrity of u-blox binary files
22
33# Written by: Paul Clark
4- # Last update: August 26th 2020
4+ # Last update: October 17th 2021
55
66# Reads a UBX file and checks the integrity of both UBX and NMEA data
77# Will rewind and re-sync if an error is found
8+ # Will create a repaired file if desired
89
910# SparkFun code, firmware, and software is released under the MIT License (http://opensource.org/licenses/MIT)
1011#
@@ -70,6 +71,17 @@ def csum(byte, sum1, sum2):
7071else :
7172 containsNMEA = False
7273
74+ # Ask user if the file should be repaired
75+ response = input ('Do you want to repair the file? (y/N): ' ) # Get the response
76+ if (response == '' ) or (response == 'N' ) or (response == 'n' ):
77+ repairFile = False
78+ else :
79+ repairFile = True
80+ if (filename [- 4 ] == '.' ):
81+ repairFilename = filename [:- 4 ] + '.repair' + filename [- 4 :]
82+ else :
83+ repairFilename = filename + '.repair'
84+
7385print ()
7486print ('Processing' ,filename )
7587print ()
@@ -81,7 +93,14 @@ def csum(byte, sum1, sum2):
8193except :
8294 raise Exception ('Invalid file!' )
8395
84- processed = - 1 # The nunber of bytes processed
96+ # Try to open repair file for writing
97+ if (repairFile ):
98+ try :
99+ fo = open (repairFilename ,"wb" )
100+ except :
101+ raise Exception ('Could not open repair file!' )
102+
103+ processed = - 1 # The number of bytes processed
85104messages = {} # The collected message types
86105longest = 0 # The length of the longest UBX message
87106keepGoing = True
@@ -137,6 +156,9 @@ def csum(byte, sum1, sum2):
137156resync_in_progress = False # Flag to indicate if a resync is in progress
138157message_start_byte = 0 # Record where the latest message started (for resync reporting)
139158
159+ rewind_repair_file_to = 0 # Keep a note of where to rewind the repair file to if sync is lost
160+ repaired_file_bytes = 0 # Keep a note of how many bytes have been written to the repair file
161+
140162try :
141163 while keepGoing :
142164
@@ -149,6 +171,11 @@ def csum(byte, sum1, sum2):
149171
150172 processed = processed + 1 # Keep a record of how many bytes have been read and processed
151173
174+ # Write the byte to the repair file if desired
175+ if (repairFile ):
176+ fo .write (fileBytes )
177+ repaired_file_bytes = repaired_file_bytes + 1
178+
152179 # Process data bytes according to ubx_nmea_state
153180 # For UBX messages:
154181 # Sync Char 1: 0xB5
@@ -267,6 +294,18 @@ def csum(byte, sum1, sum2):
267294 resyncs += 1 # Increment the number of successful resyncs
268295 print ("Sync successfully re-established at byte " + str (processed )+ ". The UBX message started at byte " + str (message_start_byte ))
269296 print ()
297+ if (repairFile ):
298+ fo .seek (rewind_repair_file_to ) # Rewind the repaired file
299+ repaired_file_bytes = rewind_repair_file_to
300+ fi .seek (message_start_byte ) # Copy the valid message into the repair file
301+ repaired_bytes_to_write = processed - message_start_byte
302+ fileBytes = fi .read (repaired_bytes_to_write )
303+ fo .write (fileBytes )
304+ repaired_file_bytes = repaired_file_bytes + repaired_bytes_to_write
305+ else :
306+ if (repairFile ):
307+ rewind_repair_file_to = repaired_file_bytes # Rewind repair file to here if sync is lost
308+
270309 # NMEA messages
271310 elif (ubx_nmea_state == looking_for_asterix ):
272311 nmea_length = nmea_length + 1 # Increase the message length count
@@ -359,6 +398,17 @@ def csum(byte, sum1, sum2):
359398 resyncs += 1 # Increment the number of successful resyncs
360399 print ("Sync successfully re-established at byte " + str (processed )+ ". The NMEA message started at byte " + str (message_start_byte ))
361400 print ()
401+ if (repairFile ):
402+ fo .seek (rewind_repair_file_to ) # Rewind the repaired file
403+ repaired_file_bytes = rewind_repair_file_to
404+ fi .seek (message_start_byte ) # Copy the valid message into the repair file
405+ repaired_bytes_to_write = processed - message_start_byte
406+ fileBytes = fi .read (repaired_bytes_to_write )
407+ fo .write (fileBytes )
408+ repaired_file_bytes = repaired_file_bytes + repaired_bytes_to_write
409+ else :
410+ if (repairFile ):
411+ rewind_repair_file_to = repaired_file_bytes # Rewind repair file to here if sync is lost
362412
363413 # Check if the end of the file has been reached
364414 if (processed >= filesize - 1 ): keepGoing = False
@@ -381,6 +431,9 @@ def csum(byte, sum1, sum2):
381431finally :
382432 fi .close () # Close the file
383433
434+ if (repairFile ):
435+ fo .close ()
436+
384437 # Print the file statistics
385438 print ()
386439 processed += 1
@@ -396,4 +449,7 @@ def csum(byte, sum1, sum2):
396449 if (resyncs > 0 ):
397450 print ('Number of successful resyncs:' ,resyncs )
398451 print ()
452+ if (repairFile ):
453+ print ('Repaired data written to:' , repairFilename )
454+ print ()
399455 print ('Bye!' )
0 commit comments