after I send command to adafruit PN532:
00 00 ff 03 fd d4 14 01 17 00
I get answer:
00 00 C3 BF 00 C3 BF 00 00 00 C3 BF 02 C3 BE C3 95 15 16 00
Instead of:
00 00 ff 00 ff 00 00 00 ff 02 fe d5 15 16 00
I am communicating with PN532 using uart through serial port "/dev/ttyAMA0" I have code in Java. Reading from GPIO using pi4j. Do you know why I am getting this kind of mishmashed answer?
Here is my code:
public class NFCapp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
byte[] SAMConfiguration = {(byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0x03, (byte) 0xfd, (byte) 0xd4, (byte) 0x14, (byte) 0x01, (byte) 0x17, (byte) 0x00};
byte[] wakeUP = {(byte) 0x55, (byte) 0x55, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
final byte[] ack = {(byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0x00, (byte) 0xff, (byte) 0x00};
final Serial serial = SerialFactory.createInstance();
try {
serial.open("/dev/ttyAMA0", 115200);
serial.addListener(new SerialDataListener() {
@Override
public void dataReceived(SerialDataEvent event) {
String data = event.getData();
StringBuilder buffer = new StringBuilder();
byte[] array = data.getBytes();
System.out.println("Read: ");
for (int i = 0; i < array.length; i++) {
System.out.printf("%02X ", array[i]);
}
serial.write(ack);
}
});
System.out.println("Port Opened: " + serial.isOpen() + " ");
serial.write(wakeUP);
System.out.print("Write: ");
for (int i = 0; i < SAMConfiguration.length; i++) {
System.out.printf("%02X ", SAMConfiguration[i]);
}
System.out.println();
serial.write(SAMConfiguration);
for (;;) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
serial.close();
}
}
You want bytes, but you are reading a string and converting it to bytes. Unfortunately, it looks like in the process your data is getting UTF-8 encoded. For example 0xC3BF is the UTF-8 encoding of "\ÿ" unicode character. Also unfortunately, the only way to get data from SerialDataEvent is as a string. How the original bytes were decoded into a string depend on which string constructor was called. Looks like UTF-16 (probably big endian), so you might try this:
byte[] array = data.getBytes("UTF-16");
This is hacky, the real fix is to improve Pi4J and add a SerialDataEvent.getBytes() method. I hope this works for you.
This worked for me
data.getBytes("ISO-8859-1");
(data.getBytes("UTF-16") was returning 2 bytes per byte read, with 0x00 in the second byte)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.