131 lines
3.9 KiB
Python
131 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
测试树莓派上的NFC功能
|
|
"""
|
|
|
|
import subprocess
|
|
import os
|
|
import re
|
|
import sys
|
|
import time
|
|
|
|
def test_nfc_connection():
|
|
"""测试NFC连接"""
|
|
print("🔍 测试NFC连接...")
|
|
|
|
# 设置环境变量
|
|
os.environ['LIBNFC_DRIVER'] = 'pn532_i2c'
|
|
os.environ['LIBNFC_DEVICE'] = 'pn532_i2c:/dev/i2c-1'
|
|
|
|
try:
|
|
result = subprocess.run(['nfc-list'], capture_output=True, text=True, timeout=10)
|
|
print(f"NFC输出: {result.stdout}")
|
|
print(f"返回码: {result.returncode}")
|
|
|
|
if result.returncode == 0:
|
|
if "NFC device: user defined device opened" in result.stdout:
|
|
print("✅ NFC设备连接成功")
|
|
return True
|
|
else:
|
|
print("⚠️ NFC设备未正确初始化")
|
|
return False
|
|
else:
|
|
print(f"❌ NFC设备连接失败: {result.stderr}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ NFC测试异常: {e}")
|
|
return False
|
|
|
|
def test_nfc_reading():
|
|
"""测试NFC读取"""
|
|
print("\n🔍 测试NFC读取...")
|
|
|
|
# 设置环境变量
|
|
os.environ['LIBNFC_DRIVER'] = 'pn532_i2c'
|
|
os.environ['LIBNFC_DEVICE'] = 'pn532_i2c:/dev/i2c-1'
|
|
|
|
try:
|
|
result = subprocess.run(['nfc-list'], capture_output=True, text=True, timeout=10)
|
|
if result.returncode == 0:
|
|
match = re.search(r'UID\s*\(NFCID1\):\s*([0-9A-Fa-f\s]+)', result.stdout)
|
|
if match:
|
|
uid = match.group(1).strip().replace(' ', '').upper()
|
|
print(f"✅ 读取到UID: {uid}")
|
|
return uid
|
|
else:
|
|
print("⚠️ 未检测到NFC卡片")
|
|
return None
|
|
else:
|
|
print(f"❌ NFC读取失败: {result.stderr}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"❌ NFC读取异常: {e}")
|
|
return None
|
|
|
|
def test_nfc_manager():
|
|
"""测试NFC管理器"""
|
|
print("\n🔍 测试NFC管理器...")
|
|
|
|
try:
|
|
# 导入NFC管理器
|
|
sys.path.append('/home/zhuchaowe/Local-Voice')
|
|
from nfc_manager import get_nfc_manager
|
|
|
|
# 创建NFC管理器
|
|
nfc_manager = get_nfc_manager()
|
|
|
|
# 检查映射
|
|
print(f"角色映射: {nfc_manager.uid_to_character}")
|
|
|
|
# 测试读取
|
|
uid = nfc_manager._read_uid()
|
|
if uid:
|
|
print(f"✅ NFC管理器读取到UID: {uid}")
|
|
|
|
# 检查映射
|
|
if uid in nfc_manager.uid_to_character:
|
|
character = nfc_manager.uid_to_character[uid]
|
|
print(f"🎭 对应角色: {character}")
|
|
else:
|
|
print("⚠️ UID未映射到角色")
|
|
else:
|
|
print("⚠️ 未读取到UID")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f"❌ NFC管理器测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def main():
|
|
"""主测试函数"""
|
|
print("🧪 树莓派NFC功能测试")
|
|
print("=" * 50)
|
|
|
|
# 测试NFC连接
|
|
connection_ok = test_nfc_connection()
|
|
|
|
# 测试NFC读取
|
|
uid = test_nfc_reading()
|
|
|
|
# 测试NFC管理器
|
|
manager_ok = test_nfc_manager()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("📊 测试结果:")
|
|
print(f" NFC连接: {'✅ 通过' if connection_ok else '❌ 失败'}")
|
|
print(f" UID读取: {'✅ 通过' if uid else '⚠️ 无卡片'}")
|
|
print(f" NFC管理器: {'✅ 通过' if manager_ok else '❌ 失败'}")
|
|
|
|
if uid:
|
|
print(f"\n🎯 当前检测到的UID: {uid}")
|
|
print("💡 提示: 您现在可以使用以下命令测试完整的NFC角色切换功能:")
|
|
print(" cd Local-Voice && python multiprocess_recorder.py --enable-nfc")
|
|
else:
|
|
print(f"\n💡 提示: 请确保NFC读取器已连接并放置了NFC卡片")
|
|
|
|
if __name__ == "__main__":
|
|
main() |