brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

RP3 not reading PN532 (RFID) over UART using node-serialport

Sun Apr 17, 2016 3:02 pm

I'm hoping someone could give me a quick pointer here, as I feel like I'm pretty close on this one. I have an Adafruit PN532 breakout board (https://www.adafruit.com/products/364) hooked up via UART to an RP3. I've attached a photo of my wiring, but just to be more explicit here on how it's wired up, I'm using the FTDI headers to hook up to my RP3 as follows (left is PN532, right is RP3):

Black: GND to pin 6 (GND)
[ no connection on NC ]
Red: 5.0V to pin 2 (5V)
Blue: TXD to GPIO 14 (UART_TXD)
Yellow: RXD to GPIO 15 (UART_RXD)
[ no connection on NC ]

The jumpers on the PN532 board are both set to OFF on SEL0/SEL1, indicating a UART configuration.

Here's what I'm running in my JS file:

Code: Select all

var SerialPort = require('serialport').SerialPort;
var port = new SerialPort('/dev/ttyAMA0');

port.on('open', function (err) {
  if (err) throw err;
  console.log('port open');

  port.on('data', function (data) {
    console.log(data);
  });
});
I do not receive an error, and I do see 'port open' in terminal. The 'data' event never gets raised. I do know that the PN532 board is working because I've previously hooked it up to nfclib in Python using a different wiring configuration, but my goal is to keep this project JS-only.

I have also attempted similar code using an XBee S2 over tty/USB0 and it works just fine, which tells me that there is unlikely any issue with Node permissions or the node-serialport package here. My guess is I just have something wired up incorrectly.

What am I doing wrong here? I'm open to any feedback, and thanks in advance for your expertise!
Attachments
rp3-pn532.jpg
rp3-pn532.jpg (31.08 KiB) Viewed 11432 times

User avatar
topguy
Posts: 4611
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 4:46 pm

I don't see that you specify speed (baudrate) in your code, quite an important parameter for serial communication.

The protocol for pn532 is not directly simple so I would check out existing node.js code. https://www.npmjs.com/package/pn532

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 5:43 pm

topguy wrote:I don't see that you specify speed (baudrate) in your code, quite an important parameter for serial communication.

The protocol for pn532 is not directly simple so I would check out existing node.js code. https://www.npmjs.com/package/pn532
That hasn't appeared to make any difference, unfortunately. I tried 115200, and also ttyS0, but none of those permutations have worked. Here's my port creation:

Code: Select all

var port = new SerialPort('/dev/ttyAMA0', {
  baudrate: 115200
});
I don't think it's a parser issue either since nothing at all is coming through

dgordon42
Posts: 741
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 6:18 pm

The serial port on the Pi 3B is different to previous models. The addition of Bluetooth to the Pi 3B ties up /dev/AMA0, and so instead the serial port is now /dev/ttyS0.
The PN532 set up guide on the Adafruit website is valid only for the Pi 2B and earlier models.

There are two ways to get the serial port working with your PN532.

1. If you do not plan on using Bluetooth on your PI, add the line:

Code: Select all

dtoverlay=pi3-disable-bt
to the end of the '/boot/config.txt' file, and reboot.
This will disable Bluetooth, and transfer /dev/AMA0 back to the serial port pins 14 & 15.

2. If you wish to use Bluetooth as well as the serial port, add the lines:

Code: Select all

force_turbo=0
core_freq=250
to the end of the '/boot/config.txt' file, and change the line:

Code: Select all

var port = new SerialPort('/dev/ttyAMA0');
to:

Code: Select all

var port = new SerialPort('/dev/ttyS0');
in your JS file, and reboot.
This will result in a small but probably unnoticeable drop in the Pi's speed, but will allow the use of Bluetooth and the serial port.

Regardless of which option you choose, you still have to disable the serial console with 'sudo raspi-config', as detailed in the PN532 set up guide.

I don't have a PN532 to test this with.

There's more to read on the Pi 3B serial port here and here.

Hope this helps,
Dave.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1767
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 7:07 pm

@dgordon42 What you've said is correct, but you can do even better.

1) If you don't want to disable Bluetooth completely then you can use another overlay - pi3-miniuart-bt - which swaps the roles of the UARTs. In order to make Bluetooth boot reliably you will also need to edit /lib/systemd/system/hciuart and change the 921600 to 460800.

2) There is a new config.txt setting - enable_uart - which defaults to 1 if ttyAMA0 is the default primary (non-BT) UART, and 0 if ttyS0 is the primary. If enable_uart is 1 then the firmware selects the appropriate UART function on GPIO pins 14 & 15, otherwise it doesn't. If ttyS0 is the primary, as is the case on Pi3 without either of the pi3-*-bt overlays loaded, then it also has the effect of locking the VPU core_freq to 250. As well as being clearer than core_freq=250, it also only limits the core when it needs to, i.e. not at all on a Pi2 unless you are using overlay-fu to make ttyS0 the primary.

3) Because we like SD card images to work in as many models of Pi as possible, there are now two symbolic links in /dev - /dev/serial0 and /dev/serial1. /dev/serial0 is configured to always point to the primary UART, while /dev/serial1 points to the other - the BT UART on a Pi3. So when you edit your code to make it Pi3 compatible, change /dev/ttyAMA0 to /dev/serial0 instead.

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 7:43 pm

Thanks for your quick response, Dave! I tried both with negative results :-/
dgordon42 wrote: 1. If you do not plan on using Bluetooth on your PI, add the line:

Code: Select all

dtoverlay=pi3-disable-bt
to the end of the '/boot/config.txt' file, and reboot.
This will disable Bluetooth, and transfer /dev/AMA0 back to the serial port pins 14 & 15.
dgordon42 wrote: 2. If you wish to use Bluetooth as well as the serial port, add the lines:

Code: Select all

force_turbo=0
core_freq=250
to the end of the '/boot/config.txt' file, and change the line:

Code: Select all

var port = new SerialPort('/dev/ttyAMA0');
to:

Code: Select all

var port = new SerialPort('/dev/ttyS0');
in your JS file, and reboot.
Also, I did disable serial console via raspi-config before trying any of this, so I don't think that's the problem either
dgordon42 wrote: Regardless of which option you choose, you still have to disable the serial console with 'sudo raspi-config', as detailed in the PN532 set up guide.

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 7:54 pm

PhilE wrote: 1) If you don't want to disable Bluetooth completely then you can use another overlay - pi3-miniuart-bt - which swaps the roles of the UARTs. In order to make Bluetooth boot reliably you will also need to edit /lib/systemd/system/hciuart and change the 921600 to 460800.
Tried this overlay with /dev/ttyAMA0, /dev/ttyS0, /dev/serial0 and /dev/serial1, but none of them worked
PhilE wrote: 2) There is a new config.txt setting - enable_uart - which defaults to 1 if ttyAMA0 is the default primary (non-BT) UART, and 0 if ttyS0 is the primary. If enable_uart is 1 then the firmware selects the appropriate UART function on GPIO pins 14 & 15, otherwise it doesn't. If ttyS0 is the primary, as is the case on Pi3 without either of the pi3-*-bt overlays loaded, then it also has the effect of locking the VPU core_freq to 250. As well as being clearer than core_freq=250, it also only limits the core when it needs to, i.e. not at all on a Pi2 unless you are using overlay-fu to make ttyS0 the primary.
I tried adding enable_uart=1 to the bottom of my /boot/config.txt file and rebooting, but that also doesn't work with any of the ports I tried above
PhilE wrote: 3) Because we like SD card images to work in as many models of Pi as possible, there are now two symbolic links in /dev - /dev/serial0 and /dev/serial1. /dev/serial0 is configured to always point to the primary UART, while /dev/serial1 points to the other - the BT UART on a Pi3. So when you edit your code to make it Pi3 compatible, change /dev/ttyAMA0 to /dev/serial0 instead.
Same as above...nothing seems to work here. Any chance I'm wired up incorrectly?

dgordon42
Posts: 741
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:04 pm

@PhilE,
Thanks for the info.
Just to confirm that I understand this correctly, the optimum method to get a serial port going, which will work correctly on any model Pi, is to add

Code: Select all

enable_uart
to '/boot/config.txt' and reference the serial port as '/dev/serial0' in my code.

I take it that it is still necessary to disable the console with raspi-config, or edit it out of '/boot/cmdline.txt', in order to use the serial port.

Thanks,
Dave.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1767
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:05 pm

Faulty wiring is always a possibility, but let's establish a few facts.

Using each of the p3-*-bt overlays in turn, what do you get from:

Code: Select all

ls -l /dev/serial*
sudo vcdbg log msg |& grep -v -E "(HDMI|gpioman|clock):"
Also, possibly a stupid question, but you do know that GPIO14 appears on header pin 8 and GPIO15 on header pin 10, don't you?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1767
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:08 pm

@dgordon42 The line you want is:

Code: Select all

enable_uart=1
Yes, you should also remove any references to serial ports from cmdline.txt - Jessie/systemd starts getty on serial ports according to the kernel command line config= setting, and removing that is enough to disable the console.

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:10 pm

PhilE wrote:Faulty wiring is always a possibility, but let's establish a few facts.

Using each of the p3-*-bt overlays in turn, what do you get from:

Code: Select all

ls -l /dev/serial*
sudo vcdbg log msg |& grep -v -E "(HDMI|gpioman|clock):"

Code: Select all

lrwxrwxrwx 1 root root 7 Apr 17 19:50 /dev/serial0 -> ttyAMA0
lrwxrwxrwx 1 root root 5 Apr 17 19:50 /dev/serial1 -> ttyS0
and then

Code: Select all

001170.330: *** Restart logging
001174.644: Read command line from file 'cmdline.txt'
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=d                                                                                                                                                             eadline fsck.repair=yes rootwait
001179.170: Loading 'kernel7.img' from SD card
001415.365: Kernel trailer DTOK property says yes
001415.549: Loading 'bcm2710-rpi-3-b.dtb' from SD card
001481.052: dtparam: uart0_clkrate=48000000
001495.326: dtparam: audio=on
001500.239: dtparam: spi=on
001507.279: dtparam: i2c_arm=on
001532.032: Loaded overlay 'pi3-miniuart-bt'
001618.907: dtparam: arm_freq=1200000000
001656.874: dtparam: core_freq=400000000
001666.276: dtparam: cache_line_size=64
002405.461: vchiq_core: vchiq_init_state: slot_zero = 0xfa880000, is_master = 1
PhilE wrote:Also, possibly a stupid question, but you do know that GPIO14 appears on header pin 8 and GPIO15 on header pin 10, don't you?
Not a stupid question...I'd like to explore all possibilities. Going off of this image:

Image

I'm using pin positions 2, 6, 8 and 10 for 5V, GND, TXD and RXD respectively

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1767
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:18 pm

Well that all looks fine - the overlay has clearly done what it should because /dev/serial0 -> ttyAMA0. Your pinout looks OK. Just to rule out a remote possibility, try this:

Code: Select all

sudo apt-get install raspi-gpio
raspi-gpio get
and look for GPIOs 14 and 15.

If that shows nothing unusual, do you have a PC with a serial port (or a USB serial port) you can hook it up to?

dgordon42
Posts: 741
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:21 pm

@brbarnett,
Tx, physical pin 8 on the Pi, should go to Rx on the PN532
Rx, physical pin 10 on the Pi, should go to Tx on the PN532
Looking at the photo you posted, they are reversed.

@PhilE,
Thanks for the clarification.

Dave.

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:21 pm

PhilE wrote:Well that all looks fine - the overlay has clearly done what it should because /dev/serial0 -> ttyAMA0. Your pinout looks OK. Just to rule out a remote possibility, try this:

Code: Select all

sudo apt-get install raspi-gpio
raspi-gpio get
and look for GPIOs 14 and 15.

If that shows nothing unusual, do you have a PC with a serial port (or a USB serial port) you can hook it up to?
I'm showing the following:

Code: Select all

BANK0 (GPIO 0 to 27):
  GPIO 00: level=1 fsel=0 alt=  func=INPUT
  GPIO 01: level=1 fsel=0 alt=  func=INPUT
  GPIO 02: level=1 fsel=4 alt=0 func=SDA1
  GPIO 03: level=1 fsel=4 alt=0 func=SCL1
  GPIO 04: level=1 fsel=0 alt=  func=INPUT
  GPIO 05: level=1 fsel=0 alt=  func=INPUT
  GPIO 06: level=1 fsel=0 alt=  func=INPUT
  GPIO 07: level=1 fsel=1 alt=  func=OUTPUT
  GPIO 08: level=1 fsel=1 alt=  func=OUTPUT
  GPIO 09: level=0 fsel=4 alt=0 func=SPI0_MISO
  GPIO 10: level=0 fsel=4 alt=0 func=SPI0_MOSI
  GPIO 11: level=0 fsel=4 alt=0 func=SPI0_SCLK
  GPIO 12: level=0 fsel=0 alt=  func=INPUT
  GPIO 13: level=0 fsel=0 alt=  func=INPUT
  GPIO 14: level=1 fsel=4 alt=0 func=TXD0
  GPIO 15: level=1 fsel=4 alt=0 func=RXD0
  GPIO 16: level=0 fsel=0 alt=  func=INPUT
  GPIO 17: level=0 fsel=0 alt=  func=INPUT
  GPIO 18: level=0 fsel=0 alt=  func=INPUT
  GPIO 19: level=0 fsel=0 alt=  func=INPUT
  GPIO 20: level=0 fsel=0 alt=  func=INPUT
  GPIO 21: level=0 fsel=0 alt=  func=INPUT
  GPIO 22: level=0 fsel=0 alt=  func=INPUT
  GPIO 23: level=0 fsel=0 alt=  func=INPUT
  GPIO 24: level=0 fsel=0 alt=  func=INPUT
  GPIO 25: level=0 fsel=0 alt=  func=INPUT
  GPIO 26: level=0 fsel=0 alt=  func=INPUT
  GPIO 27: level=0 fsel=0 alt=  func=INPUT
BANK1 (GPIO 28 to 45):
  GPIO 28: level=0 fsel=0 alt=  func=INPUT
  GPIO 29: level=1 fsel=0 alt=  func=INPUT
  GPIO 30: level=0 fsel=0 alt=  func=INPUT
  GPIO 31: level=0 fsel=0 alt=  func=INPUT
  GPIO 32: level=1 fsel=2 alt=5 func=TXD1
  GPIO 33: level=1 fsel=2 alt=5 func=RXD1
  GPIO 34: level=0 fsel=7 alt=3 func=SD1_CLK
  GPIO 35: level=1 fsel=7 alt=3 func=SD1_CMD
  GPIO 36: level=1 fsel=7 alt=3 func=SD1_DAT0
  GPIO 37: level=1 fsel=7 alt=3 func=SD1_DAT1
  GPIO 38: level=1 fsel=7 alt=3 func=SD1_DAT2
  GPIO 39: level=1 fsel=7 alt=3 func=SD1_DAT3
  GPIO 40: level=0 fsel=4 alt=0 func=PWM0
  GPIO 41: level=0 fsel=4 alt=0 func=PWM1
  GPIO 42: level=0 fsel=4 alt=0 func=GPCLK1
  GPIO 43: level=1 fsel=4 alt=0 func=GPCLK2
  GPIO 44: level=1 fsel=0 alt=  func=INPUT
  GPIO 45: level=1 fsel=0 alt=  func=INPUT
BANK2 (GPIO 46 to 53):
  GPIO 46: level=1 fsel=0 alt=  func=INPUT
  GPIO 47: level=1 fsel=0 alt=  func=INPUT
  GPIO 48: level=0 fsel=4 alt=0 func=SD0_CLK
  GPIO 49: level=1 fsel=4 alt=0 func=SD0_CMD
  GPIO 50: level=1 fsel=4 alt=0 func=SD0_DAT0
  GPIO 51: level=1 fsel=4 alt=0 func=SD0_DAT1
  GPIO 52: level=1 fsel=4 alt=0 func=SD0_DAT2
  GPIO 53: level=1 fsel=4 alt=0 func=SD0_DAT3
Does that look right? I do have a PC, but I don't have the right wires to hook it up

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:28 pm

dgordon42 wrote:@brbarnett,
Tx, physical pin 8 on the Pi, should go to Rx on the PN532
Rx, physical pin 10 on the Pi, should go to Tx on the PN532
Looking at the photo you posted, they are reversed.

@PhilE,
Thanks for the clarification.

Dave.
Maybe I've misunderstood how this works...I figured the wiring should match on both? So you're thinking TX => RX and RX => TX?

dgordon42
Posts: 741
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:32 pm

brbarnett wrote:So you're thinking TX => RX and RX => TX
That's the way the serial bus works. The Pi Receives the signal that the PL532 Transmits, and visa versa.

Dave.

DirkS
Posts: 8458
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:35 pm

dgordon42 wrote:
brbarnett wrote:So you're thinking TX => RX and RX => TX
That's the way the serial bus works. The Pi Receives the signal that the PL532 Transmits, and visa versa.

Dave.
Oddly enough, the wiring in the Adafruit docs seems to be TX-TX and RX-RX

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1767
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 8:43 pm

Your Pi configuration looks correct, so I'll duck out at this point.

dgordon42
Posts: 741
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 9:02 pm

@DirkS,
Looking at the PN532 wiring diagram, on the right hand side they do cross the Tx and Rx lines before they make it to the FTDI pad.
From this drawing the correct wiring would be Tx to Tx, and Rx to Rx, as brbarnett originally did.

Apologies for my mistake, but I've never seen a serial port wired like this before.
Thanks for the heads up,
Dave.

DirkS
Posts: 8458
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 9:13 pm

dgordon42 wrote:but I've never seen a serial port wired like this before.
I thought the same, but noticed the odd wiring by Adafruit before I posted :D

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 9:50 pm

PhilE wrote:Your Pi configuration looks correct, so I'll duck out at this point.
Thanks for trying, Phil!

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Sun Apr 17, 2016 10:10 pm

dgordon42 wrote:@DirkS,
Looking at the PN532 wiring diagram, on the right hand side they do cross the Tx and Rx lines before they make it to the FTDI pad.
From this drawing the correct wiring would be Tx to Tx, and Rx to Rx, as brbarnett originally did.
Seems like I'm wired up properly, and configured properly on the Pi. Maybe something's wacky with the breakout board or something. Maybe worth trying another wiring config? We don't need to use UART, just seems like the easiest way

dgordon42
Posts: 741
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Mon Apr 18, 2016 9:14 am

I'm wondering if you are using the libnfc library as shown in the installation guide? If so, then it's probably necessary to change all references to /dev/ttyAMA0 to /dev/serial0. See page 7 of the guide.

Dave.

brbarnett
Posts: 10
Joined: Sun Apr 17, 2016 2:34 pm

Re: RP3 not reading PN532 (RFID) over UART using node-serial

Mon Apr 18, 2016 1:06 pm

dgordon42 wrote:I'm wondering if you are using the libnfc library as shown in the installation guide? If so, then it's probably necessary to change all references to /dev/ttyAMA0 to /dev/serial0. See page 7 of the guide.

Dave.
So I read in another forum that it's more complicated than just hooking up to the serial port and expecting data. Apparently, there are commands that libnfc send to it to turn it on, initialize the field and then listen.

I tested out using https://www.npmjs.com/package/python-shell, where I'm executing the listener in Python and passing data back to Node via print statements. Might just have to go in that direction since it's what I have working. I think I just got unlucky with the PN532 in that it's not as simple as most boards. Seems funny that it's easier to hook up to XBees than it is to hook up to this board...

Thank you for all your help, guys...really appreciate it.

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 10 guests