1
1
# Checks the integrity of u-blox binary files
2
2
3
3
# Written by: Paul Clark
4
- # Last update: August 26th 2020
4
+ # Last update: October 17th 2021
5
5
6
6
# Reads a UBX file and checks the integrity of both UBX and NMEA data
7
7
# Will rewind and re-sync if an error is found
8
+ # Will create a repaired file if desired
8
9
9
10
# SparkFun code, firmware, and software is released under the MIT License (http://opensource.org/licenses/MIT)
10
11
#
@@ -70,6 +71,17 @@ def csum(byte, sum1, sum2):
70
71
else :
71
72
containsNMEA = False
72
73
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
+
73
85
print ()
74
86
print ('Processing' ,filename )
75
87
print ()
@@ -81,7 +93,14 @@ def csum(byte, sum1, sum2):
81
93
except :
82
94
raise Exception ('Invalid file!' )
83
95
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
85
104
messages = {} # The collected message types
86
105
longest = 0 # The length of the longest UBX message
87
106
keepGoing = True
@@ -137,6 +156,9 @@ def csum(byte, sum1, sum2):
137
156
resync_in_progress = False # Flag to indicate if a resync is in progress
138
157
message_start_byte = 0 # Record where the latest message started (for resync reporting)
139
158
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
+
140
162
try :
141
163
while keepGoing :
142
164
@@ -149,6 +171,11 @@ def csum(byte, sum1, sum2):
149
171
150
172
processed = processed + 1 # Keep a record of how many bytes have been read and processed
151
173
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
+
152
179
# Process data bytes according to ubx_nmea_state
153
180
# For UBX messages:
154
181
# Sync Char 1: 0xB5
@@ -267,6 +294,18 @@ def csum(byte, sum1, sum2):
267
294
resyncs += 1 # Increment the number of successful resyncs
268
295
print ("Sync successfully re-established at byte " + str (processed )+ ". The UBX message started at byte " + str (message_start_byte ))
269
296
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
+
270
309
# NMEA messages
271
310
elif (ubx_nmea_state == looking_for_asterix ):
272
311
nmea_length = nmea_length + 1 # Increase the message length count
@@ -359,6 +398,17 @@ def csum(byte, sum1, sum2):
359
398
resyncs += 1 # Increment the number of successful resyncs
360
399
print ("Sync successfully re-established at byte " + str (processed )+ ". The NMEA message started at byte " + str (message_start_byte ))
361
400
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
362
412
363
413
# Check if the end of the file has been reached
364
414
if (processed >= filesize - 1 ): keepGoing = False
@@ -381,6 +431,9 @@ def csum(byte, sum1, sum2):
381
431
finally :
382
432
fi .close () # Close the file
383
433
434
+ if (repairFile ):
435
+ fo .close ()
436
+
384
437
# Print the file statistics
385
438
print ()
386
439
processed += 1
@@ -396,4 +449,7 @@ def csum(byte, sum1, sum2):
396
449
if (resyncs > 0 ):
397
450
print ('Number of successful resyncs:' ,resyncs )
398
451
print ()
452
+ if (repairFile ):
453
+ print ('Repaired data written to:' , repairFilename )
454
+ print ()
399
455
print ('Bye!' )
0 commit comments