giovedì 14 febbraio 2019

UBX Parser Python

un lettore di file derivanti da progetto Zero

-----------------------------
import sys
contatore = 0

#i messaggi UBX-RXM-RAWX hanno classe 2 e id_messaggio 15

with open("02121413.TXT") as f:
  while True:
    c = f.read(1)
    if not c:
        break
    if (ord(c) == 0xB5):
        contatore = contatore + 1
        print(str(contatore) +"   ###########  ")
        print("Sync Char 1   " + hex(ord(c)))
        c=f.read(1)
        if (ord(c) == 0x62):
            buffer = []
            print("Sync Char 2   " + hex(ord(c)))
            ########## CLASS ######################
            c=f.read(1)
            print "Class   " + hex(ord(c))
            buffer.append(ord(c))
            ######### MESSAGE #######################
            c=f.read(1)
            print "Message " + hex(ord(c))
            buffer.append(ord(c))
            #############P1 ##########################
            c=f.read(1)
            print "Lunghezza Payload HB " + hex(ord(c))
            hb = ord(c)
            buffer.append(ord(c))
            #############P1 ##########################
            c=f.read(1)
            print "Lunghezza Payload LB  " + hex(ord(c))
            lb = ord(c)
            buffer.append(ord(c))
            ###########LUNGHEZZA PAYLOAD ##############
            lunghezza = hb+(lb*255) 
            print "Lunghezza Payload " + str(lunghezza)
            #inizia a leggere il payload
            if (lunghezza < 1000):
                    for count in range(1,lunghezza):
                        c=f.read(1)
                        #inserisce il payload in un buffer per validarlo
                        buffer.append(ord(c))
             
                    #calcolo del chacksum 
                    # https://gist.github.com/tomazas/3ab51f91cdc418f5704d
                    #CALCOLA CHECKSUM E STAMPA PAYLOAD
                    CK_A,CK_B = 0, 0
                    for i in range(len(buffer)):
                        print hex(buffer[i])
                        CK_A = CK_A + buffer[i]
                        CK_B = CK_B + CK_A

                    # ensure unsigned byte range
                    CK_A = CK_A & 0xFF
                    CK_B = CK_B & 0xFF

                    print "UBX packet checksum:", ("0x%02X,0x%02X" % (CK_A,CK_B))
                  
                    #lettura primo checksum   
                    c=f.read(1)
                    ck_a1 = ord(c)
                    print "CK_A ric " + hex(ck_a1)
                    #lettura secondo checksum
                    c=f.read(1)
                    ck_b1 = ord(c)
                    print "CK_B ric " + hex(ck_b1)
                    
                    if (CK_A == ck_a1):
                        print "############################VALIDATO############"
                    print "\n\n\n"
                    sys.stdin.readline()

Nessun commento:

Posta un commento

Sigaretta Elettronica

Avevo visto qualche tempo fa un documentario sulla RAI Svizzera sul carico inquinante delle sigarette elettroniche monouso Ne ho trovata una...