本示例使用的发卡器: T5557 T5567 T5577低频RFID读写器 EM4100 HID卡复制器 酒店门卡-淘宝网 (taobao.com)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget,QMessageBox,QCheckBox,QLineEdit
from PyQt5.QtGui import QFont
from RWT5557Demo import Ui_MainWindow
import sys
import struct # struct的pack函数把任意数据类型变成字符串
import ctypes # 调用DLL动态库要有这个引用
# 控制字定义
NEEDSERIAL = eval('0x01') # 需要只对指定系列号的卡操作
NEEDKEY = eval('0x02') # 需要用密码认证
LOCKBIT = eval('0x04') # 锁定配置块或数据块,仅对 t5557_init,t5557_write ,t5557_changekey函数有效
KEYENABLE = eval('0x08') # 启用本卡的密码功能
RESETCARD = eval('0x10') # 操作成功后重启卡片
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.check_authkey.stateChanged.connect(self.check_authkey_onStateChanged)
self.check_serial.stateChanged.connect(self.check_serial_onStateChanged)
self.check_Enkey.stateChanged.connect(self.check_Enkey_onStateChanged)
self.radio_em4100.toggled.connect(self.radio_em4100_toggled)
self.comboBox2.activated.connect(self.comboBox2_activated)
self.push_beep.clicked.connect(self.push_beep_clicked)
self.push_getdevnum.clicked.connect(self.push_getdevnum_clicked)
self.push_readcard.clicked.connect(self.push_readcard_clicked)
self.push_writcard.clicked.connect(self.push_writcard_clicked)
self.push_changekey.clicked.connect(self.push_changekey_clicked)
self.push_editconfig.clicked.connect(self.push_editconfig_clicked)
self.push_readid.clicked.connect(self.push_readid_clicked)
self.push_toidhid.clicked.connect(self.push_toidhid_clicked)
self.push_changeid0.clicked.connect(self.push_changeid0_clicked)
self.push_changeid1.clicked.connect(self.push_changeid1_clicked)
def DispErrInf(self,Errcode):
if (Errcode == 8):
QMessageBox.critical(self,'提示','错误代码:8,未寻到卡,请重新拿开卡后再放到感应区!', QMessageBox.Yes)
elif (Errcode == 1):
QMessageBox.critical(self,'提示','错误代码:1,写入配置的值不正确,请重新写入!', QMessageBox.Yes)
elif (Errcode == 2):
QMessageBox.critical(self,'提示','错误代码:2,本卡尚未开启密码功能,函数myctrlword中无需加入 NEEDKEY ', QMessageBox.Yes)
elif (Errcode == 3):
QMessageBox.critical(self,'提示','错误代码:3,需要密码才能读卡,函数myctrlword要加入NEEDKEY ', QMessageBox.Yes)
elif (Errcode == 5):
QMessageBox.critical(self,'提示','错误代码:5,密码错误!', QMessageBox.Yes)
elif (Errcode == 21):
QMessageBox.critical(self,'提示','错误代码:21,没有动态库!', QMessageBox.Yes)
elif (Errcode == 22):
QMessageBox.critical(self,'提示','错误代码:22,动态库或驱动程序异常!', QMessageBox.Yes)
elif (Errcode == 23):
QMessageBox.critical(self,'提示','错误代码:23,驱动程序错误或尚未安装!', QMessageBox.Yes)
elif (Errcode == 24):
QMessageBox.critical(self,'提示','错误代码:24,操作超时,一般是动态库没有反映!', QMessageBox.Yes)
elif (Errcode == 25):
QMessageBox.critical(self,'提示','错误代码:25,发送字数不够!', QMessageBox.Yes)
elif (Errcode == 26):
QMessageBox.critical(self,'提示','错误代码:26,发送的CRC错!', QMessageBox.Yes)
elif (Errcode == 27):
QMessageBox.critical(self,'提示','错误代码:27,接收的字数不够!', QMessageBox.Yes)
elif (Errcode == 28):
QMessageBox.critical(self,'提示','错误代码:28,接收的CRC错!', QMessageBox.Yes)
else:
QMessageBox.critical(self,'提示','未知错误,错误代码:' + str(Errcode), QMessageBox.Yes)
def comboBox2_activated(self,index):
if self.comboBox2.currentIndex() ==0:
self.text_newid3.setText('24')
elif self.comboBox2.currentIndex() ==1:
self.text_newid3.setText('35')
elif self.comboBox2.currentIndex() ==2:
self.text_newid3.setText('35')
elif self.comboBox2.currentIndex() ==3:
self.text_newid3.setText('33')
else:
self.text_newid3.setText('%d' %(self.comboBox2.currentIndex()+4))
def check_authkey_onStateChanged(self):
if self.check_authkey.isChecked():
self.text_authkey.setVisible(True)
else:
self.text_authkey.setVisible(False)
def check_serial_onStateChanged(self):
if self.check_serial.isChecked():
self.text_piccserial.setVisible(True)
else:
self.text_piccserial.setVisible(False)
def check_Enkey_onStateChanged(self):
if self.check_Enkey.isChecked():
self.label_20.setVisible(True)
self.text_Enauthkey.setVisible(True)
self.text_config.setText('00088038')
else:
self.label_20.setVisible(False)
self.text_Enauthkey.setVisible(False)
self.text_config.setText('00088028')
def radio_em4100_toggled(self):
self.text_idserial.setText('')
self.text_newid0.setText('')
self.text_newid1.setText('')
self.text_newid2.setText('')
self.text_newid3.setText('')
if self.radio_em4100.isChecked():
self.comboBox.clear()
self.comboBox.addItem('10位16进制码')
self.comboBox.addItem('8H10D 码')
self.comboBox.addItem('韦根34码')
self.comboBox.addItem('韦根26码')
self.label_28.setVisible(False)
self.text_newid3.setVisible(False)
self.comboBox2.setVisible(False)
self.comboBox.setCurrentIndex(1)
else:
self.comboBox.clear()
self.comboBox.addItem('12位16进制码')
self.comboBox.addItem('8H10D 码')
self.comboBox.addItem('韦根34码')
self.comboBox.addItem('韦根26码')
self.comboBox.addItem('4.3H6D码')
self.comboBox.addItem('4H5D码')
self.label_28.setVisible(True)
self.text_newid3.setVisible(True)
self.comboBox2.setVisible(True)
self.comboBox.setCurrentIndex(1)
def push_beep_clicked(self):
status = Objdll.idr_beep(50) % 256
if (status != 0):
self.DispErrInf(status)
def push_getdevnum_clicked(self):
devno = bytes(4) # 声明4个字节缓冲
status = Objdll.pcdgetdevicenumber(devno) % 256
if (status == 0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, len(devno)):
SerialNum = SerialNum + '%d' % (devno[num])
if (num < len(devno) - 1):
SerialNum = SerialNum + '-'
QMessageBox.information(self,'提示','设备出厂编号:' + SerialNum,QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_readcard_clicked(self):
myctrlword = 0 # 控制位
oldpicckey = bytes(4) # 4字节卡认证密码
mypiccserial = bytes(6) # 6字节卡序列号
mypiccdata = bytes(50) # 读卡缓冲
if (self.check_authkey.isChecked()): #选择需要先认证卡片密码
PasswStr=self.text_authkey.text().strip()
if(is_valid_hex(PasswStr)==False or len(bytes.fromhex(PasswStr))!=4):
QMessageBox.critical(self,'提示','卡片认证密钥错误,请输入8位16进制卡片认证密钥!',QMessageBox.Yes)
self.text_authkey.setFocus()
return
myctrlword = myctrlword + NEEDKEY
oldpicckey = bytes.fromhex(PasswStr)
if (self.check_serial.isChecked()): #选择只操作指定卡号的卡
serialstr=self.text_piccserial.text().strip()
if (is_valid_hex(serialstr) == False or len(bytes.fromhex(serialstr))!=6):
QMessageBox.critical(self, '提示', '卡号错误,请输入12位16进制卡号!', QMessageBox.Yes)
self.text_piccserial.setFocus()
return
myctrlword = myctrlword + NEEDSERIAL
mypiccserial = bytes.fromhex(serialstr)
seleblock0= '' #第0页块选标志位
for i in range(8):
self.chckbox = self.findChild(QCheckBox, "check_block%d" % (i))
if self.chckbox.isChecked():
seleblock0 = '1' + seleblock0
else:
seleblock0 = '0' + seleblock0
mypiccblockflag=bytes([int(seleblock0, 2)])
seleblock1 = '0' #第1页块选标志位,注意首位先取0
for i in range(8,12):
self.chckbox = self.findChild(QCheckBox, "check_block%d" % (i))
if self.chckbox.isChecked():
seleblock1 = '1' + seleblock1
else:
seleblock1 = '0' + seleblock1
mypiccblockflag = mypiccblockflag + bytes([int(seleblock1, 2)])
status = Objdll.t5557_read(myctrlword, mypiccserial, oldpicckey, mypiccblockflag, mypiccdata) % 256
if (status == 0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, len(mypiccserial)):
SerialNum = SerialNum + '%02X' % (mypiccserial[num])
DataHexStr = ''
for num in range(0, mypiccdata[1]):
DataHexStr = DataHexStr + '%02X' % (mypiccdata[num + 2])
seleblock=seleblock0[::-1]+seleblock1[::-1][1:5] #根据块选标志位解析每块数据
j=0
for i in range(12):
if(seleblock[i:i+1]=='1'):
self.textedit=self.findChild(QLineEdit,'text_block%d' % (i) )
datastr=DataHexStr[j:j+8]
j=j+8
self.textedit.setText(datastr)
QMessageBox.information(self,'提示','读卡成功,16进制卡号:' + SerialNum + ',卡无线转输分频比:' + str(mypiccdata[0]),QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_writcard_clicked(self):
myctrlword = 0 # 控制位
oldpicckey = bytes(4) # 4字节卡认证密码
mypiccserial = bytes(6) # 6字节卡序列号
if (self.check_authkey.isChecked()): # 选择需要先认证卡片密码
PasswStr = self.text_authkey.text().strip()
if (is_valid_hex(PasswStr) == False or len(bytes.fromhex(PasswStr)) != 4):
QMessageBox.critical(self, '提示', '卡片认证密钥错误,请输入8位16进制卡片认证密钥!', QMessageBox.Yes)
self.text_authkey.setFocus()
return
myctrlword = myctrlword + NEEDKEY
oldpicckey = bytes.fromhex(PasswStr)
if (self.check_serial.isChecked()): # 选择只操作指定卡号的卡
serialstr = self.text_piccserial.text().strip()
if (is_valid_hex(serialstr) == False or len(bytes.fromhex(serialstr)) != 6):
QMessageBox.critical(self, '提示', '卡号错误,请输入12位16进制卡号!', QMessageBox.Yes)
self.text_piccserial.setFocus()
return
myctrlword = myctrlword + NEEDSERIAL
mypiccserial = bytes.fromhex(serialstr)
seleblock0 = '' # 第0页块选标志位
writedata=''
for i in range(8):
self.chckbox = self.findChild(QCheckBox, "check_block%d" % (i))
self.textedit = self.findChild(QLineEdit, 'text_block%d' % (i))
if self.chckbox.isChecked():
seleblock0 = '1' + seleblock0
blockdata=self.textedit.text().strip()
if (is_valid_hex(blockdata) == False or len(bytes.fromhex(blockdata)) != 4):
QMessageBox.critical(self, '提示', '第%d' %(i)+"块写卡数据输入错误!", QMessageBox.Yes)
self.textedit.setFocus()
return
writedata=writedata+blockdata
else:
seleblock0 = '0' + seleblock0
mypiccblockflag = bytes([int(seleblock0, 2)])
seleblock1 = '0' # 第1页块选标志位,注意首位先取0
for i in range(8, 12):
self.chckbox = self.findChild(QCheckBox, "check_block%d" % (i))
self.textedit = self.findChild(QLineEdit, 'text_block%d' % (i))
if self.chckbox.isChecked():
seleblock1 = '1' + seleblock1
blockdata = self.textedit.text().strip()
if (is_valid_hex(blockdata) == False or len(bytes.fromhex(blockdata)) != 4):
QMessageBox.critical(self, '提示', '第%d' % (i) + "块写卡数据输入错误!", QMessageBox.Yes)
self.textedit.setFocus()
return
writedata = writedata + blockdata
else:
seleblock1 = '0' + seleblock1
mypiccblockflag = mypiccblockflag + bytes([int(seleblock1, 2)])
writebuf=bytes.fromhex(writedata)
if (len(writebuf)<1):
QMessageBox.critical(self, '提示', '请选择要写入数据的块号!', QMessageBox.Yes)
return
status = Objdll.t5557_write(myctrlword,mypiccserial,oldpicckey,mypiccblockflag,writebuf) % 256
if (status == 0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, len(mypiccserial)):
SerialNum = SerialNum + '%02X' % (mypiccserial[num])
QMessageBox.information(self,'提示','16进制卡号:' + SerialNum+ ',写卡成功!',QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_changekey_clicked(self):
myctrlword = 0 # 控制位
oldpicckey = bytes(4) # 4字节卡认证密码
mypiccserial = bytes(6) # 6字节卡序列号
if (self.check_authkey.isChecked()): # 选择需要先认证卡片密码
PasswStr = self.text_authkey.text().strip()
if (is_valid_hex(PasswStr) == False or len(bytes.fromhex(PasswStr)) != 4):
QMessageBox.critical(self, '提示', '卡片认证密钥错误,请输入8位16进制卡片认证密钥!', QMessageBox.Yes)
self.text_authkey.setFocus()
return
myctrlword = myctrlword + NEEDKEY
oldpicckey = bytes.fromhex(PasswStr)
if (self.check_serial.isChecked()): # 选择只操作指定卡号的卡
serialstr = self.text_piccserial.text().strip()
if (is_valid_hex(serialstr) == False or len(bytes.fromhex(serialstr)) != 6):
QMessageBox.critical(self, '提示', '卡号错误,请输入12位16进制卡号!', QMessageBox.Yes)
self.text_piccserial.setFocus()
return
myctrlword = myctrlword + NEEDSERIAL
mypiccserial = bytes.fromhex(serialstr)
newkeystr=self.text_newkey.text().strip()
if (is_valid_hex(newkeystr) == False or len(bytes.fromhex(newkeystr)) != 4):
QMessageBox.critical(self, '提示', '卡片新密钥错误,请输入8位16进制卡片新密钥!', QMessageBox.Yes)
self.text_newkey.setFocus()
return
newkeybuf=bytes.fromhex(newkeystr)
status = Objdll.t5557_changekey(myctrlword, mypiccserial, oldpicckey, newkeybuf) % 256
if (status == 0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, len(mypiccserial)):
SerialNum = SerialNum + '%02X' % (mypiccserial[num])
QMessageBox.information(self, '提示', '16进制卡号:' + SerialNum + ',修改卡密钥成功!', QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_editconfig_clicked(self):
myctrlword = 0 # 控制位
oldpicckey = bytes(4) # 4字节卡认证密码
newpicckey=bytes(4) # 4字节新密码
mypiccserial = bytes(6) # 6字节卡序列号
if (self.check_authkey.isChecked()): # 选择需要先认证卡片密码
PasswStr = self.text_authkey.text().strip()
if (is_valid_hex(PasswStr) == False or len(bytes.fromhex(PasswStr)) != 4):
QMessageBox.critical(self, '提示', '卡片认证密钥错误,请输入8位16进制卡片认证密钥!', QMessageBox.Yes)
self.text_authkey.setFocus()
return
myctrlword = myctrlword + NEEDKEY
oldpicckey = bytes.fromhex(PasswStr)
if (self.check_Enkey.isChecked()):
newkeystr=self.text_Enauthkey.text().strip()
if (is_valid_hex(newkeystr) == False or len(bytes.fromhex(newkeystr)) != 4):
QMessageBox.critical(self, '提示', '卡片密钥错误,请输入8位16进制卡片密钥!', QMessageBox.Yes)
self.text_Enauthkey.setFocus()
return
newpicckey=bytes.fromhex(newkeystr)
myctrlword = myctrlword + KEYENABLE
configstr=self.text_config.text().strip()
if (is_valid_hex(configstr) == False or len(bytes.fromhex(configstr)) != 4):
QMessageBox.critical(self, '提示', '卡片配置值错误,请输入8位16进制卡片配置值!', QMessageBox.Yes)
self.text_config.setFocus()
return
configbuf=bytes.fromhex(configstr)
status = Objdll.t5557_init(myctrlword, mypiccserial, oldpicckey, configbuf, newpicckey) % 256
if (status == 0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, len(mypiccserial)):
SerialNum = SerialNum + '%02X' % (mypiccserial[num])
QMessageBox.information(self, '提示', '16进制卡号:' + SerialNum + ',修改卡配置值成功!', QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_readid_clicked(self):
mypiccserial = bytes(7) # 卡序列号缓冲
if (self.radio_em4100.isChecked()):
status = Objdll.idr_read(mypiccserial) % 256
if (status==0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, 5):
SerialNum = SerialNum + '%02X' % (mypiccserial[num])
self.text_idserial.setText(SerialNum)
self.push_changeid0_clicked()
QMessageBox.information(self, '提示', '读ID卡成功,16进制卡号:' + SerialNum , QMessageBox.Yes)
else:
self.DispErrInf(status)
else:
status = Objdll.hid_read(mypiccserial) % 256
if (status == 0):
Objdll.idr_beep(38)
SerialNum = ''
for num in range(0, 7):
SerialNum = SerialNum + '%02X' % (mypiccserial[num])
self.text_idserial.setText(SerialNum)
self.push_changeid0_clicked()
QMessageBox.information(self, '提示', '读HID卡成功,16进制卡号:' + SerialNum, QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_toidhid_clicked(self):
myctrlword = 0 # 控制位
oldpicckey = bytes(4) # 4字节卡认证密码
mypiccserial = bytes(6) # 6字节卡序列号
newpicckey = bytes(4) # 4字节卡新密码
if (self.check_authkey.isChecked()): # 选择需要先认证卡片密码
PasswStr = self.text_authkey.text().strip()
if (is_valid_hex(PasswStr) == False or len(bytes.fromhex(PasswStr)) != 4):
QMessageBox.critical(self, '提示', '卡片认证密钥错误,请输入8位16进制卡片认证密钥!', QMessageBox.Yes)
self.text_authkey.setFocus()
return
myctrlword = myctrlword + NEEDKEY
oldpicckey = bytes.fromhex(PasswStr)
if (self.check_serial.isChecked()): # 选择只操作指定卡号的卡
serialstr = self.text_piccserial.text().strip()
if (is_valid_hex(serialstr) == False or len(bytes.fromhex(serialstr)) != 6):
QMessageBox.critical(self, '提示', '卡号错误,请输入12位16进制卡号!', QMessageBox.Yes)
self.text_piccserial.setFocus()
return
myctrlword = myctrlword + NEEDSERIAL
mypiccserial = bytes.fromhex(serialstr)
if (self.check_authkey_2.isChecked()):
newkeystr=self.text_authkey_2.text().strip()
if (is_valid_hex(newkeystr) == False or len(bytes.fromhex(newkeystr)) != 4):
QMessageBox.critical(self, '提示', '卡密钥错误,请输入8位16进制卡密钥!', QMessageBox.Yes)
self.text_authkey_2.setFocus()
return
myctrlword = myctrlword + KEYENABLE
newpicckey = bytes.fromhex(newkeystr)
myctrlword = myctrlword + RESETCARD # 操作后重启卡片,否则在制卡后,需要拿开卡片重放才能成功读卡
SerialNum = self.text_idserial.text().strip()
if (self.radio_em4100.isChecked()):
if (is_valid_hex(SerialNum) == False or len(bytes.fromhex(SerialNum)) != 5):
QMessageBox.critical(self, '提示', '原始16进制卡信息错误,请输入10位原始16进制ID卡信息!', QMessageBox.Yes)
self.text_idserial.setFocus()
return
mynewuid=bytes.fromhex(SerialNum)
status = Objdll.t5557_to4100(myctrlword, mypiccserial, oldpicckey, newpicckey, mynewuid)
if status==0:
Objdll.idr_beep(38)
QMessageBox.information(self, '提示', 'ID卡号写入成功,卡片变成ID卡!不能再用t5557的指令读写此卡,可重新设置配置块恢复t5557卡功能!', QMessageBox.Yes)
else:
self.DispErrInf(status)
else:
if (is_valid_hex(SerialNum) == False or len(bytes.fromhex(SerialNum)) != 7):
QMessageBox.critical(self, '提示', '原始16进制卡信息错误,请输入14位原始16进制HID卡信息!', QMessageBox.Yes)
self.text_idserial.setFocus()
return
mynewuid = bytes.fromhex(SerialNum)
status = Objdll.t5557_tohid(myctrlword, mypiccserial, oldpicckey, newpicckey, mynewuid)
if status == 0:
Objdll.idr_beep(38)
QMessageBox.information(self, '提示', 'HID卡号写入成功,卡片变成HID卡!不能再用t5557的指令读写此卡,可重新设置配置块恢复t5557卡功能!',QMessageBox.Yes)
else:
self.DispErrInf(status)
def push_changeid0_clicked(self):
SerialNum = self.text_idserial.text().strip()
if (self.radio_em4100.isChecked()):
if (is_valid_hex(SerialNum) == False or len(bytes.fromhex(SerialNum)) != 5):
QMessageBox.critical(self, '提示', '原始16进制卡信息错误,请输入10位原始16进制ID卡信息!', QMessageBox.Yes)
self.text_idserial.setFocus()
return
if self.comboBox.currentIndex()==0:
self.text_newid0.setText(SerialNum[0:2])
self.text_newid1.setText(SerialNum[2:4])
self.text_newid2.setText(SerialNum[4:10])
elif self.comboBox.currentIndex()==1:
self.text_newid0.setText(SerialNum[0:2])
self.text_newid1.setText('')
self.text_newid2.setText('%010d' %(int(SerialNum[2:10],16)))
elif self.comboBox.currentIndex()==2:
self.text_newid0.setText(SerialNum[0:2])
self.text_newid1.setText('')
self.text_newid2.setText('%05d' % (int(SerialNum[2:6], 16))+'%05d' % (int(SerialNum[6:10], 16)))
elif self.comboBox.currentIndex()==3:
self.text_newid0.setText(SerialNum[0:2])
self.text_newid1.setText(SerialNum[2:4])
self.text_newid2.setText('%03d' % (int(SerialNum[4:6], 16))+'%05d' % (int(SerialNum[6:10], 16)))
else:
if (is_valid_hex(SerialNum) == False or len(bytes.fromhex(SerialNum)) != 7):
QMessageBox.critical(self, '提示', '原始16进制卡信息错误,请输入14位原始16进制HID卡信息!', QMessageBox.Yes)
self.text_idserial.setFocus()
return
if self.comboBox.currentIndex()==0:
self.text_newid3.setText('%d' %(int(SerialNum[0:2],16)))
self.text_newid0.setText(SerialNum[2:6])
self.text_newid1.setText(SerialNum[6:8])
self.text_newid2.setText(SerialNum[8:14])
elif self.comboBox.currentIndex()==1:
self.text_newid3.setText('%d' %(int(SerialNum[0:2],16)))
self.text_newid0.setText(SerialNum[2:6])
self.text_newid1.setText('')
self.text_newid2.setText('%010d' %(int(SerialNum[6:14],16)))
elif self.comboBox.currentIndex()==2:
self.text_newid3.setText('%d' %(int(SerialNum[0:2],16)))
self.text_newid0.setText(SerialNum[2:6])
self.text_newid1.setText('')
self.text_newid2.setText('%05d' % (int(SerialNum[6:10], 16))+'%05d' % (int(SerialNum[10:14], 16)))
elif self.comboBox.currentIndex()==3:
self.text_newid3.setText('%d' %(int(SerialNum[0:2],16)))
self.text_newid0.setText(SerialNum[2:6])
self.text_newid1.setText(SerialNum[6:8])
self.text_newid2.setText('%03d' % (int(SerialNum[8:10], 16))+'%05d' % (int(SerialNum[10:14], 16)))
elif self.comboBox.currentIndex()==4:
self.text_newid3.setText('%d' %(int(SerialNum[0:2],16)))
self.text_newid0.setText(SerialNum[2:6])
bitstr=('00000000000000000000000000000000'+bin(int(SerialNum[6:14],16))[2:])[-32:]
self.text_newid1.setText('%d' % (int(bitstr[:13], 2)))
self.text_newid2.setText('%d' %(int(bitstr[-19:],2)))
elif self.comboBox.currentIndex()==5:
self.text_newid3.setText('%d' %(int(SerialNum[0:2],16)))
self.text_newid0.setText(SerialNum[2:6])
self.text_newid1.setText(SerialNum[6:10])
self.text_newid2.setText('%05d' % (int(SerialNum[10:14], 16)))
def push_changeid1_clicked(self):
str0 = self.text_newid0.text().strip()
str1 = self.text_newid1.text().strip()
str2 = self.text_newid2.text().strip()
str3 = self.text_newid3.text().strip()
if (self.radio_em4100.isChecked()):
if self.comboBox.currentIndex() == 0:
newidstr=str0+str1+str2
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 5):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 1:
newidstr = str0 + '%08X' %(int(str2))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 5):
QMessageBox.critical(self, '提示', '厂商代码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 2:
str2=('0000000000'+str2)[-10:]
if int(str2[:5])>65535 or int(str2[-5:])>65535:
QMessageBox.critical(self, '提示', '卡号输入错误!', QMessageBox.Yes)
return
newidstr = str0 + '%04X' %(int(str2[:5]))+ '%04X' %(int(str2[-5:]))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 5):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 3:
str2 = ('00000000' + str2)[-8:]
if int(str2[:3]) > 255 or int(str2[-5:]) > 65535:
QMessageBox.critical(self, '提示', '卡号输入错误!', QMessageBox.Yes)
return
newidstr = str0 + str1 + '%02X' %(int(str2[:3])) + '%04X' %(int(str2[-5:]))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 5):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
else:
str3='%02X'%(int(str3))
if self.comboBox.currentIndex() == 0:
newidstr = str3 + str0 + str1 + str2
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 7):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 1:
newidstr = str3 + str0 + str1 + '%08X' %(int(str2))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 7):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 2:
str2 = ('0000000000' + str2)[-10:]
if int(str2[:5]) > 65535 or int(str2[-5:]) > 65535:
QMessageBox.critical(self, '提示', '卡号输入错误!', QMessageBox.Yes)
return
newidstr = str3 + str0 + '%04X' %(int(str2[:5]))+ '%04X' %(int(str2[-5:]))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 7):
QMessageBox.critical(self, '提示', '厂商代码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 3:
str2 = ('00000000' + str2)[-8:]
if int(str2[:3]) > 255 or int(str2[-5:]) > 65535:
QMessageBox.critical(self, '提示', '卡号输入错误!', QMessageBox.Yes)
return
newidstr = str3 + str0 + str1 + '%02X' %(int(str2[:3])) + '%04X' %(int(str2[-5:]))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 7):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 4:
if int(str1)>8191 or int(str2)>524287:
QMessageBox.critical(self, '提示', '前缀码、卡号输入错误!', QMessageBox.Yes)
return
str12=('0000000000000'+bin(int(str1))[2:])[-13:]+ ('0000000000000000000'+bin(int(str2))[2:])[-19:]
str12='%08X' %(int(str12, 2))
newidstr = str3 + str0 + str12
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 7):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
elif self.comboBox.currentIndex() == 5:
newidstr = str3 + str0 + str1 + '%04X' %(int(str2))
if (is_valid_hex(newidstr) == False or len(bytes.fromhex(newidstr)) != 7):
QMessageBox.critical(self, '提示', '厂商代码、前缀码、卡号输入错误!', QMessageBox.Yes)
return
self.text_idserial.setText(newidstr)
def is_valid_hex(input_str):
input_str = input_str.replace(" ", "")
try:
if (len(bytes.fromhex(input_str)) == len(input_str) / 2):
return True
else:
return False
except:
return False
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
screen = QDesktopWidget().screenGeometry()
size = mainWindow.geometry()
# 获得窗口相关坐标
newLeft = (screen.width() - size.width()) // 2
newTop = (screen.height() - size.height()) // 2
# 移动窗口使其居中
mainWindow.move(newLeft, newTop)
mainWindow.setWindowTitle("T5557 Python Demo")
mainWindow.text_authkey.setVisible(False)
mainWindow.text_piccserial.setVisible(False)
mainWindow.label_20.setVisible(False)
mainWindow.text_Enauthkey.setVisible(False)
mainWindow.label_28.setVisible(False)
mainWindow.text_newid3.setVisible(False)
mainWindow.comboBox2.setVisible(False)
mainWindow.comboBox.setCurrentIndex(1)
mainWindow.show()
font = QFont()
font.setFamily('Arial') # 设置字体名称
font.setPointSize(8)
if sys.platform == 'linux' or sys.platform == 'mac':
# Linux加载当前目录下的so库
dllfile = sys.path[0] + '/libOURIDR.so'
Objdll = ctypes.cdll.LoadLibrary(dllfile)
else:
# windows系统加载当前目录下的DLL库
dllfile = sys.path[0] + '\OUR_IDR.dll'
Objdll = ctypes.windll.LoadLibrary(dllfile)
sys.exit(app.exec_())
源码下载:T5557低频标签Python读写源码资源-CSDN文库