python进行crc校验的方法小感
短信预约 -IT技能 免费直播动态提醒
import serial
import re
import time
'''
读卡器操作类:
'''
class reader():
baud_rate = 115200
device_name = ''
timeout = 1
ser = None
@staticmethod
def opencom(device_name):
try:
reader.ser = serial.Serial(reader.device_name,reader.baud_rate,timeout = reader.timeout)
return True
except Exception, e:
return False
@staticmethod
def com_detect():
reader.device_name = '/dev/ttyUSB0'
if reader.opencom(reader.device_name):
return True
else:
reader.device_name = '/dev/ttyUSB1'
if reader.opencom(reader.device_name):
return True
else:
reader.device_name = '/dev/ttyUSB2'
if reader.opencom(reader.device_name):
return True
else:
reader.device_name = 'COM2'
if reader.opencom(reader.device_name):
return True
else:
return False
@staticmethod
def _crc1(data = '0001020304050607080900102030405060708090'):
datalist = re.findall(r'(.{2})',data)
crcnum = 0
for i in datalist:
try:
num = int(i,16)
crcnum = crcnum ^ num
except Exception, e:
return 'error of change2int '
crcstr = format(crcnum,'x')
return crcstr
@staticmethod
def _crc2(data = '0001020304050607080900102030405060708090'):
num = (len(data)+1)/2
crcnum = 0
for i in range(num):
try:
num = int(data[:2],16)
crcnum = crcnum ^ num
data = data[2:]
except Exception, e:
return 'error of change2int '
crcstr = format(crcnum,'x')
return crcstr
@staticmethod
def _crc3(data = '0001020304050607080900102030405060708090'):
num = (len(data)+1)/2
datalist = []
for i in range(num):
string = data[(0+i*2):(2+i*2)]
datalist.append(string)
crcnum = 0
for i in datalist:
try:
num = int(i,16)
crcnum = crcnum ^ num
except Exception, e:
return 'error of change2int '
crcstr = format(crcnum,'x')
return crcstr
if __name__ == '__main__':
if reader.com_detect():
a = 0
for i in range(1000):
start = time.clock()
for i in range(100):
reader._crc1()
end = time.clock()
a = a + end - start
print a
a = 0
for i in range(1000):
start = time.clock()
for i in range(100):
reader._crc2()
end = time.clock()
a = a + end - start
print a
a = 0
for i in range(1000):
start = time.clock()
for i in range(100):
reader._crc3()
end = time.clock()
a = a + end - start
print a
else:
import re
import time
'''
读卡器操作类:
'''
class reader():
baud_rate = 115200
device_name = ''
timeout = 1
ser = None
@staticmethod
def opencom(device_name):
try:
reader.ser = serial.Serial(reader.device_name,reader.baud_rate,timeout = reader.timeout)
return True
except Exception, e:
return False
@staticmethod
def com_detect():
reader.device_name = '/dev/ttyUSB0'
if reader.opencom(reader.device_name):
return True
else:
reader.device_name = '/dev/ttyUSB1'
if reader.opencom(reader.device_name):
return True
else:
reader.device_name = '/dev/ttyUSB2'
if reader.opencom(reader.device_name):
return True
else:
reader.device_name = 'COM2'
if reader.opencom(reader.device_name):
return True
else:
return False
@staticmethod
def _crc1(data = '0001020304050607080900102030405060708090'):
datalist = re.findall(r'(.{2})',data)
crcnum = 0
for i in datalist:
try:
num = int(i,16)
crcnum = crcnum ^ num
except Exception, e:
return 'error of change2int '
crcstr = format(crcnum,'x')
return crcstr
@staticmethod
def _crc2(data = '0001020304050607080900102030405060708090'):
num = (len(data)+1)/2
crcnum = 0
for i in range(num):
try:
num = int(data[:2],16)
crcnum = crcnum ^ num
data = data[2:]
except Exception, e:
return 'error of change2int '
crcstr = format(crcnum,'x')
return crcstr
@staticmethod
def _crc3(data = '0001020304050607080900102030405060708090'):
num = (len(data)+1)/2
datalist = []
for i in range(num):
string = data[(0+i*2):(2+i*2)]
datalist.append(string)
crcnum = 0
for i in datalist:
try:
num = int(i,16)
crcnum = crcnum ^ num
except Exception, e:
return 'error of change2int '
crcstr = format(crcnum,'x')
return crcstr
if __name__ == '__main__':
if reader.com_detect():
a = 0
for i in range(1000):
start = time.clock()
for i in range(100):
reader._crc1()
end = time.clock()
a = a + end - start
print a
a = 0
for i in range(1000):
start = time.clock()
for i in range(100):
reader._crc2()
end = time.clock()
a = a + end - start
print a
a = 0
for i in range(1000):
start = time.clock()
for i in range(100):
reader._crc3()
end = time.clock()
a = a + end - start
print a
else:
print 'no com found'
输出的时间:crc1:1.76726367849
crc2:1.41187894711
crc3:1.80825700785
第一种方法快于第三种慢于第二种
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341