-
-
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.
Allow to pass socket options to TCP client (#519)
* add jsdocs * allow to pass more opts to socket * allow to pass AbortSignal to socket * add abort signal example * lint the code * remove vscode/ from gitignore * drop node v14 (reached EOL April 2023)
- Loading branch information
Showing
9 changed files
with
206 additions
and
36 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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
// Use IntelliSense to learn about possible Node.js debug attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug Current File", | ||
"type": "node", | ||
"request": "launch", | ||
"program": "${file}" | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Mocha single test", | ||
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js", | ||
// current file | ||
"args": ["${file}"], | ||
"console": "internalConsole", | ||
"cwd": "${workspaceRoot}", | ||
"runtimeVersion": "16", | ||
} | ||
] | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use strict"; | ||
|
||
// create an empty modbus client | ||
// 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"); | ||
}); | ||
|
||
async function connect() { | ||
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); | ||
} | ||
|
||
async function runner() { | ||
await connect(); | ||
|
||
setTimeout(() => { | ||
abortController.abort("Aborting request"); | ||
}, 1000); | ||
|
||
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(); | ||
}); |
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
Oops, something went wrong.