I have a C# program based on unfc.codeplex.com to configure a PN532 module (connected via USB To TTL CH340G Converter) in card emulation mode. Im using dotnet SerialPort class to communicate with the module. When I hover my phone over the module, phone starts to vibrate and tries to detect the card/module but it doesn't detect consistently. Occasionally it does get detected, but this doesn't happen consistently. I found out the relevant hexcodes by analyzing the serial port traffic for Stollmann nfcplayer which is working perfectly for card emulation mode.
Does anybody have any idea whats wrong with the code? Or is there anything wrong with this approach?
Preamble, Postamble, length and checksum and other packet data added in the "ex" method. following executed before the detection thread
byte[] cmd0 = { 0x14, 0x01, 0x00, 0x01 };
this.pn532.ex(cmd0);
byte[] cmd1 = { 0x32, 0x02, 0x00, 0x0F, 0x0A };
this.pn532.ex(cmd1);
byte[] cmd2 = { 0x32, 0x05, 0xFF, 0xFF, 0xFF };
this.pn532.ex(cmd2);
Following is running inside a thread
private void HCEDetectionThread()
{
while (this.isRunning)
{
byte[] cmd1 = { 0x8C, 0x05, 0x04, 0x00, 0x14, 0x44, 0x3A,
0x20, 0x01, 0xFE, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x14,
0x44, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00 };
this.pn532.ex(cmd1);
byte[] cmd2 = { 0x14, 0x01, 0x00, 0x01 };
this.pn532.ex(cmd2);
byte[] cmd3 = { 0x32, 0x01, 0x00 };
this.pn532.ex(cmd3);
}
}
If the HCEDetectionThread is constantly sending frames to the PN532. The NFC chip rarely has time to establish the connection. That's why it fails.
Try to add a pause in the range of 100 to 300 milliseconds after the last frame.
Btw, you should give the Stollmann library a try. It is not that costly, and in the long run it may be cheaper overall. Close to 20 man-years of development went into it, and all the little quirks and problems that you will face later in your project have already been solved.
Disclamer: I worked for Stollmann and spend 3.5 years fulltime on exactly this NFC stack.