Skip to content

Instantly share code, notes, and snippets.

@jsantanders
Created November 27, 2016 03:33
Show Gist options
  • Save jsantanders/b10f004c5eddcf23ce14641ad2bcceb7 to your computer and use it in GitHub Desktop.
Save jsantanders/b10f004c5eddcf23ce14641ad2bcceb7 to your computer and use it in GitHub Desktop.

Revisions

  1. Jesus Alberto created this gist Nov 27, 2016.
    51 changes: 51 additions & 0 deletions CRC16.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    import random

    class CRC16:
    #codificacion de bits basado en el estandar CRC-12
    def __init__(self, word):
    self.word = word
    print("La trama a transmitir es:\n", bin(int(self.word,2)))
    G = 0b11000000000000101 #Polinomio generador G(x)

    def mod2(self,data):
    Glist = [1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1]
    Wordlist = [int(d) for d in str(bin(data))[2:]]
    while len(Glist) <= len(Wordlist) and Wordlist:
    if Wordlist[0] == Glist[0]:
    del Wordlist[0]
    for j in range(len(Glist)-1):
    Wordlist[j] ^= Glist[j+1]
    while Wordlist and Wordlist[0] == 0:
    del Wordlist[0]
    if not Wordlist:
    return 0
    else:
    CRCstring = ''.join(str(e) for e in Wordlist)
    CRCdata = int(CRCstring,2)
    return CRCdata

    def M(self):
    stringdata = self.word + '0000000000000000' #Agregar 13 bits 0 a la palabra
    dataM = int(stringdata,2) #Convertirlos a binarios
    CRC = self.mod2(dataM) #Operacion Mod2
    CRCfix = format(CRC, 'b').zfill(16)
    Tstring = str(self.word) + CRCfix #Palabra binaria
    T = int(Tstring,2)
    return T

    def verificar(word):
    if Trama.mod2(word) == 0:
    print("La transmisión se realizó sin errores")
    else:
    print("Hubo errores en la transmisión")

    #Parte principal del programa

    Wordinput = input("Ingrese la trama binaria:\n")
    Trama = CRC16(Wordinput) #Objeto de la clase CRC16
    print("La palabra códificada es: \n", bin(Trama.M()))

    biterror = random.randint(0,1)
    wordrecived = biterror + Trama.M()
    print("La palabra recibida es:\n",bin(wordrecived))
    verificar(wordrecived)