1
1
forked from kalmar/DALGLI0

changed input

This commit is contained in:
Szymon Wojciechowski 2018-06-25 17:54:51 +00:00
parent 772736120b
commit 09dcfac9ab

View File

@ -90,24 +90,34 @@ def encode(in_data):
M = [0] * 16 + data_binary M = [0] * 16 + data_binary
L = [0] * (len(data_binary)) + [1] * 16 L = [0] * (len(data_binary)) + [1] * 16
G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
FCS_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G)) result_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G))
FCS = FCS_poly.coefficients + [0] * (16 - len(FCS_poly.coefficients)) result = result_poly.coefficients + [0] * (16 - len(result_poly.coefficients))
FCS.reverse() result.reverse()
data_binary.reverse() data_binary.reverse()
data_binary = data_binary + FCS data_binary = data_binary + result
data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)] data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)]
data = [chr(int(''.join(map(str, byte)), 2)) for byte in data_binary] data_binary = [int(''.join(map(str, byte)), 2) for byte in data_binary]
return ''.join(data) fcs = [hex(byte) for byte in data_binary]
fcs = fcs[-2:]
data = [chr(byte) for byte in data_binary]
data = ''.join(data[0:-2])
return data, fcs
def check_fcs(in_data): def check_fcs(in_data, fcs):
data = in_data data = in_data
data = list(data) data = list(data)
data_binary = [bin(ord(char)).replace('b', '') for char in data] data_binary = [bin(ord(char)).replace('b', '') for char in data]
data_binary = [normalize_byte(byte) for byte in data_binary] data_binary = [normalize_byte(byte) for byte in data_binary]
data_binary = [int(bit) for bit in list(''.join(data_binary))] data_binary = [int(bit) for bit in list(''.join(data_binary))]
fcs_binary = [bin(int(in_hex, 16)).replace('b', '') for in_hex in fcs]
fcs_binary = [normalize_byte(byte) for byte in fcs_binary]
fcs_binary = [int(bit) for bit in list(''.join(fcs_binary))]
data_binary += fcs_binary
data_binary.reverse() data_binary.reverse()
C = [0] * 16 + data_binary C = [0] * 16 + data_binary
@ -124,15 +134,17 @@ def check_fcs(in_data):
def main(): def main():
try: try:
if sys.argv[1] == '-e' or sys.argv[1] == '-encode': if sys.argv[1] == '-e' or sys.argv[1] == '-encode':
arg = encode(sys.argv[2]) arg, fcs = encode(sys.argv[2])
print(arg) print(arg, fcs)
print(check_fcs(arg)) # powinno zawsze zwracać true print(check_fcs(arg, fcs)) # powinno zawsze zwracać true
elif sys.argv[1] == '-c' or sys.argv[1] == '-check': elif sys.argv[1] == '-c' or sys.argv[1] == '-check':
print(check_fcs(sys.argv[2])) arg = sys.argv[2]
fcs = ast.literal_eval(sys.argv[3])
print(check_fcs(arg, fcs))
else: else:
raise IndexError raise IndexError
except IndexError: except IndexError:
print("To encode: python3 CRC16.py -e [argument]\nTo check FCS: python3 CRC16.py -c [argument]") print("To encode: python3 CRC16.py -e [argument]\nTo check result: python3 CRC16.py -c [argument] [fcs]")
if __name__ == "__main__": if __name__ == "__main__":