python/nfc_ros/nfc_ros_node.py
Go to the documentation of this file.
1 import nfc
2 import nfc.tag
3 import nfc.tag.tt1
4 import nfc.tag.tt1_broadcom
5 import nfc.tag.tt2
6 import nfc.tag.tt2_nxp
7 import nfc.tag.tt3
8 import nfc.tag.tt3_sony
9 import nfc.tag.tt4
10 
11 import rospy
12 from nfc_ros.msg import Tag
13 from nfc_ros.msg import NDEFRecord
14 
15 from nfc_ros.nfc_interface import NFCInterface
16 
17 
18 class NFCROSNode:
19 
20  def __init__(self):
21 
23  self.pub_tag = rospy.Publisher('~tag', Tag, queue_size=1)
24  rospy.loginfo('Initialized')
25 
26  def publish_tag(self, tag):
27 
28  rospy.loginfo('tag: {}'.format(tag))
29  msg = Tag()
30 
31  if type(tag.identifier) == bytes:
32  msg.identifier = tag.identifier.hex()
33  else:
34  msg.identifier = tag.identifier
35 
36  if type(tag) == nfc.tag.tt1.Type1Tag:
37  msg.tag_type = Tag.NFC_TAG_TYPE1TAG
38  elif type(tag) == nfc.tag.tt2.Type2Tag:
39  msg.tag_type = Tag.NFC_TAG_TYPE2TAG
40  elif type(tag) == nfc.tag.tt2_nxp.NTAG215:
41  msg.tag_type = Tag.NFC_TAG_TYPE2NXPNTAG215
42  elif type(tag) == nfc.tag.tt3.Type3Tag:
43  msg.tag_type = Tag.NFC_TAG_TYPE3TAG
44  elif type(tag) == nfc.tag.tt3_sony.FelicaStandard:
45  msg.tag_type == Tag.NFC_TAG_FelicaStandard
46  elif type(tag) == nfc.tag.tt3_sony.FelicaMobile:
47  msg.tag_type == Tag.NFC_TAG_FelicaMobile
48  elif type(tag) == nfc.tag.tt3_sony.FelicaLite:
49  msg.tag_type == Tag.NFC_TAG_FelicaLite
50  elif type(tag) == nfc.tag.tt3_sony.FelicaLiteS:
51  msg.tag_type == Tag.NFC_TAG_FelicaLiteS
52  elif type(tag) == nfc.tag.tt3_sony.FelicaPlug:
53  msg.tag_type == Tag.NFC_TAG_FelicaPlug
54  elif type(tag) == nfc.tag.tt4.Type4Tag:
55  msg.tag_type = Tag.NFC_TAG_TYPE4TAG
56  elif type(tag) == nfc.tag.tt4.Type4ATag:
57  msg.tag_type = Tag.NFC_TAG_TYPE4ATAG
58  elif type(tag) == nfc.tag.tt4.Type4BTag:
59  msg.tag_type = Tag.NFC_TAG_TYPE4BTAG
60  else:
61  rospy.logerr('Unknown tag type: {}'.format(type(tag)))
62  return True
63 
64  if tag.ndef is not None:
65  for record in tag.ndef.records:
66  ndef_record = NDEFRecord()
67  ndef_record.type = record.type
68  ndef_record.name = record.name
69  ndef_record.data = record.data.decode()
70  msg.ndef_records.append(ndef_record)
71 
72  self.pub_tag.publish(msg)
73  return True
74 
75  def spin(self):
76 
77  while not rospy.is_shutdown():
78  self.interface.read_card(on_connect=self.publish_tag)


nfc_ros
Author(s): Koki Shinjo
autogenerated on Sat Jun 24 2023 02:40:29