driver

driver

The driver namespace provides classes and functions for users to read/write data from/to device addresses.

Classes

Address
Subscription

Namespaces

promises

Methods

(static) getData(address, length, callback)

Read data from device tags.

Examples
// Read two bits (for one bit, replace 2 with 1)
driver.getData(this.config.a_bit_addr, 2, (err, data) => {
    if (err) {
        console.log('Error:', err.message);
    } else {
        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);
    }
});
// 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
driver.getData(this.config.an_u16_addr, 2, (err, data) => {
    if (err) {
        console.log('Error:', err.message);
    } else {
        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);
    }
});
// 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();
driver.getData(this.config.an_u16_addr, 3, (err, data) => {
    if (err) {
        console.log('Error:', err.message);
    } else {
        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);
    }
});
// 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.

callback driver~ResponseCallback

The response callback.

(static) setData(address, data, callbackopt)

Write data to device tags.

Examples
// Write two bits
var data = [false, true]; // [0, 1] also works here
driver.setData(this.config.a_bit_addr, data, (err) => {
    if (err) {
        console.log('Error:', err.message);
    } else {
        console.log('Write succeeded');
    }
});
// Expected result:
// [11:55:36] Write succeeded 
// Write two unsigned 16-bit integers
var data = [10, 11];
driver.setData(this.config.an_u16_addr, data, (err) => {
    if (err) {
        console.log('Error:', err.message);
    } else {
        console.log('Write succeeded');
    }
});
// Expected result:
// [11:55:50] Write succeeded 
Parameters:
Name Type Attributes 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.

callback driver~ResponseCallback <optional>

The response callback.

(static) setStringData(address, length, data, callbackopt)

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.
driver.setStringData(this.config.a_string_addr, 3, rand, (err) => {
    if (err) {
        console.log('Error:', err.message);
    }
});
Parameters:
Name Type Attributes 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.

callback driver~ResponseCallback <optional>

The response callback.

Type Definitions

ResponseCallback(err, data)

The response callback will be invoked on the following circumstances:

  1. The read-data operation is complete. (e.g. driver.getData(...))
  2. The data read from monitored device tags changed. (e.g. driver.subscription.onResponse(...))
  3. The read-data operation failed.

The arguments passed into the response callback:

Circumstances err data
The data changed undefined The response data
The read operation failed Error undefined
Parameters:
Name Type Description
err Error
data driver~ResponseData

ResponseData

Properties:
Name Type Description
timestamp Number

The number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

buffer ArrayBuffer

The raw/binary data.

values Array.<Number>

The array of numbers converted from the raw data according to data type of the tag(s).

Response data is an object containing the information about the data of some device tags at a specific moment.

We usually receive response data under the following circumstances:

  1. driver.getData(address, length, callback)

    The second parameter (usually named ‘data’) passed into the callback function if read operation succeeds.

  2. driver.promises.getData(address, length)

    The value which the returned Promise object will be resolved with if read operation succeeds.

  3. driver.subscription.onResponse(callback)

    The second parameter (usually named ‘data’) passed into the callback function if data of the device tags which the Subscription object monitors changes.

Type:
  • Object