..
 #############################################################
 # This file was automatically generated on 2015-05-08.      #
 #                                                           #
 # If you have a bugfix for this file and want to commit it, #
 # please fix the bug in the generator. You can find a link  #
 # to the generators git repository on tinkerforge.com       #
 #############################################################


:breadcrumbs: <a href="../../index.html">Home</a> / <a href="../../index.html#software-modbus">Software</a> / Modbus - NFC/RFID Bricklet

.. |device_identifier_constant| unicode:: 0xA0
   :trim:

.. _nfc_rfid_bricklet_modbus:

Modbus - NFC/RFID Bricklet
==========================

This is the description of the :ref:`Modbus protocol <llproto_modbus>` for the :ref:`NFC/RFID Bricklet <nfc_rfid_bricklet>`. General information and technical
specifications for the NFC/RFID Bricklet are summarized in its :ref:`hardware description
<nfc_rfid_bricklet_description>`.

.. _nfc_rfid_bricklet_modbus_api:

API
---

A general description of the Modbus protocol structure can be found
:ref:`here <llproto_modbus>`.




Basic Functions
^^^^^^^^^^^^^^^



.. modbus:function:: BrickletNFCRFID.request_tag_id

 :functionid: 1
 :request tag_type: uint8

 :noresponse: no response

 To read or write a tag that is in proximity of the NFC/RFID Bricklet you 
 first have to call this function with the expected tag type as parameter.
 It is no problem if you don't know the tag type. You can cycle through 
 the available tag types until the tag gives an answer to the request.
 
 Current the following tag types are supported:
 
 * Mifare Classic (``tag_type`` = 0)
 * NFC Forum Type 1 (``tag_type`` = 1)
 * NFC Forum Type 2 (``tag_type`` = 2)
 
 After you call :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>` the NFC/RFID Bricklet will try to read 
 the tag ID from the tag. After this process is done the state will change.
 You can either register the :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>` callback or you can poll
 :modbus:func:`get_state <BrickletNFCRFID.get_state>` to find out about the state change.
 
 If the state changes to *RequestTagIDError* it means that either there was 
 no tag present or that the tag is of an incompatible type. If the state 
 changes to *RequestTagIDReady* it means that a compatible tag was found 
 and that the tag ID could be read out. You can now get the tag ID by
 calling :modbus:func:`get_tag_id <BrickletNFCRFID.get_tag_id>`.
 
 If two tags are in the proximity of the NFC/RFID Bricklet, this
 function will cycle through the tags. To select a specific tag you have
 to call :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>` until the correct tag id is found.
 
 In case of any *Error* state the selection is lost and you have to
 start again by calling :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>`.
 
.. modbus:function:: BrickletNFCRFID.get_tag_id

 :functionid: 2
 :emptyrequest: empty payload

 :response tag_type: uint8
 :response tid_length: uint8
 :response tid: uint8[7]

 Returns the tag type, tag ID and the length of the tag ID 
 (4 or 7 bytes are possible length). This function can only be called if the
 NFC/RFID is currently in one of the *Ready* states. The returned ID
 is the ID that was saved through the last call of :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>`.
 
 To get the tag ID of a tag the approach is as follows:
 
 1. Call :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>`
 2. Wait for state to change to *RequestTagIDReady* (see :modbus:func:`get_state <BrickletNFCRFID.get_state>` or
    :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 3. Call :modbus:func:`get_tag_id <BrickletNFCRFID.get_tag_id>`
 
.. modbus:function:: BrickletNFCRFID.get_state

 :functionid: 3
 :emptyrequest: empty payload

 :response state: uint8
 :response idle: bool

 Returns the current state of the NFC/RFID Bricklet.
 
 On startup the Bricklet will be in the *Initialization* state. The
 initialization will only take about 20ms. After that it changes to *Idle*.
 
 The functions of this Bricklet can be called in the *Idle* state and all of
 the *Ready* and *Error* states.
 
 Example: If you call :modbus:func:`request_page <BrickletNFCRFID.request_page>`, the state will change to 
 *RequestPage* until the reading of the page is finished. Then it will change
 to either *RequestPageReady* if it worked or to *RequestPageError* if it
 didn't. If the request worked you can get the page by calling :modbus:func:`get_page <BrickletNFCRFID.get_page>`.
 
 The same approach is used analogously for the other API functions.
 
 Possible states are:
 
 * Initialization = 0
 * Idle = 128
 * Error = 192
 * RequestTagID = 2
 * RequestTagIDReady = 130
 * RequestTagIDError = 194
 * AuthenticatingMifareClassicPage = 3
 * AuthenticatingMifareClassicPageReady = 131
 * AuthenticatingMifareClassicPageError = 195
 * WritePage = 4
 * WritePageReady = 132
 * WritePageError = 196
 * RequestPage = 5
 * RequestPageReady = 133
 * RequestPageError = 197
 
.. modbus:function:: BrickletNFCRFID.authenticate_mifare_classic_page

 :functionid: 4
 :request page: uint16
 :request key_number: uint8
 :request key: uint8[6]

 :noresponse: no response

 Mifare Classic tags use authentication. If you want to read from or write to
 a Mifare Classic page you have to authenticate it beforehand.
 Each page can be authenticated with two keys: A (``key_number`` = 0) and B
 (``key_number`` = 1). A new Mifare Classic
 tag that has not yet been written to can can be accessed with key A
 and the default key ``[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]``.
 
 The approach to read or write a Mifare Classic page is as follows:
 
 1. Call :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>`
 2. Wait for state to change to *RequestTagIDReady* (see :modbus:func:`get_state <BrickletNFCRFID.get_state>`
    or :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 3. If looking for a specific tag then call :modbus:func:`get_tag_id <BrickletNFCRFID.get_tag_id>` and check if the
    expected tag was found, if it was not found got back to step 1
 4. Call :modbus:func:`authenticate_mifare_classic_page <BrickletNFCRFID.authenticate_mifare_classic_page>` with page and key for the page
 5. Wait for state to change to *AuthenticatingMifareClassicPageReady* (see
    :modbus:func:`get_state <BrickletNFCRFID.get_state>` or :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 6. Call :modbus:func:`request_page <BrickletNFCRFID.request_page>` or :modbus:func:`write_page <BrickletNFCRFID.write_page>` to read/write page
 
.. modbus:function:: BrickletNFCRFID.write_page

 :functionid: 5
 :request page: uint16
 :request data: uint8[16]

 :noresponse: no response

 Writes 16 bytes starting from the given page. How many pages are written
 depends on the tag type. The page sizes are as follows:
 
 * Mifare Classic page size: 16 byte (one page is written)
 * NFC Forum Type 1 page size: 8 byte (two pages are written)
 * NFC Forum Type 2 page size: 4 byte (four pages are written)
 
 The general approach for writing to a tag is as follows:
 
 1. Call :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>`
 2. Wait for state to change to *RequestTagIDReady* (see :modbus:func:`get_state <BrickletNFCRFID.get_state>` or
    :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 3. If looking for a specific tag then call :modbus:func:`get_tag_id <BrickletNFCRFID.get_tag_id>` and check if the
    expected tag was found, if it was not found got back to step 1
 4. Call :modbus:func:`write_page <BrickletNFCRFID.write_page>` with page number and data
 5. Wait for state to change to *WritePageReady* (see :modbus:func:`get_state <BrickletNFCRFID.get_state>` or
    :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 
 If you use a Mifare Classic tag you have to authenticate a page before you
 can write to it. See :modbus:func:`authenticate_mifare_classic_page <BrickletNFCRFID.authenticate_mifare_classic_page>`.
 
.. modbus:function:: BrickletNFCRFID.request_page

 :functionid: 6
 :request page: uint16

 :noresponse: no response

 Reads 16 bytes starting from the given page and stores them into a buffer. 
 The buffer can then be read out with :modbus:func:`get_page <BrickletNFCRFID.get_page>`.
 How many pages are read depends on the tag type. The page sizes are 
 as follows:
 
 * Mifare Classic page size: 16 byte (one page is read)
 * NFC Forum Type 1 page size: 8 byte (two pages are read)
 * NFC Forum Type 2 page size: 4 byte (four pages are read)
 
 The general approach for reading a tag is as follows:
 
 1. Call :modbus:func:`request_tag_id <BrickletNFCRFID.request_tag_id>`
 2. Wait for state to change to *RequestTagIDReady* (see :modbus:func:`get_state <BrickletNFCRFID.get_state>`
    or :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 3. If looking for a specific tag then call :modbus:func:`get_tag_id <BrickletNFCRFID.get_tag_id>` and check if the
    expected tag was found, if it was not found got back to step 1
 4. Call :modbus:func:`request_page <BrickletNFCRFID.request_page>` with page number
 5. Wait for state to change to *RequestPageReady* (see :modbus:func:`get_state <BrickletNFCRFID.get_state>`
    or :modbus:func:`CALLBACK_STATE_CHANGED <BrickletNFCRFID.CALLBACK_STATE_CHANGED>`)
 6. Call :modbus:func:`get_page <BrickletNFCRFID.get_page>` to retrieve the page from the buffer
 
 If you use a Mifare Classic tag you have to authenticate a page before you
 can read it. See :modbus:func:`authenticate_mifare_classic_page <BrickletNFCRFID.authenticate_mifare_classic_page>`.
 
.. modbus:function:: BrickletNFCRFID.get_page

 :functionid: 7
 :emptyrequest: empty payload

 :response data: uint8[16]

 Returns 16 bytes of data from an internal buffer. To fill the buffer
 with specific pages you have to call :modbus:func:`request_page <BrickletNFCRFID.request_page>` beforehand.
 


Advanced Functions
^^^^^^^^^^^^^^^^^^

.. modbus:function:: BrickletNFCRFID.get_identity

 :functionid: 255
 :emptyrequest: empty payload

 :response uid: char[8]
 :response connected_uid: char[8]
 :response position: char
 :response hardware_version: uint8[3]
 :response firmware_version: uint8[3]
 :response device_identifier: uint16

 Returns the UID, the UID where the Bricklet is connected to, 
 the position, the hardware and firmware version as well as the
 device identifier.
 
 The position can be 'a', 'b', 'c' or 'd'.
 
 The device identifier numbers can be found :ref:`here <device_identifier>`.
 |device_identifier_constant|
 


.. _nfc_rfid_bricklet_modbus_callbacks:

Callbacks
^^^^^^^^^

.. modbus:function:: BrickletNFCRFID.CALLBACK_STATE_CHANGED

 :functionid: 8

 :response state: uint8
 :response idle: bool


 This callback is called if the state of the NFC/RFID Bricklet changes.
 See :modbus:func:`get_state <BrickletNFCRFID.get_state>` for more information about the possible states.
 


