Skip to content

Serial communication with the target application

This guide details how communicate with a target application using serial protocols.

The TapNPass device communicates with RF technology (BLE, Wi-Fi, NFC) via the API. The TapNPass device forwards data to the Target Application using the correctly configured protocol (RS-232, RS-485, USB, ...).

General architecture

Prerequisities

What you need

  • TapNPass
  • Target Application that handles serial communications
  • Mobile Device with RF capabilities
  • IoTize Studio

Configure your Tap Device

Your Tap must be configured as a TapNPass type of target. To do this, in IoTize Studio (manual) configuration explorer, set option IoTized Application / Target / Type of Target to System (TapNPass).

Choose the default target protocol to use with option IoTized Application / Target / Link target <=> tap / Target Protocol:

  • Modbus
  • Serial standard
  • Serial transparant

[Optional] You can configure serial settings in IoTized Application / Target / Link target <=> tap / Serial Configuration, but these values can also be programmed.

Do not forget to Apply the configuration.

You are able to connect to your Tap Device

If you don't know how to connect to your Tap Device, read Getting Started.

import { TargetService } from '@iotize/device-client.js/device/impl/service/target-service'
import { IoTizeDevice } from '@iotize/device-client.js/device/impl/iotize-device'

let device: IoTizeDevice = // ...;
let targetService: TargetService = device.service.target; 
IoTizeDevice device = IoTizeDevice.fromProtocol(
    yourComProtocol
);
device.connect();
TargetService targetService = device.service.target;

The next examples use the TargetService instance to regroup functionalities and communicate with the target application.

[Optional] Check device configuration programmatically

You can check device configuration programatically with @reference:TargetService::getProtocol()

let protocol = (await targetService.getProtocol()).body();
console.log(`Target prototocol is ${protocol}`);
TargetProtocol protocol = targetService.getProtocol().get();
Log.i(TAG, "Target protocol is " + protocol.toString());

Target protocol value should be either Modbus, serial standard or serial transparent.

If not, you incorrectly configured your Tap Device.

Serial configuration

Read serial configuration

To read the serial configuration, use @reference:TargetService::getUARTSettings().

let settings: UARTSettings = ((await) targetService.getUARTSettings()).body();
UARTSettings uartSettings = targetService.getUARTSettings().get();

This will give you a @[reference:UARTSettings].

Edit serial configuration

To edit serial configuration:

// Create your UART configuration
let uartSettings = new UartSettings();
uartSettings
    .setBaudRate(9600)
    .setBitParity(UARTSettings.BitParity.ODD)
    .setDataBitsLength(8)
    .setPhysicalPort(UARTSettings.PhysicalPort.RS232)
    // ...
    ;
// write the configuration on the device 
await targetService.setUARTSettings(uartSettings);

// To apply the new configuration, reconnect to the target
await targetService.connect();
// Create your UART configuration
UARTSettings uartSettings = new UartSettings();
uartSettings
    .setBaudRate(9600)
    .setBitParity(UartSettings.BitParityEnum.NONE)
    .setDataBitsLength(8)
    .setTimeout(200)
    .setStopBit(UartSettings.StopBitEnum.ONE)
    .setHandshake(UartSettings.HandshakeEnum.CTS)
    .setHandshakeDelimiter(UartSettings.HandshakeDelimiterEnum.NONE)
    .setPhysicalPort(UartSettings.PhysicalPortEnum.USB)
    ;
// write the configuration on the device 
targetService.setUARTSettings(uartSettings).execute();

// To apply the new configuration, reconnect to the target
Response<Void> response = targetService.connect().execute();
if (!response.isSuccessful()){
    throw new DeviceResponseError(response);
}

Note:

  • When you reset the tap, the configuration will be lost
  • Configuration will not be applied until you call targetService.connect()
  • For more information about these parameters see this link

If you want to write only a subset of the configuration, read the configuration first and update the value.

// Read config first
let uartSettings = (await device.service.target.getUARTSettings()).body();

// Change only a subset of options
uartSettings.setBaudRate(187500);

// Write the new configuration
await device.service.target.setUARTSettings(uartSettings);
await device.service.target.connect();
// Read config first
UartSettings uartSettings = device.service.target.getUARTSettings().execute().body();

// Change only a subset of options
uartSettings.setBaudRate(187500);

// Write the new configuration
Response lastResponse;
lastResponse = targetService.setUARTSettings(uartSettings).execute();
if (!lastResponse.isSuccessful()){
    throw new DeviceResponseError(lastResponse);
}
lastResponse = targetService.connect().execute();
if (!lastResponse.isSuccessful()){
    throw new DeviceResponseError(lastResponse);
}

Serial communication

Send data to target

try{
    // Send "Hello World" and wait for a response
    let response1: Response<Uint8Array> = (await) targetService.sendReceive("Hello World".getBytes());
    let targetResponse: Uint8Array = response1.body();
    console.log(`Target application responded: `, targetResponse);

    // Send "Hello World" and do not wait for a response
    let response2: Response<Uint8Array> = (await) targetService.send("Hello World".getBytes());
    if (!response2.isSuccessful()){
        throw new DeviceResponseError(response2);
    }
    console.log(`Successful send `);
}
catch (err) {
    console.error(`Response error: ${err}`);
}
// Send "Hello World" and wait for a response
Response<byte[]> response1 = targetService
        .sendReceive("Hello World".getBytes())
        .execute();
if (response1.isSuccessful()){
    byte[] data = response1.body();
}

// Send "Hello World" and do not wait for a response
Response<Void> response3 = targetService
        .send("Hello World".getBytes())
        .execute();
if (response3.isSuccessful()){
    // OK
}

Read target data

try{
    // Send "Hello World" and wait for a response
    let response1: Response<Uint8Array> = (await) targetService.read();
    let targetResponse: Uint8Array = response1.body();
    console.log(`Target application responded: `, targetResponse);
}
catch (err) {
    console.error(`Response error: ${err}`);
}
// Read data
Response<byte[]> response2 = targetService
        .read()
        .execute();
if (response2.isSuccessful()){
    byte[] data = response2.body();
}