-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiag_reader.py
More file actions
99 lines (88 loc) · 4.12 KB
/
Copy pathdiag_reader.py
File metadata and controls
99 lines (88 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
r"""
读卡器占用诊断 —— 看清楚到底谁占着 PaSoRi、felica.dll 卡在哪一步。
跑法(在 main.py 同目录, 比如 E:\NekoCardReaderx):
1) 先关掉所有别的本程序窗口 / 旧打包版 exe(任务管理器确认无残留)
2) 先【不要贴卡】, 直接跑: python diag_reader.py
3) 把整段输出原样发回
"""
import ctypes
import subprocess
import sys
LINE = "=" * 64
def step1_processes():
print(LINE); print("STEP 1 正在运行、可能抢读卡器的进程"); print(LINE)
try:
out = subprocess.run(["tasklist"], capture_output=True, text=True, timeout=10).stdout
keys = ["python", "neko", "card", "sfcard", "felica", "pasori", "sony", "nfc", "pcscd", "scard"]
hits = [ln.rstrip() for ln in out.splitlines() if any(k in ln.lower() for k in keys)]
if hits:
for ln in hits:
print(" ", ln)
print(" ^ 如果这里除了本次 python 之外还有 NekoCardReader/旧 exe/SFCard 等, 它就是占用者")
else:
print(" (没发现明显的读卡相关进程)")
except Exception as e:
print(" tasklist 失败:", e)
def step2_pcsc():
print(); print(LINE); print("STEP 2 PC/SC 层(中国卡走这里): 读卡器是否被这套栈占着"); print(LINE)
try:
from smartcard.System import readers
rs = readers()
print(" PC/SC readers:", [str(r) for r in rs] or "(无)")
if not rs:
print(" 没有 PC/SC 读卡器 —— 说明设备没挂在 PC/SC 上"); return
tgt = next((r for r in rs if any(k in str(r) for k in ("FeliCa", "PaSoRi", "Sony", "RC-S", "NFC"))), rs[0])
conn = tgt.createConnection()
try:
conn.connect()
print(" PC/SC 连接:", tgt, "-> 成功")
print(" >>> 设备此刻挂在 PC/SC 栈上(智能卡服务在用)。felica.dll 要直连独占,")
print(" PC/SC 占着时它就打不开 = 你看到的『占用』。")
try: conn.disconnect()
except Exception: pass
except Exception as e:
print(" PC/SC 连接:", tgt, "-> 失败:", e)
print(" (没贴卡时连不上属正常; 若报 in use / sharing violation 则是别的进程占着)")
except Exception as e:
print(" PC/SC 不可用(没装 pyscard 或服务没起):", e)
def step3_felica():
print(); print(LINE); print("STEP 3 felica.dll: 加载 + 打开读卡器, 走到哪一步"); print(LINE)
try:
import felica64_reader as F
except Exception as e:
print(" import felica64_reader 失败:", e); return
try:
dll = F._dll()
print(" load felica.dll: OK")
except Exception as e:
print(" load felica.dll 失败:", e); return
U8 = ctypes.c_ubyte
def cf(name, at=()):
f = getattr(dll, name); f.restype = U8; f.argtypes = list(at); return f
try:
cf("initialize_library")()
cf("set_time_out", [ctypes.c_ulong])(1000)
print(" initialize_library: OK")
r = cf("open_reader_writer_auto")()
print(" open_reader_writer_auto 返回值 =", r, " (1=成功独占, 其它=打不开/被占)")
if r == 1:
print(" >>> felica 这次成功独占了读卡器!")
print(" => 读卡本身没问题, 之前的『占用』是别的进程没关干净(关掉它即可)。")
try: cf("close_reader_writer")()
except Exception: pass
else:
print(" >>> felica 打不开。结合 STEP 2: 若 STEP 2 能连上, 就是 PC/SC/智能卡服务占着;")
print(" 若 STEP 1 有别的本程序进程, 就是那个进程占着。")
try: cf("dispose_library")()
except Exception: pass
except Exception as e:
print(" felica 调用异常:", e)
if __name__ == "__main__":
print("读卡器占用诊断 (Python", sys.version.split()[0],
",", 8 * ctypes.sizeof(ctypes.c_void_p), "位)")
print()
step1_processes()
step2_pcsc()
step3_felica()
print(); print("DONE —— 把上面【全部】输出发回。")