-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
"use strict"; | ||
|
||
// create an empty modbus client | ||
//let ModbusRTU = require("modbus-serial"); | ||
// let ModbusRTU = require("modbus-serial"); | ||
const ModbusRTU = require("../index"); | ||
const client = new ModbusRTU(); | ||
|
||
const abortController = new AbortController(); | ||
const { signal } = abortController; | ||
signal.addEventListener("abort", () => { | ||
console.log("Abort signal received by the abort controller"); | ||
console.log("Abort signal received by the abort controller"); | ||
}); | ||
|
||
async function connect() { | ||
await client.connectTCP("127.0.0.1", { | ||
port: 8502, | ||
socketOpts: { | ||
signal: signal, | ||
}, | ||
}); | ||
client.setID(1); | ||
client.setTimeout(2000); | ||
await client.connectTCP("127.0.0.1", { | ||
port: 8502, | ||
socketOpts: { | ||
signal: signal | ||
} | ||
}); | ||
client.setID(1); | ||
client.setTimeout(2000); | ||
} | ||
|
||
async function readRegisters() { | ||
const data = await client.readHoldingRegisters(5, 4); | ||
console.log("Received:", data.data); | ||
const data = await client.readHoldingRegisters(5, 4); | ||
console.log("Received:", data.data); | ||
} | ||
|
||
async function runner() { | ||
await connect(); | ||
await connect(); | ||
|
||
setTimeout(() => { | ||
abortController.abort("Aborting request"); | ||
}, 1000); | ||
setTimeout(() => { | ||
abortController.abort("Aborting request"); | ||
}, 1000); | ||
|
||
await readRegisters(); | ||
await readRegisters(); | ||
} | ||
|
||
runner() | ||
.then(() => { | ||
if (signal.aborted) { | ||
if (signal.reason) { | ||
console.log(`Request aborted with reason: ${signal.reason}`); | ||
} else { | ||
console.log("Request aborted but no reason was given."); | ||
} | ||
} else { | ||
console.log("Request not aborted"); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
}) | ||
.finally(async () => { | ||
console.log("Close client"); | ||
await client.close(); | ||
}); | ||
.then(() => { | ||
if (signal.aborted) { | ||
if (signal.reason) { | ||
console.log(`Request aborted with reason: ${signal.reason}`); | ||
} else { | ||
console.log("Request aborted but no reason was given."); | ||
} | ||
} else { | ||
console.log("Request not aborted"); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
}) | ||
.finally(async() => { | ||
console.log("Close client"); | ||
await client.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters