-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6088122_DataComm.py
122 lines (106 loc) · 4.79 KB
/
6088122_DataComm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import datacomm as dc
# mylist = [7, 8, 1]
# hi = declist2bilist(mylist)
# print(list2string(hi, max([len(x) for x in hi])))
print("Hello, this is data communication assignment 1 by Thanirin (6088122)")
print("====================================================================")
print("First of all please choose which function you would like to use.")
print(" Enter 0 to use 'Unreliable Transmission'")
print(" Enter 1 to use 'Code generators'")
print(" Enter 2 to use 'Code checkers'")
x = input("Enter a number.. ")
if x == '0':
binary = input("Please enter a string of only 0 and 1: ")
percent = input("Please enter a percentage from 0 to 1; e.g. 20% means 0.2: ")
percent = float(percent)
if not (0 <= percent <= 1):
print("Please enter valid percentage!")
error = dc.unreliable_transmission(binary, percent)
print("The output of ", binary, " with the percentage of ", percent, " error is ", error, ".")
elif x == '1':
print("You chose 'Code generators! Next please choose which algorithms you want.'")
print(" Enter 0 to use 'Parity Bit'")
print(" Enter 1 to use 'CRC'")
print(" Enter 2 to use 'Checksum'")
print(" Enter 3 to use 'Hamming Code'")
y = input("Enter a number.. ")
if y == '2':
word_block = int(input("Please enter how many words or numbers you want to input: "))
list2 = list()
for i in range(word_block):
temp = input("Enter the word: ")
list2.append(dc.ASCIItoBinary(temp))
size = max([len(x) for x in list2])
print("The output of checksum encoded string is ", dc.Checksum_gen(list2, size, word_block))
elif y == '0' or y == '1' or y == '3':
word = input("Please enter a word: ")
ASCIIword = dc.ASCIItoBinary(word)
if y == '0':
print("Please enter the type of parity bit.")
print(" Enter 0 for even parity bit")
print(" Enter 1 for odd parity bit")
print(" Enter 2 for even 2D parity bit")
print(" Enter 3 for even parity bit")
type0 = int(input("Enter a VALID number: "))
size = ""
if type0 == 2 or type0 == 3:
size = str(int(len(ASCIIword) / len(word)))
size += 'x'
size += str(len(word))
print("The output of parity bit encoded string is ", dc.parity_gen(ASCIIword, type0, size))
elif y == '1':
print("Please enter the CRC method you want.")
print(" Enter 0 for CRC-4")
print(" Enter 1 for CRC-8")
print(" Enter 2 for reversed CRC-16")
print(" Enter 3 for CRC-16")
print(" Enter 4 for CRC-24")
print(" Enter 5 for CRC-32")
type1 = int(input("Enter a VALID number: "))
print("The output of CRC encoded string is ", dc.CRC_gen(ASCIIword, type1))
elif y == '3':
print("The output of hamming code encoded string is ", dc.Hamming_gen(ASCIIword))
else:
print("Please input a valid number!")
elif x == '2':
print("You chose 'Code Checker! Next please choose which algorithms you want.'")
print(" Enter 0 to use 'Parity Bit'")
print(" Enter 1 to use 'CRC'")
print(" Enter 2 to use 'Checksum'")
print(" Enter 3 to use 'Hamming Code'")
z = input("Enter a number.. ")
bistring = input("Please enter a string of 0 and 1 only: ")
if z == '2':
word_block = int(input("Please enter how many words or numbers you have input: ")) + 1
size = int(len(bistring) / word_block)
list2 = dc.string2list(bistring, size, word_block)
print(dc.Checksum_check(list2, size, word_block))
if z == '0':
print("Please enter the type of parity bit.")
print(" Enter 0 for even parity bit")
print(" Enter 1 for odd parity bit")
print(" Enter 2 for even 2D parity bit")
print(" Enter 3 for even parity bit")
type0 = int(input("Enter a VALID number: "))
size = ""
if type0 == 2 or type0 == 3:
size = "8"
size += 'x'
size += str(int(len(bistring)/8))
print(dc.parity_check(bistring, type0, size))
elif z == '1':
print("Please enter the CRC method you want.")
print(" Enter 0 for CRC-4")
print(" Enter 1 for CRC-8")
print(" Enter 2 for reversed CRC-16")
print(" Enter 3 for CRC-16")
print(" Enter 4 for CRC-24")
print(" Enter 5 for CRC-32")
type1 = int(input("Enter a VALID number: "))
print(dc.CRC_check(bistring, type1))
elif z == '3':
print(dc.Hamming_check(bistring))
else:
print("Please input a valid number!")
else:
print("Please input a valid number!")