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
第一种方法快于第三种慢于第二种