(编辑:jimmy 日期: 2024/11/17 浏览:2)
本篇文章,完全是用来记录代码用的,目的是使用Python,基于Tkinter编写crc校验工具。
# -*- coding: utf-8 -*- import Tkinter import tkFileDialog WIDTH = 16 TOPBIT = (1 << (WIDTH - 1)) crcTable = {} class FindLocation(object): def __init__(self): #创建主窗口,用于容纳其它组件 self.root = Tkinter.Tk() #给主窗口设置标题内容 self.root.title("获取bin文件的crc校验码") #创建一个输入框,并设置尺寸 self.default_value = Tkinter.StringVar() self.default_value.set('请输入bin文件全路径') self.ip_input = Tkinter.Entry(self.root,width=30,textvariable = self.default_value) #创建一个回显列表 self.display_info = Tkinter.Listbox(self.root,width=50) #创建一个搜索文件的按钮 self.getPath_button = Tkinter.Button(self.root,command = self.getPath,text = "获取文件") #创建一个查询结果的按钮 self.result_button = Tkinter.Button(self.root,command = self.crcFast,text = "获取校验码") def gui_arrang(self): self.ip_input.pack() self.display_info.pack() self.getPath_button.pack() self.result_button.pack() def getPath(self): self.fname = tkFileDialog.askopenfilename() self.default_value.set(self.fname) def crcInit(self): SHIFT = WIDTH - 8 for step in range(0,256): remainder = step << SHIFT for bit in range(8,0,-1): if remainder & TOPBIT: remainder = ((remainder << 1) & 0xFFFF) ^0x1021 else: remainder = remainder <<1 crcTable[step]=remainder #print("%x"%remainder) def crc16(self,cCRC,szData,rLen): for len in range(rLen): data = (cCRC 8) ^ ord(szData[len]) cCRC = crcTable[data] ^ (cCRC << 8) & 0xFFFF return cCRC def crcFast(self): filePath = self.ip_input.get() tLen = 0 rLen = 0 szData = 0 cCRC = 0 self.crcInit() fp = open(filePath,'rb') fp.seek(0,2) iFileLen = fp.tell() fp.seek(0,0) while True: if (iFileLen - tLen) < 8192: rLen = iFileLen - tLen else: rLen = 8192 szData = fp.read(rLen) cCRC = self.crc16(cCRC,szData,rLen) tLen += rLen if tLen >= iFileLen: break print "crc is %x\n" % cCRC fp.close() self.display_info.insert(0,"%x"%cCRC) self.display_info.insert(0,"this bin crc is :") def main(): #初始化对象 FL = FindLocation() #进行布局 FL.gui_arrang() #主程序执行 Tkinter.mainloop() if __name__ == '__main__': main()
出差必备
买火车票、高铁票、机票,订酒店都打9折的出行工具TRIP,点击注册