pn532修改水卡脚本
import serial
import binascii
#pip install serial
#pip install pyserial
com="3" #端口号
pw="115200" #波特率
ser = serial.Serial("com"+com, int(pw), timeout=0.5)
ser.bytesize = 8
ser.parity = serial.PARITY_NONE
ser.stopbits = 1
print("已连接端口:" + str(ser.name)+"\n")
def sst(data, start, end):
data = data.replace(" ", "")
list = []
i = 0
b = -2
c = 0
while len(data) / 2 > i:
b = b + 2
c = c + 2
li = data[b:c]
list.append(li)
i = i + 1
stt = list
shuju = bytes.fromhex(stt[start])[0]
i = 6
while i <= end:
shuju = shuju + bytes.fromhex(stt[i])[0]
i = i + 1
hex_shuju = hex(int(shuju))
shuju = ("00" + str(hex_shuju))[-2:]
bin_shuju = bin(int(shuju, 16)).lstrip("0b")
if len(bin_shuju) == 8:
bi = ""
for s in bin_shuju:
if s == "1":
bi = bi + str(0)
else:
bi = bi + str(1)
sr = "1"
bin_fan = bin(int(bi, 2) + int(sr, 2))[2:]
bin_hex = hex(int(bin_fan, 2)).lstrip("0x").upper()
return (bin_hex)
else:
bin_shuju = ("00000000" + bin_shuju)[-8:]
bi = ""
for s in bin_shuju:
if s == "1":
bi = bi + str(0)
else:
bi = bi + str(1)
sr = "1"
bin_fan = bin(int(bi, 2) + int(sr, 2))[2:]
bin_hex = hex(int(bin_fan, 2)).lstrip("0x").upper()
return (bin_hex)
def huan():
da1 = bytes.fromhex("55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff 03 fd d4 14 01 17 00")
ser.write(da1)
ss = str(binascii.b2a_hex(ser.read(25)))[2:-1]
return ser
def getid():
ser = huan()
data = bytes.fromhex("00 00 FF 04 FC D4 4A 02 00 E0 00")
print("获取卡片信息")
ser.write(data)
redata_yid = str(binascii.b2a_hex(ser.read(50)))[2:-1]
if len(redata_yid) == 50:
id = redata_yid[-12:-4]
print("卡号:" + id)
return str(id), ser
else:
print("数据返回出错:" + redata_yid)
return "0", "0"
def yanz():
id = getid()[0]
if id[0] != "0":
global k
k="74"
password = "5F 10 DF 62 52 01"
password = password.replace(" ", "")
yss = "00 00 FF 0F F1 D4 40 01 60" + k + password + id + "b2 00"
dcs = "00 00 FF 0F F1 D4 40 01 60" + k + password + id + sst(yss, 5, 19) + " 00"
dcs = dcs.replace(" ", "")
yanze = bytes.fromhex(dcs)
ser.write(yanze)
erok = str(binascii.b2a_hex(ser.read(32)))[2:-1]
ma = erok[-8:-4]
if ma == "4100":
print("密码正确")
writedata(1)
writedata(2)
return ser
else:
print("密码错误\n")
else:
print("获取失败,请检查卡片\n")
def blokey(block_key):
blo_k = bin(int(k, 16))
k_hex = bin(int(block_key, 16))
sun_bl = bin(int(blo_k, 2) + int(k_hex, 2))
blo_hex = ("000" + hex(int(sun_bl, 2)).lstrip("0x"))[-2:]
return (blo_hex)
def writedata(block_key):
data="bf090000000015061215312b340000f8"#水卡数据
blo = blokey(str(block_key))
reco = "00 00 ff 15 EB D4 40 01 A0 "+blo+data+" D2 00"
writecode = bytes.fromhex("00 00 ff 15 EB D4 40 01 A0 "+blo+data+sst(reco,5,25)+" 00")
ser.write(writecode)
ss = str(binascii.b2a_hex(ser.read(64)))[2:-1]
# print(ss[-9:-4])
if ss[-9:-4] == "54100":
print("成功写入数据:" + data+"\n")
else:
print("写入失败")
while True:
yanz()
exit()
ser.close()