1#!/usr/bin/env python3
2
3import argparse
4import binascii
5import logging
6import sys
7
8from pn532 import PN532
9
10logger = logging.getLogger()
11
12
13def die(msg, exc=None):
14  logger.error(msg)
15  sys.exit(1)
16
17
18def main():
19  parser = argparse.ArgumentParser(prog='pn532')
20  parser.add_argument(
21      '-p',
22      '--path',
23      action='store',
24      required=True,
25      help='Path to the PN532 serial device, e.g. /dev/ttyUSB0',
26  )
27  parser.add_argument(
28      '-f',
29      '--polling-frame',
30      action='store',
31      help='Optional custom polling frame in hex',
32  )
33
34  args = parser.parse_args()
35
36  polling_frame = None
37  if args.polling_frame:
38    try:
39      polling_frame = binascii.unhexlify(args.polling_frame)
40    except Exception as e:
41      die('Failed to parse polling frame hex: ' + str(e))
42
43  try:
44    pn = PN532(path=args.path)
45
46    while True:
47      pn.poll_a()
48      if polling_frame:
49        pn.send_broadcast(polling_frame)
50
51      pn.poll_b()
52      pn.mute()
53  except Exception as e:
54    die('Polling failed: ' + str(e))
55
56
57if __name__ == '__main__':
58  main()
59