Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python เพื่อตรวจสอบ Cyclic Redundancy


สำหรับการตรวจจับข้อผิดพลาดในข้อมูลดิจิทัล CRC นั้นใช้ นี่เป็นเทคนิคที่ดีในการตรวจจับข้อผิดพลาดในการส่ง ในเทคนิคนี้ส่วนใหญ่จะใช้การหารเลขฐานสอง

ในเทคนิคเหล่านี้ มีบิตตรวจสอบความซ้ำซ้อนแบบวนซ้ำ ซึ่งเป็นลำดับของบิตที่ซ้ำซ้อน บิตเหล่านี้ถูกต่อท้ายหน่วยข้อมูลเพื่อให้หน่วยข้อมูลที่ได้หารด้วยวินาทีซึ่งเป็นเลขฐานสองที่กำหนดไว้ล่วงหน้า

ที่ฝั่งปลายทาง ข้อมูลที่เข้ามาจะถูกหารด้วยตัวเลขเดียวกัน หากไม่มีเศษเหลือ ให้ถือว่าข้อมูลนั้นถูกต้องและพร้อมที่จะยอมรับ

ส่วนที่เหลือบ่งชี้ว่ามีบางอย่างเกิดขึ้นระหว่างการเปลี่ยนผ่าน หน่วยข้อมูลได้รับความเสียหาย จึงไม่รับหน่วยข้อมูลนี้

โปรแกรม Python เพื่อตรวจสอบ Cyclic Redundancy

โค้ดตัวอย่าง

frompycrc.crclib import *
def main():
#-----------------------------------------------------------------------------
#Sender Side
div = str(input("Input divisor in binary type: "))
#user_dataword = str(raw_input("Input dataword in binary type: "))
userdataword = '1001'
print ("\nSender:")
sen = Sender(bin2dec(userdataword), div)
sen.send()
print ("arg_dataword:", sen.arg_dataword2)
print ("remainder:", sen.remainder2)
print ("codeword:", sen.codeword2)
#-----------------------------------------------------------------------------
#Channel
print ("\nChannel:")
ch = Channel(sen.codeword)
print ("Through to the channel get channel codeword:", dec2bin(ch.ch_codeword))
#-----------------------------------------------------------------------------
#Receiver Side
print ("\nReceiver:")
rcv = Receiver(ch.ch_codeword, div)
rcv.receive()
print ("syndrome:", rcv.syndrome2)
print ("Discard or not?", rcv.discard)
print ("rx_dataword:", rcv.rx_dataword2)
if __name__ == '__main__':
   main()

ผลลัพธ์

Sender
Input dataword in binary type 1010000
arg_dataword:1010000000
remainder: 011
codeword:1010000011
Receiver
syndrome:1010000011
Discard or not? N
rx_dataword:1010000011