1
1
# Checks the integrity of u-blox binary files
2
2
3
3
# Written by: Paul Clark
4
- # Last update: February 21st , 2023
4
+ # Last update: May 10th , 2023
5
5
6
6
# Reads a UBX file and checks the integrity of UBX, NMEA and RTCM data
7
7
# Will rewind and re-sync if an error is found
8
8
# Will create a repaired file if desired
9
+ # Will print any GNTXT messages if desired
9
10
10
11
# SparkFun code, firmware, and software is released under the MIT License (http://opensource.org/licenses/MIT)
11
12
#
@@ -106,6 +107,13 @@ def crc24q(byte, sum):
106
107
else :
107
108
repairFilename = filename + '.repair'
108
109
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
+
109
117
print ()
110
118
print ('Processing' ,filename )
111
119
print ()
@@ -262,11 +270,13 @@ def crc24q(byte, sum):
262
270
if (c == 0xB5 ): # Have we found Sync Char 1 (0xB5) if we were expecting one?
263
271
if (ubx_nmea_state == sync_lost ):
264
272
print ("UBX Sync Char 1 (0xB5) found at byte " + str (processed ))
273
+ print ()
265
274
ubx_nmea_state = looking_for_62 # Now look for Sync Char 2 (0x62)
266
275
message_start_byte = processed # Record the message start byte for resync reporting
267
276
elif (c == 0x24 ) and (containsNMEA == True ): # Have we found an NMEA '$' if we were expecting one?
268
277
if (ubx_nmea_state == sync_lost ):
269
278
print ("NMEA $ found at byte " + str (processed ))
279
+ print ()
270
280
ubx_nmea_state = looking_for_asterix # Now keep going until we receive an asterix
271
281
nmea_length = 0 # Reset nmea_length then use it to check for excessive message length
272
282
nmea_csum = 0 # Reset the nmea_csum. Update it as each character arrives
@@ -276,9 +286,13 @@ def crc24q(byte, sum):
276
286
nmea_char_4 = 0x30
277
287
nmea_char_5 = 0x30
278
288
message_start_byte = processed # Record the message start byte for resync reporting
289
+ nmea_string = None
290
+ if printGNTXT == True :
291
+ nmea_string = '$'
279
292
elif (c == 0xD3 ) and (containsRTCM == True ): # Have we found 0xD3 if we were expecting one?
280
293
if (ubx_nmea_state == sync_lost ):
281
294
print ("RTCM 0xD3 found at byte " + str (processed ))
295
+ print ()
282
296
ubx_nmea_state = looking_for_RTCM_len1 # Now keep going until we receive the checksum
283
297
rtcm_expected_csum = 0 # Reset the RTCM csum with a seed of 0. Update it as each character arrives
284
298
rtcm_expected_csum = crc24q (c , rtcm_expected_csum ) # Update expected checksum
@@ -287,8 +301,10 @@ def crc24q(byte, sum):
287
301
#print("Was expecting Sync Char 0xB5, NMEA $ or RTCM 0xD3 but did not receive one!")
288
302
if (c == 0x24 ):
289
303
print ("Warning: * found at byte " + str (processed )+ "! Are you sure this file does not contain NMEA messages?" )
304
+ print ()
290
305
if (c == 0xD3 ):
291
306
print ("Warning: 0xD3 found at byte " + str (processed )+ "! Are you sure this file does not contain RTCM messages?" )
307
+ print ()
292
308
sync_lost_at = processed
293
309
ubx_nmea_state = sync_lost
294
310
@@ -301,6 +317,7 @@ def crc24q(byte, sum):
301
317
else :
302
318
print ("Panic!! Was expecting Sync Char 2 (0x62) but did not receive one!" )
303
319
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
320
+ print ()
304
321
sync_lost_at = processed
305
322
resync_in_progress = True
306
323
ubx_nmea_state = sync_lost
@@ -344,6 +361,7 @@ def crc24q(byte, sum):
344
361
if ((ubx_expected_checksum_A != ubx_checksum_A ) or (ubx_expected_checksum_B != ubx_checksum_B )):
345
362
print ("Panic!! UBX checksum error!" )
346
363
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync." )
364
+ print ()
347
365
sync_lost_at = processed
348
366
resync_in_progress = True
349
367
ubx_nmea_state = sync_lost
@@ -377,12 +395,16 @@ def crc24q(byte, sum):
377
395
# NMEA messages
378
396
elif (ubx_nmea_state == looking_for_asterix ):
379
397
nmea_length = nmea_length + 1 # Increase the message length count
398
+ if nmea_string is not None :
399
+ nmea_string += chr (c )
380
400
if (nmea_length > max_nmea_len ): # If the length is greater than max_nmea_len, something bad must have happened (sync_lost)
381
401
print ("Panic!! Excessive NMEA message length!" )
382
402
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
403
+ print ()
383
404
sync_lost_at = processed
384
405
resync_in_progress = True
385
406
ubx_nmea_state = sync_lost
407
+ nmea_string = None
386
408
continue
387
409
# If this is one of the first five characters, store it
388
410
if (nmea_length <= 5 ):
@@ -400,6 +422,8 @@ def crc24q(byte, sum):
400
422
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
401
423
if (message_type == "PUBX," ): # Remove the comma from PUBX
402
424
message_type = "PUBX"
425
+ if (message_type != "GNTXT" ): # Reset nmea_string if this is not GNTXT
426
+ nmea_string = None
403
427
# Now check if this is an '*'
404
428
if (c == 0x2A ):
405
429
# Asterix received
@@ -420,6 +444,8 @@ def crc24q(byte, sum):
420
444
elif (ubx_nmea_state == looking_for_csum1 ):
421
445
# Store the first NMEA checksum character
422
446
nmea_csum1 = c
447
+ if nmea_string is not None :
448
+ nmea_string += chr (c )
423
449
ubx_nmea_state = looking_for_csum2
424
450
elif (ubx_nmea_state == looking_for_csum2 ):
425
451
# Store the second NMEA checksum character
@@ -429,30 +455,40 @@ def crc24q(byte, sum):
429
455
# The checksum does not match so sync_lost
430
456
print ("Panic!! NMEA checksum error!" )
431
457
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
458
+ print ()
432
459
sync_lost_at = processed
433
460
resync_in_progress = True
434
461
ubx_nmea_state = sync_lost
462
+ nmea_string = None
435
463
else :
436
464
# Checksum was valid so wait for the terminators
465
+ if nmea_string is not None :
466
+ nmea_string += chr (c )
437
467
ubx_nmea_state = looking_for_term1
438
468
elif (ubx_nmea_state == looking_for_term1 ):
439
469
# Check if this is CR
440
470
if (c != 0x0D ):
441
471
print ("Panic!! NMEA CR not found!" )
442
472
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
473
+ print ()
443
474
sync_lost_at = processed
444
475
resync_in_progress = True
445
476
ubx_nmea_state = sync_lost
477
+ nmea_string = None
446
478
else :
479
+ if nmea_string is not None :
480
+ nmea_string += chr (c )
447
481
ubx_nmea_state = looking_for_term2
448
482
elif (ubx_nmea_state == looking_for_term2 ):
449
483
# Check if this is LF
450
484
if (c != 0x0A ):
451
485
print ("Panic!! NMEA LF not found!" )
452
486
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync" )
487
+ print ()
453
488
sync_lost_at = processed
454
489
resync_in_progress = True
455
490
ubx_nmea_state = sync_lost
491
+ nmea_string = None
456
492
else :
457
493
# Valid NMEA message was received. Check if we have seen this message type before
458
494
if message_type in messages :
@@ -461,6 +497,11 @@ def crc24q(byte, sum):
461
497
messages [message_type ] = 1 # if we have not, set its count to 1
462
498
if (nmea_length > longest_NMEA ): # Update the longest NMEA message length
463
499
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
464
505
# LF was received so go back to looking for B5 or a $
465
506
ubx_nmea_state = looking_for_B5_dollar_D3
466
507
rewind_in_progress = False # Clear rewind_in_progress
@@ -542,6 +583,7 @@ def crc24q(byte, sum):
542
583
if (rtcm_expected_csum != rtcm_actual_csum ):
543
584
print ("Panic!! RTCM checksum error!" )
544
585
print ("Sync lost at byte " + str (processed )+ ". Attemting to re-sync." )
586
+ print ()
545
587
sync_lost_at = processed
546
588
resync_in_progress = True
547
589
ubx_nmea_state = sync_lost
@@ -587,6 +629,7 @@ def crc24q(byte, sum):
587
629
keepGoing = False
588
630
else :
589
631
print ("Sync has been lost. Currently processing byte " + str (processed )+ ". Rewinding to byte " + str (rewind_to ))
632
+ print ()
590
633
fi .seek (rewind_to ) # Rewind the file
591
634
processed = rewind_to - 1 # Rewind processed too! (-1 is needed as processed is incremented at the start of the loop)
592
635
rewind_in_progress = True # Flag that a rewind is in progress
0 commit comments