promises

driver. promises

The driver.promises API provides an alternative set of asynchronous driver methods that return Promise objects rather than using callbacks.

Methods

(static) getData(address, length) → {Promise}

Read data from device tags.

Examples
// Read two bits (for one bit, replace 2 with 1)
try {
    var data = await driver.promises.getData(this.config.a_bit_addr, 2);
    console.log("timestamp:", data.timestamp);
    console.log("buffer.byteLength:", data.buffer.byteLength);
    console.log("buffer as Uint8Array:", new Uint8Array(data.buffer));
    console.log("values:", data.values);
} catch(err) {
    console.log('Error:', err.message);
}
// Expected result:
// [11:23:24] timestamp: 1653362604687 
// [11:23:24] buffer.byteLength: 2 
// [11:23:24] buffer as Uint8Array: { '0': 0, '1': 0 } 
// [11:23:24] values: [ 0, 0 ] 
// Read two unsigned 16-bit integers
try {
    driver.promises.getData(this.config.an_u16_addr, 2);
    console.log("timestamp:", data.timestamp);
    console.log("buffer.byteLength:", data.buffer.byteLength);
    console.log("buffer as Uint16Array:", new Uint16Array(data.buffer));
    console.log("values:", data.values);
} catch (err) {
    console.log('Error:', err.message);
}
// Expected result:
// [11:30:26] timestamp: 1653363026921 
// [11:30:26] buffer.byteLength: 4 
// [11:30:26] buffer as Uint16Array: { '0': 121, '1': 232 } 
// [11:30:26] values: [ 121, 232 ] 
// Read an UTF-8 string
var utf8decoder = new TextDecoder();
try {
    driver.promises.getData(this.config.an_u16_addr, 3);
    console.log("timestamp:", data.timestamp);
    console.log("buffer.byteLength:", data.buffer.byteLength);
    var bufferAsUint8Array = new Uint8Array(data.buffer);
    console.log("string:", utf8decoder.decode(bufferAsUint8Array));
    console.log("values:", data.values);
} catch (err) {
    console.log('Error:', err.message);
}
// Expected result:
// [13:47:12] timestamp: 1653371232533 
// [13:47:12] buffer.byteLength: 6 
// [13:47:12] string: 7747 
// [13:47:12] values: [ 14135, 14132, 0 ] 
Parameters:
Name Type Description
address driver.Address

The address of the first device tag to read.

length Number

Number of device tags to read.

Returns:

A Promise object. Following successful read, the Promise is resolved with the response data.

Type
Promise

(static) setData(address, data) → {Promise}

Write data to device tags.

Examples
// Write two bits
var data = [false, true]; // [0, 1] also works here
try {
    await driver.promises.setData(this.config.a_bit_addr, data);
    console.log('Write succeeded');
} catch(err) {
    console.log('Error:', err.message);
}
// Expected result:
// [11:55:36] Write succeeded 
// Write two unsigned 16-bit integers
var data = [10, 11];
try {
    await driver.promises.setData(this.config.an_u16_addr, data);
    console.log('Write succeeded');
} catch(err) {
    console.log('Error:', err.message);
}
// Expected result:
// [11:55:50] Write succeeded 
Parameters:
Name Type Description
address driver.Address

The address of the first device tag to write.

data Number | Array.<Number> | ArrayBuffer

The data to write to the device tags.

Returns:

A Promise object. Following successful write, the Promise is resolved with undefined.

Type
Promise

(static) setStringData(address, length, data) → {Promise}

Write string data to device tags.

This function will calculate the required data length, in bytes, to fill the device tags from [address] to [address + length - 1]. If data length, in bytes, of data parameter:

  • Is less than the required data length ⇨ append zero.
  • Is greater than required data length ⇨ drop the data exceeding the required data length.
Example
// Write a random 0-99999 number as string
var rand = Math.round(Math.random() * 100000).toString(); // "0" to "99999"
// Max. length of rand is 5 bytes, which occupies three 16-bit words.
// Assuming a_string_addr is an unsigned 16-bit integer address,
// then length should be 3 here.
try {
    driver.promises.setStringData(this.config.a_string_addr, 3, rand);
} catch (err) {
    console.log('Error:', err.message);
}
Parameters:
Name Type Description
address driver.Address

The address of the first device tag to write.

length Number

Number of device tags to write.

data String

The data to write to the device tags.

Returns:

A Promise object. Following successful write, the Promise is resolved with undefined.

Type
Promise