Modulo two Division
Modulo two-division is required to calculate the CRC. Explaining the working of this code is beyond the scope of this article, the curious ones can refer Modulo two division.
Save this file as calc.py.
Python3
def xor(a, b): result = [] for i in range ( 1 , len (b)): if a[i] = = b[i]: result.append( '0' ) else : result.append( '1' ) return ''.join(result) def mod2div(dividend, divisor): pick = len (divisor) tmp = dividend[ 0 : pick] while pick < len (dividend): if tmp[ 0 ] = = '1' : tmp = xor(divisor, tmp) + dividend[pick] else : tmp = xor( '0' * pick, tmp) + dividend[pick] pick + = 1 if tmp[ 0 ] = = '1' : tmp = xor(divisor, tmp) else : tmp = xor( '0' * pick, tmp) return tmp if __name__ = = "__main__" : dividend = "10010101" divisor = "011010" print (dividend + " % " + divisor + " = " + mod2div(dividend, divisor)) |
Output:
10010101 % 011010 = 01001
Python – Stop & Wait Implementation using CRC
Stop and wait protocol is an error control protocol, in this protocol the sender sends data packets one at a time and waits for positive acknowledgment from the receiver’s side, if acknowledgment is received then the sender sends the next data packet else it’ll resend the previous packet until a positive acknowledgment is not received.
Note: To get more info on what is stop and wait protocol, refer Stop and Wait ARQ article.
CRC aka Cyclic redundancy check is an error detection mechanism, its procedure is as follows.