Introduction:
This post demonstrates how to use the driver.setData function within a JS object to write data to a device address.
Warning
JS objects provide powerful customization features, but using them incorrectly can result in system error or performance degradation. Please use JS objects carefully.
Software Version:
The JS Object is exclusive to cMT-X series HMIs.
EasyBuilder Pro 6.05.02.370+
Related Tutorials:
Video - Introduction to Weintek’s JS Object (JavaScript)
JS Action/Object SDK Documentation
Demo:
Instructions:
This project will contain:
- Two Numeric objects (referred to as A and B) that monitor different device addresses.
- A JS object that writes the data from Numeric A to Numeric B’s address whenever there is a change in Numeric A’s address data (simulating data transfer-like behavior).
Numeric Object Settings
- Set the Read/Write address of Numeric A to LW-100 and the Device data format to 16-bit Unsigned.
- Set the Read/Write address of Numeric B to LW-101 and the Device data format to 16-bit Unsigned.
JS Object Settings
The JS object can be found within the “Object” tab:
- In the “Config” tab, Select “New Value” and add a “Subscription” named “sourceAddress”, pointing to LW-100, with the data type set to 16-bit Unsigned and a length of 1.
- Add another value. This time select the “Address” type and name it “destAddress”, pointing to LW-101, with the data type set to 16-bit Unsigned.
When finished, your JS object should look like:
Source Code
Select the “Source Code” tab and enter the following code:
// Use Subscription to monitor data changes at Numeric A's address
this.config.sourceAddress.onResponse((err, data) => {
if (err) {
console.log('Error:', err.message);
} else {
// Use driver.setData to write data to Numeric B's address
driver.setData(this.config.destAddress, data.values[0], (err, data) => {
if (err) {
console.log('driver.setData failed, error:', err.message);
} else {
console.log('driver.setData succeeded');
}
});
}
});
When finished, Click “OK” and place the JS object within your project.
Execution Result
- Run a simulation and change the value of LW-100 using the Numeric object A.
- Observe that the value of LW-101 within Numeric object B is also updated accordingly.
Code Explanation
driver.setData (function)
- Used to write data to a specified device address.
Response Callback Function (The 3rd parameter)
- Writing data may take time, so a callback function is used to receive the device response without blocking the program.
- After sending a data request, the program continues executing subsequent code until the system notifies of “successful data writing” or “an error occurs” through the callback function.
- If you don’t need to know the outcome of driver.setData, this parameter can be omitted.
The code below replaces driver.setData with driver.promises.setData. This method uses JavaScript’s Promise object, which allows for handling asynchronous operations more intuitively. The benefit of using Promise is that the code becomes easier to read and maintain, especially when dealing with multiple asynchronous operations.
this.config.sourceAddress.onResponse(async (err, data) => {
if (err) {
console.log('Error:', err.message);
} else {
try {
await driver.promises.setData(this.config.destAddress, data.values[0]);
} catch (error) {
console.log('Error:', error.message);
}
}
});