Serial communication with the target application¶
This guide will show you how communicate to the target application with serial protocols.
Thanks to the API, you communicate with RF technologies (BLE, Wi-Fi, NFC) with the TapNPass device. The TapNPass device will forward data to the Target Application using the proper configured protocol (RS232, RS485, USB, ...).
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. In IoTize Studio (manual), in configuration explorer set option IoTized Application / Target / Type of Target
to System (TapNPass)
.
You can now pick 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 with options in IoTized Application / Target / Link target <=> tap / Serial Configuration
. It's optional as we will see how to change this values programmatically.
Do not forget to apply 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;
We will now use the TargetService
instance (regroups functionnalities to communicate with the target application) in the next examples.
[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 be Modbus, serial standard or serial transparent.
If not, you dit not properly 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 an @[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, you should 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 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 the response let response2: Response<Uint8Array> = (await) targetService.send("Hello World".getBytes()); if (!response2.isSuccessfull()){ throw new DeviceResponseError(response2); } console.log(`Succesfull send `); } catch (err) { console.error(`Response error: ${err}`); }
// Send "Hello World" and wait for 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 the response Response<Void> response3 = targetService .send("Hello World".getBytes()) .execute(); if (response3.isSuccessful()){ // OK }
Read target data¶
try{ // Send "Hello World" and wait for 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(); }