fix:修复PN532未能正确上电初始化

This commit is contained in:
2026-05-18 04:34:57 +08:00
parent 66ff0341ee
commit 4831ee6ecc
6 changed files with 32 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F84fcf9255ea045aab003a751cdb5a2f6573620_003F0d_003Fef77d1e2_003FArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F84fcf9255ea045aab003a751cdb5a2f6573620_003F0d_003Fef77d1e2_003FArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AReadOnlySpan_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FSourcesCache_003F47bfed48817fad7d8e1a89bf3530e4be7277b022a9c7477c5a243031605a5f_003FReadOnlySpan_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AReadOnlySpan_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FSourcesCache_003F47bfed48817fad7d8e1a89bf3530e4be7277b022a9c7477c5a243031605a5f_003FReadOnlySpan_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASerialPort_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Ffcd99788c52243bd9f38f3f526acbc38360fe8_003Fe8_003F70e5ff51_003FSerialPort_002Ecs_002Fz_003A2_002D1/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStackFrameIterator_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8b6d5d70605340b2a1b73ddc36254a2ae8e910_003Fbe_003F5760bbf2_003FStackFrameIterator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStackFrameIterator_002Ecs_002Fl_003AC_0021_003FUsers_003Fmczhi_003FAppData_003FRoaming_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8b6d5d70605340b2a1b73ddc36254a2ae8e910_003Fbe_003F5760bbf2_003FStackFrameIterator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Current\Bin\MSBuild.exe</s:String> <s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Current\Bin\MSBuild.exe</s:String>
<s:Int64 x:Key="/Default/Environment/Hierarchy/Build/BuildTool/MsbuildVersion/@EntryValue">1179648</s:Int64> <s:Int64 x:Key="/Default/Environment/Hierarchy/Build/BuildTool/MsbuildVersion/@EntryValue">1179648</s:Int64>

View File

@@ -27,7 +27,7 @@ internal sealed class SerialFrameTransport : IPn532FrameTransport
Parity = Parity.None, Parity = Parity.None,
DataBits = 8, DataBits = 8,
StopBits = StopBits.One, StopBits = StopBits.One,
DtrEnable = false, DtrEnable = true,
RtsEnable = false RtsEnable = false
}; };
_readChunkTimeout = chunkTimeout; _readChunkTimeout = chunkTimeout;
@@ -39,6 +39,9 @@ internal sealed class SerialFrameTransport : IPn532FrameTransport
if (!_port.IsOpen) if (!_port.IsOpen)
{ {
_port.Open(); _port.Open();
_port.DtrEnable = true;
_port.RtsEnable = false;
Thread.Sleep(250);
_port.DiscardInBuffer(); _port.DiscardInBuffer();
_port.DiscardOutBuffer(); _port.DiscardOutBuffer();
SendWakeupPattern(); SendWakeupPattern();
@@ -72,11 +75,11 @@ internal sealed class SerialFrameTransport : IPn532FrameTransport
private void SendWakeupPattern() private void SendWakeupPattern()
{ {
// Simple HSU wakeup: Just long preamble. var wakeup = new byte[] { 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// We avoid sending SAMConfig here to keep the buffer clean for the first real command.
var wakeup = new byte[] { 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
_port.Write(wakeup, 0, wakeup.Length); _port.Write(wakeup, 0, wakeup.Length);
Thread.Sleep(50); var sam = Pn532HsuFrame.BuildDataFrame(Pn532HsuFrame.HostToPn532Tfi, new byte[] { 0x14, 0x01 });
_port.Write(sam, 0, sam.Length);
Thread.Sleep(100);
if (_port.IsOpen) if (_port.IsOpen)
{ {

View File

@@ -94,9 +94,19 @@ internal static class Program
private static void RunDiag(CliOptions options) private static void RunDiag(CliOptions options)
{ {
using var serial = new SerialPort(options.Port, options.Baud) { ReadTimeout = 500, WriteTimeout = 500 }; using var serial = new SerialPort(options.Port, options.Baud)
{
ReadTimeout = 500,
WriteTimeout = 500,
DtrEnable = true,
RtsEnable = false
};
serial.Open(); serial.Open();
serial.Write(new byte[] { 0x55, 0x55, 0x00, 0x00, 0x00 }, 0, 5); serial.DtrEnable = true;
serial.RtsEnable = false;
Thread.Sleep(250);
var wakeup = new byte[] { 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
serial.Write(wakeup, 0, wakeup.Length);
Thread.Sleep(50); Thread.Sleep(50);
var fw = Pn532HsuFrame.BuildDataFrame(Pn532HsuFrame.HostToPn532Tfi, new byte[] { 0x02 }); var fw = Pn532HsuFrame.BuildDataFrame(Pn532HsuFrame.HostToPn532Tfi, new byte[] { 0x02 });
serial.Write(fw, 0, fw.Length); serial.Write(fw, 0, fw.Length);
@@ -113,7 +123,7 @@ internal static class Program
private static FlowResult RunPn532FelicaFlow(Pn532Session session) private static FlowResult RunPn532FelicaFlow(Pn532Session session)
{ {
ExpectPn532ResponseCode(session.SendCommand(new byte[] { 0x02 }), expectedResponseCode: 0x03); ExpectPn532ResponseCode(session.SendCommand(new byte[] { 0x02 }), expectedResponseCode: 0x03);
ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x14, 0x01, 0x14, 0x01 }), expectedResponseCode: 0x15); ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x14, 0x01 }), expectedResponseCode: 0x15);
ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x32, 0x01, 0x03 }), expectedResponseCode: 0x33); ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x32, 0x01, 0x03 }), expectedResponseCode: 0x33);
var target = WaitForCard(session); var target = WaitForCard(session);

View File

@@ -62,7 +62,7 @@ public class AimeReader
private FlowResult RunPn532Flow(Pn532Session session) private FlowResult RunPn532Flow(Pn532Session session)
{ {
ExpectPn532ResponseCode(session.SendCommand(new byte[] { 0x02 }), expectedResponseCode: 0x03); ExpectPn532ResponseCode(session.SendCommand(new byte[] { 0x02 }), expectedResponseCode: 0x03);
ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x14, 0x01, 0x14, 0x01 }), expectedResponseCode: 0x15); ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x14, 0x01 }), expectedResponseCode: 0x15);
ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x32, 0x01, 0x03 }), expectedResponseCode: 0x33); ExpectPn532StatusOk(session.SendCommand(new byte[] { 0x32, 0x01, 0x03 }), expectedResponseCode: 0x33);
var target = WaitForCard(session); var target = WaitForCard(session);

View File

@@ -18,7 +18,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>

View File

@@ -29,7 +29,7 @@ namespace NfcAime.Dll.PN532 {
Parity = Parity.None, Parity = Parity.None,
DataBits = 8, DataBits = 8,
StopBits = StopBits.One, StopBits = StopBits.One,
DtrEnable = false, DtrEnable = true,
RtsEnable = false RtsEnable = false
}; };
_readChunkTimeout = chunkTimeout; _readChunkTimeout = chunkTimeout;
@@ -41,6 +41,9 @@ namespace NfcAime.Dll.PN532 {
if (!_port.IsOpen) if (!_port.IsOpen)
{ {
_port.Open(); _port.Open();
_port.DtrEnable = true;
_port.RtsEnable = false;
Thread.Sleep(250);
_port.DiscardInBuffer(); _port.DiscardInBuffer();
_port.DiscardOutBuffer(); _port.DiscardOutBuffer();
SendWakeupPattern(); SendWakeupPattern();
@@ -76,9 +79,11 @@ namespace NfcAime.Dll.PN532 {
{ {
// Simple HSU wakeup: Just long preamble. // Simple HSU wakeup: Just long preamble.
// We avoid sending SAMConfig here to keep the buffer clean for the first real command. // We avoid sending SAMConfig here to keep the buffer clean for the first real command.
var wakeup = new byte[] { 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; var wakeup = new byte[] { 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
_port.Write(wakeup, 0, wakeup.Length); _port.Write(wakeup, 0, wakeup.Length);
Thread.Sleep(50); var sam = Pn532HsuFrame.BuildDataFrame(Pn532HsuFrame.HostToPn532Tfi, new byte[] { 0x14, 0x01 });
_port.Write(sam, 0, sam.Length);
Thread.Sleep(100);
if (_port.IsOpen) if (_port.IsOpen)
{ {