Created
December 14, 2020 14:21
-
-
Save watagashi0619/639062958adbc60f44e6c3f8fdb79a74 to your computer and use it in GitHub Desktop.
nfcpyで学生証から情報を読む+おまけ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nfc | |
import struct | |
# 京都大学学生証 学籍番号と名前(半角カナ)を返す | |
def read_kucard(tag): | |
servc = 0x1A8B | |
service_code = [nfc.tag.tt3.ServiceCode(servc >> 6, servc & 0x3F)] | |
tag.dump() # これがないと何故かうまくいかなかった | |
bc_id = [nfc.tag.tt3.BlockCode(0)] | |
bd_id = tag.read_without_encryption(service_code, bc_id) | |
student_id = int(bd_id[2:-4].decode("utf-8")) | |
bc_name = [nfc.tag.tt3.BlockCode(1)] | |
student_name = ( | |
tag.read_without_encryption(service_code, bc_name) | |
.decode("shift-jis") | |
.rstrip("\x00") | |
) | |
return student_id, student_name | |
# 交通系ICカード(モバイルでない)から残高を返す | |
def read_suica(tag): | |
servc = 0x090F | |
service_code = [nfc.tag.tt3.ServiceCode(servc >> 6, servc & 0x3F)] | |
block_code = [nfc.tag.tt3.BlockCode(0, service=0)] | |
row_le = struct.unpack( | |
"<2B2H4BH4B", tag.read_without_encryption(service_code, block_code) | |
) | |
balance = row_le[8] | |
return balance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment