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

@@ -27,7 +27,7 @@ internal sealed class SerialFrameTransport : IPn532FrameTransport
Parity = Parity.None,
DataBits = 8,
StopBits = StopBits.One,
DtrEnable = false,
DtrEnable = true,
RtsEnable = false
};
_readChunkTimeout = chunkTimeout;
@@ -39,6 +39,9 @@ internal sealed class SerialFrameTransport : IPn532FrameTransport
if (!_port.IsOpen)
{
_port.Open();
_port.DtrEnable = true;
_port.RtsEnable = false;
Thread.Sleep(250);
_port.DiscardInBuffer();
_port.DiscardOutBuffer();
SendWakeupPattern();
@@ -72,11 +75,11 @@ internal sealed class SerialFrameTransport : IPn532FrameTransport
private void SendWakeupPattern()
{
// Simple HSU wakeup: Just long preamble.
// 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);
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)
{

View File

@@ -94,9 +94,19 @@ internal static class Program
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.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);
var fw = Pn532HsuFrame.BuildDataFrame(Pn532HsuFrame.HostToPn532Tfi, new byte[] { 0x02 });
serial.Write(fw, 0, fw.Length);
@@ -113,7 +123,7 @@ internal static class Program
private static FlowResult RunPn532FelicaFlow(Pn532Session session)
{
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);
var target = WaitForCard(session);

View File

@@ -62,7 +62,7 @@ public class AimeReader
private FlowResult RunPn532Flow(Pn532Session session)
{
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);
var target = WaitForCard(session);

View File

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

View File

@@ -29,7 +29,7 @@ namespace NfcAime.Dll.PN532 {
Parity = Parity.None,
DataBits = 8,
StopBits = StopBits.One,
DtrEnable = false,
DtrEnable = true,
RtsEnable = false
};
_readChunkTimeout = chunkTimeout;
@@ -41,6 +41,9 @@ namespace NfcAime.Dll.PN532 {
if (!_port.IsOpen)
{
_port.Open();
_port.DtrEnable = true;
_port.RtsEnable = false;
Thread.Sleep(250);
_port.DiscardInBuffer();
_port.DiscardOutBuffer();
SendWakeupPattern();
@@ -76,9 +79,11 @@ namespace NfcAime.Dll.PN532 {
{
// Simple HSU wakeup: Just long preamble.
// 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);
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)
{