Skip to content

Instantly share code, notes, and snippets.

@Sucareto
Created January 29, 2022 08:09
Show Gist options
  • Save Sucareto/13d6b9f4d043d2145beb7c175d4103d6 to your computer and use it in GitHub Desktop.
Save Sucareto/13d6b9f4d043d2145beb7c175d4103d6 to your computer and use it in GitHub Desktop.
某串口数据校验工具
str1 = "E0 06 00 0F 40 01 03 59"
REQ = str1.split(" ")
print(REQ)
rSYN, rESC, rLEN, rSUM, rESCsw = 0, 0, 0, 0, False
if REQ[0] == "E0":
rSYN = "E0"
rESC = "D0"
rLEN = int(REQ[1], 16)
print("读卡器数据,SYN是{},ESC是{},LEN是{}".format(rSYN, rESC, rLEN))
elif REQ[0] == "FF":
rSYN = "FF"
rESC = "FD"
rLEN = int(REQ[2], 16)
print("触摸数据,SYN是{},ESC是{},LEN是{}".format(rSYN, rESC, rLEN))
else:
print("未知数据")
exit(1)
for i in range(1, rLEN+1):
if REQ[i] == rESC:
rESCsw = True
continue
elif rESCsw:
rSUM += int(REQ[i], 16)+1
rESCsw = False
continue
rSUM += int(REQ[i], 16)
if REQ[0] == "E0":
if (rSUM & 0xFF) == int(REQ[-1], 16):
print("校验通过")
else:
print(hex(rSUM & 0xFF))
elif REQ[0] == "FF":
if not (rSUM & 0xFF):
print("校验通过")
else:
print(hex(rSUM & 0xFF))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment