Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Fix S7 Read not working (#34)
Browse files Browse the repository at this point in the history
S7 driver does not work. `readAllItems()` returns `<Promise>` instead of
returning actual values.

This PR forces reads and writes to use `await` as per
`https://github.com/st-one-io/nodes7`. Tested on local connection with
S7 PLC.
  • Loading branch information
grigals authored Nov 23, 2023
2 parents 0433cf6 + 4060ced commit 043b681
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/devices/S7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ export class S7Connection extends DeviceConnection {
* @param {array} metrics Array of metric objects to read to
* @returns {array} Old metric values (for RbE checking)
*/
readMetrics(metrics: Metrics, payloadFormat?: string,) {
async readMetrics(metrics: Metrics, payloadFormat?: string,) {
const changedMetrics: sparkplugMetric[] = [];
// Tell S7 to update metric values
let newVals = this.#itemGroup.readAllItems(); // name: value
let newVals = await this.#itemGroup.readAllItems(); // name: value
this.emit('data', newVals, false);
}

/**
* Writes metric values to the PLC
* @param {array} metrics Array of metric objects to write to the PLC
*/
writeMetrics(metrics: Metrics) {
async writeMetrics(metrics: Metrics) {
// This doesn't seem to work for Ixxx value writes
// Untested with other writes at present

Expand All @@ -116,22 +116,22 @@ export class S7Connection extends DeviceConnection {
// Notify user
log(`Writing ${values} to ${addrs}`);
// Write metric values
this.#itemGroup.writeItems(addrs, values);
await this.#itemGroup.writeItems(addrs, values);
}
}

/**
* Close connection and tidy up
*/
close() {
async close() {
// Clear the variable list
this.#vars = {};
// Destroy the metric item group, if it exists
if (this.#itemGroup) {
this.#itemGroup.destroy();
}
// Close the PLC connection
this.#s7Conn.disconnect();
await this.#s7Conn.disconnect();
}
}

Expand Down

0 comments on commit 043b681

Please sign in to comment.