Mathematica - NFC Bricklet

This is the description of the Mathematica API bindings for the NFC Bricklet. General information and technical specifications for the NFC Bricklet are summarized in its hardware description.

An installation guide for the Mathematica API bindings is part of their general description.

API

Generally, every function of the Mathematica bindings that returns a value can throw a Tinkerforge.TimeoutException. This exception gets thrown if the device did not respond. If a cable based connection is used, it is unlikely that this exception gets thrown (assuming nobody plugs the device out). However, if a wireless connection is used, timeouts will occur if the distance to the device gets too big.

Since .NET/Link does not support multiple return values directly, we use the out keyword to return multiple values from a function. For further information about the out keyword in .NET/Link see the corresponding Mathematica .NET/Link documentation.

The namespace for all Brick/Bricklet bindings and the IPConnection is Tinkerforge.*.

Basic Functions

BrickletNFC[uid, ipcon] → nfc
Parameters:
  • uid – Type: String
  • ipcon – Type: NETObject[IPConnection]
Returns:
  • nfc – Type: NETObject[BrickletNFC]

Creates an object with the unique device ID uid:

nfc=NETNew["Tinkerforge.BrickletNFC","YOUR_DEVICE_UID",ipcon]

This object can then be used after the IP Connection is connected.

The .NET runtime has built-in garbage collection that frees objects that are no longer in use by a program. But because Mathematica can not automatically tell when a Mathematica "program" doesn't use a .NET object anymore, this has to be done by the program. For this the ReleaseNETObject[] function is used in the examples.

For further information about object management in .NET/Link see the corresponding Mathematica .NET/Link documentation.

BrickletNFC@SetMode[mode] → Null
Parameters:
  • mode – Type: Integer, Range: See constants, Default: 0

Sets the mode. The NFC Bricklet supports four modes:

  • Off
  • Card Emulation (Cardemu): Emulates a tag for other readers
  • Peer to Peer (P2P): Exchange data with other readers
  • Reader: Reads and writes tags
  • Simple: Automatically reads tag IDs

If you change a mode, the Bricklet will reconfigure the hardware for this mode. Therefore, you can only use functions corresponding to the current mode. For example, in Reader mode you can only use Reader functions.

The following constants are available for this function:

For mode:

  • BrickletNFC`MODEUOFF = 0
  • BrickletNFC`MODEUCARDEMU = 1
  • BrickletNFC`MODEUP2P = 2
  • BrickletNFC`MODEUREADER = 3
  • BrickletNFC`MODEUSIMPLE = 4
BrickletNFC@GetMode[] → mode
Returns:
  • mode – Type: Integer, Range: See constants, Default: 0

Returns the mode as set by SetMode[].

The following constants are available for this function:

For mode:

  • BrickletNFC`MODEUOFF = 0
  • BrickletNFC`MODEUCARDEMU = 1
  • BrickletNFC`MODEUP2P = 2
  • BrickletNFC`MODEUREADER = 3
  • BrickletNFC`MODEUSIMPLE = 4
BrickletNFC@ReaderRequestTagID[] → Null

After you call ReaderRequestTagID[] the NFC 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 ReaderStateChangedCallback callback or you can poll ReaderGetState[] to find out about the state change.

If the state changes to ReaderRequestTagIDError it means that either there was no tag present or that the tag has an incompatible type. If the state changes to ReaderRequestTagIDReady it means that a compatible tag was found and that the tag ID has been saved. You can now read out the tag ID by calling ReaderGetTagID[].

If two tags are in the proximity of the NFC Bricklet, this function will cycle through the tags. To select a specific tag you have to call ReaderRequestTagID[] until the correct tag ID is found.

In case of any ReaderError state the selection is lost and you have to start again by calling ReaderRequestTagID[].

BrickletNFC@ReaderGetTagID[out tagType, out {tagID1, tagID2, ...}] → Null
Output Parameters:
  • tagType – Type: Integer, Range: See constants
  • tagIDi – Type: Integer, Range: [0 to 255]

Returns the tag type and the tag ID. This function can only be called if the NFC Bricklet is currently in one of the ReaderReady states. The returned tag ID is the tag ID that was saved through the last call of ReaderRequestTagID[].

To get the tag ID of a tag the approach is as follows:

  1. Call ReaderRequestTagID[]
  2. Wait for state to change to ReaderRequestTagIDReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  3. Call ReaderGetTagID[]

The following constants are available for this function:

For tagType:

  • BrickletNFC`TAGUTYPEUMIFAREUCLASSIC = 0
  • BrickletNFC`TAGUTYPEUTYPE1 = 1
  • BrickletNFC`TAGUTYPEUTYPE2 = 2
  • BrickletNFC`TAGUTYPEUTYPE3 = 3
  • BrickletNFC`TAGUTYPEUTYPE4 = 4
  • BrickletNFC`TAGUTYPEUTYPE5 = 5
BrickletNFC@ReaderGetState[out state, out idle] → Null
Output Parameters:
  • state – Type: Integer, Range: See constants
  • idle – Type: True/False

Returns the current reader state of the NFC Bricklet.

On startup the Bricklet will be in the ReaderInitialization state. The initialization will only take about 20ms. After that it changes to ReaderIdle.

The Bricklet is also reinitialized if the mode is changed, see SetMode[].

The functions of this Bricklet can be called in the ReaderIdle state and all of the ReaderReady and ReaderError states.

Example: If you call ReaderRequestPage[], the state will change to ReaderRequestPage until the reading of the page is finished. Then it will change to either ReaderRequestPageReady if it worked or to ReaderRequestPageError if it didn't. If the request worked you can get the page by calling ReaderReadPage[].

The same approach is used analogously for the other API functions.

The following constants are available for this function:

For state:

  • BrickletNFC`READERUSTATEUINITIALIZATION = 0
  • BrickletNFC`READERUSTATEUIDLE = 128
  • BrickletNFC`READERUSTATEUERROR = 192
  • BrickletNFC`READERUSTATEUREQUESTUTAGUID = 2
  • BrickletNFC`READERUSTATEUREQUESTUTAGUIDUREADY = 130
  • BrickletNFC`READERUSTATEUREQUESTUTAGUIDUERROR = 194
  • BrickletNFC`READERUSTATEUAUTHENTICATEUMIFAREUCLASSICUPAGE = 3
  • BrickletNFC`READERUSTATEUAUTHENTICATEUMIFAREUCLASSICUPAGEUREADY = 131
  • BrickletNFC`READERUSTATEUAUTHENTICATEUMIFAREUCLASSICUPAGEUERROR = 195
  • BrickletNFC`READERUSTATEUWRITEUPAGE = 4
  • BrickletNFC`READERUSTATEUWRITEUPAGEUREADY = 132
  • BrickletNFC`READERUSTATEUWRITEUPAGEUERROR = 196
  • BrickletNFC`READERUSTATEUREQUESTUPAGE = 5
  • BrickletNFC`READERUSTATEUREQUESTUPAGEUREADY = 133
  • BrickletNFC`READERUSTATEUREQUESTUPAGEUERROR = 197
  • BrickletNFC`READERUSTATEUWRITEUNDEF = 6
  • BrickletNFC`READERUSTATEUWRITEUNDEFUREADY = 134
  • BrickletNFC`READERUSTATEUWRITEUNDEFUERROR = 198
  • BrickletNFC`READERUSTATEUREQUESTUNDEF = 7
  • BrickletNFC`READERUSTATEUREQUESTUNDEFUREADY = 135
  • BrickletNFC`READERUSTATEUREQUESTUNDEFUERROR = 199
BrickletNFC@ReaderWriteNDEF[{ndef1, ndef2, ...}] → Null
Parameters:
  • ndefi – Type: Integer, Range: [0 to 255]

Writes NDEF formated data.

This function currently supports NFC Forum Type 2, 4, 5 and Mifare Classic.

The general approach for writing a NDEF message is as follows:

  1. Call ReaderRequestTagID[]
  2. Wait for state to change to ReaderRequestTagIDReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  3. If looking for a specific tag then call ReaderGetTagID[] and check if the expected tag was found, if it was not found got back to step 1
  4. Call ReaderWriteNDEF[] with the NDEF message that you want to write
  5. Wait for state to change to ReaderWriteNDEFReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
BrickletNFC@ReaderRequestNDEF[] → Null

Reads NDEF formated data from a tag.

This function currently supports NFC Forum Type 1, 2, 3, 4, 5 and Mifare Classic.

The general approach for reading a NDEF message is as follows:

  1. Call ReaderRequestTagID[]
  2. Wait for state to change to RequestTagIDReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  3. If looking for a specific tag then call ReaderGetTagID[] and check if the expected tag was found, if it was not found got back to step 1
  4. Call ReaderRequestNDEF[]
  5. Wait for state to change to ReaderRequestNDEFReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  6. Call ReaderReadNDEF[] to retrieve the NDEF message from the buffer
BrickletNFC@ReaderReadNDEF[] → {ndef1, ndef2, ...}
Returns:
  • ndefi – Type: Integer, Range: [0 to 255]

Returns the NDEF data from an internal buffer. To fill the buffer with a NDEF message you have to call ReaderRequestNDEF[] beforehand.

BrickletNFC@ReaderAuthenticateMifareClassicPage[page, keyNumber, {key1, key2, ..., key6}] → Null
Parameters:
  • page – Type: Integer, Range: [0 to 216 - 1]
  • keyNumber – Type: Integer, Range: See constants
  • keyi – Type: Integer, Range: [0 to 255]

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 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 ReaderRequestTagID[]
  2. Wait for state to change to ReaderRequestTagIDReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  3. If looking for a specific tag then call ReaderGetTagID[] and check if the expected tag was found, if it was not found got back to step 1
  4. Call ReaderAuthenticateMifareClassicPage[] with page and key for the page
  5. Wait for state to change to ReaderAuthenticatingMifareClassicPageReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  6. Call ReaderRequestPage[] or ReaderWritePage[] to read/write page

The authentication will always work for one whole sector (4 pages).

The following constants are available for this function:

For keyNumber:

  • BrickletNFC`KEYUA = 0
  • BrickletNFC`KEYUB = 1
BrickletNFC@ReaderWritePage[page, {data1, data2, ...}] → Null
Parameters:
  • page – Type: Integer, Range: See constants
  • datai – Type: Integer, Range: [0 to 255]

Writes a maximum of 8192 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
  • NFC Forum Type 1 page size: 8 byte
  • NFC Forum Type 2 page size: 4 byte
  • NFC Forum Type 3 page size: 16 byte
  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)
  • NFC Forum Type 5 page size: 4 byte

The general approach for writing to a tag is as follows:

  1. Call ReaderRequestTagID[]
  2. Wait for state to change to ReaderRequestTagIDReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  3. If looking for a specific tag then call ReaderGetTagID[] and check if the expected tag was found, if it was not found got back to step 1
  4. Call ReaderWritePage[] with page number and data
  5. Wait for state to change to ReaderWritePageReady (see ReaderGetState[] or ReaderStateChangedCallback callback)

If you use a Mifare Classic tag you have to authenticate a page before you can write to it. See ReaderAuthenticateMifareClassicPage[].

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.

The following constants are available for this function:

For page:

  • BrickletNFC`READERUWRITEUTYPE4UCAPABILITYUCONTAINER = 3
  • BrickletNFC`READERUWRITEUTYPE4UNDEF = 4
BrickletNFC@ReaderRequestPage[page, length] → Null
Parameters:
  • page – Type: Integer, Range: See constants
  • length – Type: Integer, Range: [0 to 213]

Reads a maximum of 8192 bytes starting from the given page and stores them into a buffer. The buffer can then be read out with ReaderReadPage[]. How many pages are read depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte
  • NFC Forum Type 1 page size: 8 byte
  • NFC Forum Type 2 page size: 4 byte
  • NFC Forum Type 3 page size: 16 byte
  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)
  • NFC Forum Type 5 page size: 4 byte

The general approach for reading a tag is as follows:

  1. Call ReaderRequestTagID[]
  2. Wait for state to change to RequestTagIDReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  3. If looking for a specific tag then call ReaderGetTagID[] and check if the expected tag was found, if it was not found got back to step 1
  4. Call ReaderRequestPage[] with page number
  5. Wait for state to change to ReaderRequestPageReady (see ReaderGetState[] or ReaderStateChangedCallback callback)
  6. Call ReaderReadPage[] 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 ReaderAuthenticateMifareClassicPage[].

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.

The following constants are available for this function:

For page:

  • BrickletNFC`READERUREQUESTUTYPE4UCAPABILITYUCONTAINER = 3
  • BrickletNFC`READERUREQUESTUTYPE4UNDEF = 4
BrickletNFC@ReaderReadPage[] → {data1, data2, ...}
Returns:
  • datai – Type: Integer, Range: [0 to 255]

Returns the page data from an internal buffer. To fill the buffer with specific pages you have to call ReaderRequestPage[] beforehand.

BrickletNFC@CardemuGetState[out state, out idle] → Null
Output Parameters:
  • state – Type: Integer, Range: See constants
  • idle – Type: True/False

Returns the current cardemu state of the NFC Bricklet.

On startup the Bricklet will be in the CardemuInitialization state. The initialization will only take about 20ms. After that it changes to CardemuIdle.

The Bricklet is also reinitialized if the mode is changed, see SetMode[].

The functions of this Bricklet can be called in the CardemuIdle state and all of the CardemuReady and CardemuError states.

Example: If you call CardemuStartDiscovery[], the state will change to CardemuDiscover until the discovery is finished. Then it will change to either CardemuDiscoverReady if it worked or to CardemuDiscoverError if it didn't.

The same approach is used analogously for the other API functions.

The following constants are available for this function:

For state:

  • BrickletNFC`CARDEMUUSTATEUINITIALIZATION = 0
  • BrickletNFC`CARDEMUUSTATEUIDLE = 128
  • BrickletNFC`CARDEMUUSTATEUERROR = 192
  • BrickletNFC`CARDEMUUSTATEUDISCOVER = 2
  • BrickletNFC`CARDEMUUSTATEUDISCOVERUREADY = 130
  • BrickletNFC`CARDEMUUSTATEUDISCOVERUERROR = 194
  • BrickletNFC`CARDEMUUSTATEUTRANSFERUNDEF = 3
  • BrickletNFC`CARDEMUUSTATEUTRANSFERUNDEFUREADY = 131
  • BrickletNFC`CARDEMUUSTATEUTRANSFERUNDEFUERROR = 195
BrickletNFC@CardemuStartDiscovery[] → Null

Starts the discovery process. If you call this function while a NFC reader device is near to the NFC Bricklet the state will change from CardemuDiscovery to CardemuDiscoveryReady.

If no NFC reader device can be found or if there is an error during discovery the cardemu state will change to CardemuDiscoveryError. In this case you have to restart the discovery process.

If the cardemu state changes to CardemuDiscoveryReady you can start the NDEF message transfer with CardemuWriteNDEF[] and CardemuStartTransfer[].

BrickletNFC@CardemuWriteNDEF[{ndef1, ndef2, ...}] → Null
Parameters:
  • ndefi – Type: Integer, Range: [0 to 255]

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size in Cardemu mode is 255 byte.

You can call this function at any time in Cardemu mode. The internal buffer will not be overwritten until you call this function again or change the mode.

BrickletNFC@CardemuStartTransfer[transfer] → Null
Parameters:
  • transfer – Type: Integer, Range: See constants

You can start the transfer of a NDEF message if the cardemu state is CardemuDiscoveryReady.

Before you call this function to start a write transfer, the NDEF message that is to be transferred has to be written via CardemuWriteNDEF[] first.

After you call this function the state will change to CardemuTransferNDEF. It will change to CardemuTransferNDEFReady if the transfer was successful or CardemuTransferNDEFError if it wasn't.

The following constants are available for this function:

For transfer:

  • BrickletNFC`CARDEMUUTRANSFERUABORT = 0
  • BrickletNFC`CARDEMUUTRANSFERUWRITE = 1
BrickletNFC@P2PGetState[out state, out idle] → Null
Output Parameters:
  • state – Type: Integer, Range: See constants
  • idle – Type: True/False

Returns the current P2P state of the NFC Bricklet.

On startup the Bricklet will be in the P2PInitialization state. The initialization will only take about 20ms. After that it changes to P2PIdle.

The Bricklet is also reinitialized if the mode is changed, see SetMode[].

The functions of this Bricklet can be called in the P2PIdle state and all of the P2PReady and P2PError states.

Example: If you call P2PStartDiscovery[], the state will change to P2PDiscover until the discovery is finished. Then it will change to either P2PDiscoverReady* if it worked or to P2PDiscoverError if it didn't.

The same approach is used analogously for the other API functions.

The following constants are available for this function:

For state:

  • BrickletNFC`P2PUSTATEUINITIALIZATION = 0
  • BrickletNFC`P2PUSTATEUIDLE = 128
  • BrickletNFC`P2PUSTATEUERROR = 192
  • BrickletNFC`P2PUSTATEUDISCOVER = 2
  • BrickletNFC`P2PUSTATEUDISCOVERUREADY = 130
  • BrickletNFC`P2PUSTATEUDISCOVERUERROR = 194
  • BrickletNFC`P2PUSTATEUTRANSFERUNDEF = 3
  • BrickletNFC`P2PUSTATEUTRANSFERUNDEFUREADY = 131
  • BrickletNFC`P2PUSTATEUTRANSFERUNDEFUERROR = 195
BrickletNFC@P2PStartDiscovery[] → Null

Starts the discovery process. If you call this function while another NFC P2P enabled device is near to the NFC Bricklet the state will change from P2PDiscovery to P2PDiscoveryReady.

If no NFC P2P enabled device can be found or if there is an error during discovery the P2P state will change to P2PDiscoveryError. In this case you have to restart the discovery process.

If the P2P state changes to P2PDiscoveryReady you can start the NDEF message transfer with P2PStartTransfer[].

BrickletNFC@P2PWriteNDEF[{ndef1, ndef2, ...}] → Null
Parameters:
  • ndefi – Type: Integer, Range: [0 to 255]

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size for P2P transfer is 255 byte.

You can call this function at any time in P2P mode. The internal buffer will not be overwritten until you call this function again, change the mode or use P2P to read an NDEF messages.

BrickletNFC@P2PStartTransfer[transfer] → Null
Parameters:
  • transfer – Type: Integer, Range: See constants

You can start the transfer of a NDEF message if the P2P state is P2PDiscoveryReady.

Before you call this function to start a write transfer, the NDEF message that is to be transferred has to be written via P2PWriteNDEF[] first.

After you call this function the P2P state will change to P2PTransferNDEF. It will change to P2PTransferNDEFReady if the transfer was successfull or P2PTransferNDEFError if it wasn't.

If you started a write transfer you are now done. If you started a read transfer you can now use P2PReadNDEF[] to read the NDEF message that was written by the NFC peer.

The following constants are available for this function:

For transfer:

  • BrickletNFC`P2PUTRANSFERUABORT = 0
  • BrickletNFC`P2PUTRANSFERUWRITE = 1
  • BrickletNFC`P2PUTRANSFERUREAD = 2
BrickletNFC@P2PReadNDEF[] → {ndef1, ndef2, ...}
Returns:
  • ndefi – Type: Integer, Range: [0 to 255]

Returns the NDEF message that was written by a NFC peer in NFC P2P mode.

The NDEF message is ready if you called P2PStartTransfer[] with a read transfer and the P2P state changed to P2PTransferNDEFReady.

BrickletNFC@SimpleGetTagID[index, out tagType, out {tagID1, tagID2, ...}, out lastSeen] → Null
Parameters:
  • index – Type: Integer, Range: [0 to 7]
Output Parameters:
  • tagType – Type: Integer, Range: See constants
  • tagIDi – Type: Integer, Range: [0 to 255]
  • lastSeen – Type: Integer, Unit: 1 ms, Range: [0 to 232 - 1]

Returns the tag type and tag ID from simple mode sorted by last seen time for a given index.

Up to eight tags are saved.

The following constants are available for this function:

For tagType:

  • BrickletNFC`TAGUTYPEUMIFAREUCLASSIC = 0
  • BrickletNFC`TAGUTYPEUTYPE1 = 1
  • BrickletNFC`TAGUTYPEUTYPE2 = 2
  • BrickletNFC`TAGUTYPEUTYPE3 = 3
  • BrickletNFC`TAGUTYPEUTYPE4 = 4
  • BrickletNFC`TAGUTYPEUTYPE5 = 5

New in version 2.0.6 (Plugin).

BrickletNFC@CardemuSetTagID[tagIDLength, {tagIDData1, tagIDData2, ..., tagIDData7}] → Null
Parameters:
  • tagIDLength – Type: Integer, Range: [0 to 7]
  • tagIDDatai – Type: Integer, Range: [0 to 255]

Sets the tag ID for cardemu mode. The tag ID can either have a length of 4 or 7.

Set a length of 0 for random tag ID (default)

New in version 2.1.0 (Plugin).

BrickletNFC@CardemuGetTagID[out tagIDLength, out {tagIDData1, tagIDData2, ..., tagIDData7}] → Null
Output Parameters:
  • tagIDLength – Type: Integer, Range: [0 to 7]
  • tagIDDatai – Type: Integer, Range: [0 to 255]

Returns the tag ID and length as set by CardemuSetTagID[].

New in version 2.1.0 (Plugin).

Advanced Functions

BrickletNFC@SetDetectionLEDConfig[config] → Null
Parameters:
  • config – Type: Integer, Range: See constants, Default: 3

Sets the detection LED configuration. By default the LED shows if a card/reader is detected.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is off.

The following constants are available for this function:

For config:

  • BrickletNFC`DETECTIONULEDUCONFIGUOFF = 0
  • BrickletNFC`DETECTIONULEDUCONFIGUON = 1
  • BrickletNFC`DETECTIONULEDUCONFIGUSHOWUHEARTBEAT = 2
  • BrickletNFC`DETECTIONULEDUCONFIGUSHOWUDETECTION = 3
BrickletNFC@GetDetectionLEDConfig[] → config
Returns:
  • config – Type: Integer, Range: See constants, Default: 3

Returns the configuration as set by SetDetectionLEDConfig[]

The following constants are available for this function:

For config:

  • BrickletNFC`DETECTIONULEDUCONFIGUOFF = 0
  • BrickletNFC`DETECTIONULEDUCONFIGUON = 1
  • BrickletNFC`DETECTIONULEDUCONFIGUSHOWUHEARTBEAT = 2
  • BrickletNFC`DETECTIONULEDUCONFIGUSHOWUDETECTION = 3
BrickletNFC@SetMaximumTimeout[timeout] → Null
Parameters:
  • timeout – Type: Integer, Unit: 1 ms, Range: [0 to 216 - 1], Default: 2000

Sets the maximum timeout.

This is a global maximum used for all internal state timeouts. The timeouts depend heavily on the used tags etc. For example: If you use a Type 2 tag and you want to detect if it is present, you have to use ReaderRequestTagID[] and wait for the state to change to either the error state or the ready state.

With the default configuration this takes 2-3 seconds. By setting the maximum timeout to 100ms you can reduce this time to ~150-200ms. For Type 2 this would also still work with a 20ms timeout (a Type 2 tag answers usually within 10ms). A type 4 tag can take up to 500ms in our tests.

If you need a fast response time to discover if a tag is present or not you can find a good timeout value by trial and error for your specific tag.

By default we use a very conservative timeout, to be sure that any tag can always answer in time.

New in version 2.0.1 (Plugin).

BrickletNFC@GetMaximumTimeout[] → timeout
Returns:
  • timeout – Type: Integer, Unit: 1 ms, Range: [0 to 216 - 1], Default: 2000

Returns the timeout as set by SetMaximumTimeout[]

New in version 2.0.1 (Plugin).

BrickletNFC@GetSPITFPErrorCount[out errorCountAckChecksum, out errorCountMessageChecksum, out errorCountFrame, out errorCountOverflow] → Null
Output Parameters:
  • errorCountAckChecksum – Type: Integer, Range: [0 to 232 - 1]
  • errorCountMessageChecksum – Type: Integer, Range: [0 to 232 - 1]
  • errorCountFrame – Type: Integer, Range: [0 to 232 - 1]
  • errorCountOverflow – Type: Integer, Range: [0 to 232 - 1]

Returns the error count for the communication between Brick and Bricklet.

The errors are divided into

  • ACK checksum errors,
  • message checksum errors,
  • framing errors and
  • overflow errors.

The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.

BrickletNFC@SetStatusLEDConfig[config] → Null
Parameters:
  • config – Type: Integer, Range: See constants, Default: 3

Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.

The following constants are available for this function:

For config:

  • BrickletNFC`STATUSULEDUCONFIGUOFF = 0
  • BrickletNFC`STATUSULEDUCONFIGUON = 1
  • BrickletNFC`STATUSULEDUCONFIGUSHOWUHEARTBEAT = 2
  • BrickletNFC`STATUSULEDUCONFIGUSHOWUSTATUS = 3
BrickletNFC@GetStatusLEDConfig[] → config
Returns:
  • config – Type: Integer, Range: See constants, Default: 3

Returns the configuration as set by SetStatusLEDConfig[]

The following constants are available for this function:

For config:

  • BrickletNFC`STATUSULEDUCONFIGUOFF = 0
  • BrickletNFC`STATUSULEDUCONFIGUON = 1
  • BrickletNFC`STATUSULEDUCONFIGUSHOWUHEARTBEAT = 2
  • BrickletNFC`STATUSULEDUCONFIGUSHOWUSTATUS = 3
BrickletNFC@GetChipTemperature[] → temperature
Returns:
  • temperature – Type: Integer, Unit: 1 °C, Range: [-215 to 215 - 1]

Returns the temperature as measured inside the microcontroller. The value returned is not the ambient temperature!

The temperature is only proportional to the real temperature and it has bad accuracy. Practically it is only useful as an indicator for temperature changes.

BrickletNFC@Reset[] → Null

Calling this function will reset the Bricklet. All configurations will be lost.

After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!

BrickletNFC@GetIdentity[out uid, out connectedUid, out position, out {hardwareVersion1, hardwareVersion2, hardwareVersion3}, out {firmwareVersion1, firmwareVersion2, firmwareVersion3}, out deviceIdentifier] → Null
Output Parameters:
  • uid – Type: String, Length: up to 8
  • connectedUid – Type: String, Length: up to 8
  • position – Type: Integer, Range: [ToCharacterCode["a"][[0]] to ToCharacterCode["h"][[0]], ToCharacterCode["z"][[0]]]
  • hardwareVersioni – Type: Integer
    • 1: major – Type: Integer, Range: [0 to 255]
    • 2: minor – Type: Integer, Range: [0 to 255]
    • 3: revision – Type: Integer, Range: [0 to 255]
  • firmwareVersioni – Type: Integer
    • 1: major – Type: Integer, Range: [0 to 255]
    • 2: minor – Type: Integer, Range: [0 to 255]
    • 3: revision – Type: Integer, Range: [0 to 255]
  • deviceIdentifier – Type: Integer, Range: [0 to 216 - 1]

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', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done by assigning a function to a callback property of the device object:

MyCallback[sender_,value_]:=Print["Value: "<>ToString[value]]

AddEventHandler[nfc@ExampleCallback,MyCallback]

For further information about event handling using .NET/Link see the corresponding Mathematica .NET/Link documentation.

The available callback property and their type of parameters are described below.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

event BrickletNFC@ReaderStateChangedCallback[sender, state, idle]
Callback Parameters:
  • sender – Type: NETObject[BrickletNFC]
  • state – Type: Integer, Range: See constants
  • idle – Type: True/False

This callback is called if the reader state of the NFC Bricklet changes. See ReaderGetState[] for more information about the possible states.

The following constants are available for this function:

For state:

  • BrickletNFC`READERUSTATEUINITIALIZATION = 0
  • BrickletNFC`READERUSTATEUIDLE = 128
  • BrickletNFC`READERUSTATEUERROR = 192
  • BrickletNFC`READERUSTATEUREQUESTUTAGUID = 2
  • BrickletNFC`READERUSTATEUREQUESTUTAGUIDUREADY = 130
  • BrickletNFC`READERUSTATEUREQUESTUTAGUIDUERROR = 194
  • BrickletNFC`READERUSTATEUAUTHENTICATEUMIFAREUCLASSICUPAGE = 3
  • BrickletNFC`READERUSTATEUAUTHENTICATEUMIFAREUCLASSICUPAGEUREADY = 131
  • BrickletNFC`READERUSTATEUAUTHENTICATEUMIFAREUCLASSICUPAGEUERROR = 195
  • BrickletNFC`READERUSTATEUWRITEUPAGE = 4
  • BrickletNFC`READERUSTATEUWRITEUPAGEUREADY = 132
  • BrickletNFC`READERUSTATEUWRITEUPAGEUERROR = 196
  • BrickletNFC`READERUSTATEUREQUESTUPAGE = 5
  • BrickletNFC`READERUSTATEUREQUESTUPAGEUREADY = 133
  • BrickletNFC`READERUSTATEUREQUESTUPAGEUERROR = 197
  • BrickletNFC`READERUSTATEUWRITEUNDEF = 6
  • BrickletNFC`READERUSTATEUWRITEUNDEFUREADY = 134
  • BrickletNFC`READERUSTATEUWRITEUNDEFUERROR = 198
  • BrickletNFC`READERUSTATEUREQUESTUNDEF = 7
  • BrickletNFC`READERUSTATEUREQUESTUNDEFUREADY = 135
  • BrickletNFC`READERUSTATEUREQUESTUNDEFUERROR = 199
event BrickletNFC@CardemuStateChangedCallback[sender, state, idle]
Callback Parameters:
  • sender – Type: NETObject[BrickletNFC]
  • state – Type: Integer, Range: See constants
  • idle – Type: True/False

This callback is called if the cardemu state of the NFC Bricklet changes. See CardemuGetState[] for more information about the possible states.

The following constants are available for this function:

For state:

  • BrickletNFC`CARDEMUUSTATEUINITIALIZATION = 0
  • BrickletNFC`CARDEMUUSTATEUIDLE = 128
  • BrickletNFC`CARDEMUUSTATEUERROR = 192
  • BrickletNFC`CARDEMUUSTATEUDISCOVER = 2
  • BrickletNFC`CARDEMUUSTATEUDISCOVERUREADY = 130
  • BrickletNFC`CARDEMUUSTATEUDISCOVERUERROR = 194
  • BrickletNFC`CARDEMUUSTATEUTRANSFERUNDEF = 3
  • BrickletNFC`CARDEMUUSTATEUTRANSFERUNDEFUREADY = 131
  • BrickletNFC`CARDEMUUSTATEUTRANSFERUNDEFUERROR = 195
event BrickletNFC@P2PStateChangedCallback[sender, state, idle]
Callback Parameters:
  • sender – Type: NETObject[BrickletNFC]
  • state – Type: Integer, Range: See constants
  • idle – Type: True/False

This callback is called if the P2P state of the NFC Bricklet changes. See P2PGetState[] for more information about the possible states.

The following constants are available for this function:

For state:

  • BrickletNFC`P2PUSTATEUINITIALIZATION = 0
  • BrickletNFC`P2PUSTATEUIDLE = 128
  • BrickletNFC`P2PUSTATEUERROR = 192
  • BrickletNFC`P2PUSTATEUDISCOVER = 2
  • BrickletNFC`P2PUSTATEUDISCOVERUREADY = 130
  • BrickletNFC`P2PUSTATEUDISCOVERUERROR = 194
  • BrickletNFC`P2PUSTATEUTRANSFERUNDEF = 3
  • BrickletNFC`P2PUSTATEUTRANSFERUNDEFUREADY = 131
  • BrickletNFC`P2PUSTATEUTRANSFERUNDEFUERROR = 195

Virtual Functions

Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.

BrickletNFC@GetAPIVersion[] → {apiVersion1, apiVersion2, apiVersion3}
Output Parameters:
  • apiVersioni – Type: Integer
    • 1: major – Type: Integer, Range: [0 to 255]
    • 2: minor – Type: Integer, Range: [0 to 255]
    • 3: revision – Type: Integer, Range: [0 to 255]

Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.

BrickletNFC@GetResponseExpected[functionId] → responseExpected
Parameters:
  • functionId – Type: Integer, Range: See constants
Returns:
  • responseExpected – Type: True/False

Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.

For getter functions this is enabled by default and cannot be disabled, because those functions will always send a response. For callback configuration functions it is enabled by default too, but can be disabled by SetResponseExpected[]. For setter functions it is disabled by default and can be enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For functionId:

  • BrickletNFC`FUNCTIONUSETUMODE = 1
  • BrickletNFC`FUNCTIONUREADERUREQUESTUTAGUID = 3
  • BrickletNFC`FUNCTIONUREADERUWRITEUNDEF = 6
  • BrickletNFC`FUNCTIONUREADERUREQUESTUNDEF = 7
  • BrickletNFC`FUNCTIONUREADERUAUTHENTICATEUMIFAREUCLASSICUPAGE = 9
  • BrickletNFC`FUNCTIONUREADERUWRITEUPAGE = 10
  • BrickletNFC`FUNCTIONUREADERUREQUESTUPAGE = 11
  • BrickletNFC`FUNCTIONUCARDEMUUSTARTUDISCOVERY = 15
  • BrickletNFC`FUNCTIONUCARDEMUUWRITEUNDEF = 16
  • BrickletNFC`FUNCTIONUCARDEMUUSTARTUTRANSFER = 17
  • BrickletNFC`FUNCTIONUP2PUSTARTUDISCOVERY = 20
  • BrickletNFC`FUNCTIONUP2PUWRITEUNDEF = 21
  • BrickletNFC`FUNCTIONUP2PUSTARTUTRANSFER = 22
  • BrickletNFC`FUNCTIONUSETUDETECTIONULEDUCONFIG = 25
  • BrickletNFC`FUNCTIONUSETUMAXIMUMUTIMEOUT = 27
  • BrickletNFC`FUNCTIONUCARDEMUUSETUTAGUID = 30
  • BrickletNFC`FUNCTIONUSETUWRITEUFIRMWAREUPOINTER = 237
  • BrickletNFC`FUNCTIONUSETUSTATUSULEDUCONFIG = 239
  • BrickletNFC`FUNCTIONURESET = 243
  • BrickletNFC`FUNCTIONUWRITEUUID = 248
BrickletNFC@SetResponseExpected[functionId, responseExpected] → Null
Parameters:
  • functionId – Type: Integer, Range: See constants
  • responseExpected – Type: True/False

Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For functionId:

  • BrickletNFC`FUNCTIONUSETUMODE = 1
  • BrickletNFC`FUNCTIONUREADERUREQUESTUTAGUID = 3
  • BrickletNFC`FUNCTIONUREADERUWRITEUNDEF = 6
  • BrickletNFC`FUNCTIONUREADERUREQUESTUNDEF = 7
  • BrickletNFC`FUNCTIONUREADERUAUTHENTICATEUMIFAREUCLASSICUPAGE = 9
  • BrickletNFC`FUNCTIONUREADERUWRITEUPAGE = 10
  • BrickletNFC`FUNCTIONUREADERUREQUESTUPAGE = 11
  • BrickletNFC`FUNCTIONUCARDEMUUSTARTUDISCOVERY = 15
  • BrickletNFC`FUNCTIONUCARDEMUUWRITEUNDEF = 16
  • BrickletNFC`FUNCTIONUCARDEMUUSTARTUTRANSFER = 17
  • BrickletNFC`FUNCTIONUP2PUSTARTUDISCOVERY = 20
  • BrickletNFC`FUNCTIONUP2PUWRITEUNDEF = 21
  • BrickletNFC`FUNCTIONUP2PUSTARTUTRANSFER = 22
  • BrickletNFC`FUNCTIONUSETUDETECTIONULEDUCONFIG = 25
  • BrickletNFC`FUNCTIONUSETUMAXIMUMUTIMEOUT = 27
  • BrickletNFC`FUNCTIONUCARDEMUUSETUTAGUID = 30
  • BrickletNFC`FUNCTIONUSETUWRITEUFIRMWAREUPOINTER = 237
  • BrickletNFC`FUNCTIONUSETUSTATUSULEDUCONFIG = 239
  • BrickletNFC`FUNCTIONURESET = 243
  • BrickletNFC`FUNCTIONUWRITEUUID = 248
BrickletNFC@SetResponseExpectedAll[responseExpected] → Null
Parameters:
  • responseExpected – Type: True/False

Changes the response expected flag for all setter and callback configuration functions of this device at once.

Internal Functions

Internal functions are used for maintenance tasks such as flashing a new firmware of changing the UID of a Bricklet. These task should be performed using Brick Viewer instead of using the internal functions directly.

BrickletNFC@SetBootloaderMode[mode] → status
Parameters:
  • mode – Type: Integer, Range: See constants
Returns:
  • status – Type: Integer, Range: See constants

Sets the bootloader mode and returns the status after the requested mode change was instigated.

You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

The following constants are available for this function:

For mode:

  • BrickletNFC`BOOTLOADERUMODEUBOOTLOADER = 0
  • BrickletNFC`BOOTLOADERUMODEUFIRMWARE = 1
  • BrickletNFC`BOOTLOADERUMODEUBOOTLOADERUWAITUFORUREBOOT = 2
  • BrickletNFC`BOOTLOADERUMODEUFIRMWAREUWAITUFORUREBOOT = 3
  • BrickletNFC`BOOTLOADERUMODEUFIRMWAREUWAITUFORUERASEUANDUREBOOT = 4

For status:

  • BrickletNFC`BOOTLOADERUSTATUSUOK = 0
  • BrickletNFC`BOOTLOADERUSTATUSUINVALIDUMODE = 1
  • BrickletNFC`BOOTLOADERUSTATUSUNOUCHANGE = 2
  • BrickletNFC`BOOTLOADERUSTATUSUENTRYUFUNCTIONUNOTUPRESENT = 3
  • BrickletNFC`BOOTLOADERUSTATUSUDEVICEUIDENTIFIERUINCORRECT = 4
  • BrickletNFC`BOOTLOADERUSTATUSUCRCUMISMATCH = 5
BrickletNFC@GetBootloaderMode[] → mode
Returns:
  • mode – Type: Integer, Range: See constants

Returns the current bootloader mode, see SetBootloaderMode[].

The following constants are available for this function:

For mode:

  • BrickletNFC`BOOTLOADERUMODEUBOOTLOADER = 0
  • BrickletNFC`BOOTLOADERUMODEUFIRMWARE = 1
  • BrickletNFC`BOOTLOADERUMODEUBOOTLOADERUWAITUFORUREBOOT = 2
  • BrickletNFC`BOOTLOADERUMODEUFIRMWAREUWAITUFORUREBOOT = 3
  • BrickletNFC`BOOTLOADERUMODEUFIRMWAREUWAITUFORUERASEUANDUREBOOT = 4
BrickletNFC@SetWriteFirmwarePointer[pointer] → Null
Parameters:
  • pointer – Type: Integer, Unit: 1 B, Range: [0 to 232 - 1]

Sets the firmware pointer for WriteFirmware[]. The pointer has to be increased by chunks of size 64. The data is written to flash every 4 chunks (which equals to one page of size 256).

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

BrickletNFC@WriteFirmware[{data1, data2, ..., data64}] → status
Parameters:
  • datai – Type: Integer, Range: [0 to 255]
Returns:
  • status – Type: Integer, Range: [0 to 255]

Writes 64 Bytes of firmware at the position as written by SetWriteFirmwarePointer[] before. The firmware is written to flash every 4 chunks.

You can only write firmware in bootloader mode.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

BrickletNFC@WriteUID[uid] → Null
Parameters:
  • uid – Type: Integer, Range: [0 to 232 - 1]

Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.

We recommend that you use Brick Viewer to change the UID.

BrickletNFC@ReadUID[] → uid
Returns:
  • uid – Type: Integer, Range: [0 to 232 - 1]

Returns the current UID as an integer. Encode as Base58 to get the usual string version.

Constants

BrickletNFC`DEVICEUIDENTIFIER

This constant is used to identify a NFC Bricklet.

The GetIdentity[] function and the IPConnection@EnumerateCallback callback of the IP Connection have a deviceIdentifier parameter to specify the Brick's or Bricklet's type.

BrickletNFC`DEVICEDISPLAYNAME

This constant represents the human readable name of a NFC Bricklet.