Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the chances of multiple Modbus APUs per TCP frame #564

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
/** @type {net.Socket?} - Optional custom socket */
this._externalSocket = null;

if(typeof ip === "object") {
if (typeof ip === "object") {
options = ip;
ip = undefined;
}
Expand All @@ -62,7 +62,7 @@
}

/** @type {net.TcpSocketConnectOpts} - Options for net.connect(). */
this.connectOptions = {
this.connectOptions = {
// Default options
...{
host: ip || options.ip,
Expand All @@ -72,8 +72,8 @@
...options
};

if(options.socket) {
if(options.socket instanceof net.Socket) {
if (options.socket) {
if (options.socket instanceof net.Socket) {
this._externalSocket = options.socket;
this.openFlag = this._externalSocket.readyState === "opening" || this._externalSocket.readyState === "open";
} else {
Expand All @@ -92,6 +92,7 @@

// init a socket
this._client = this._externalSocket || new net.Socket(this.socketOpts);
this._writeCompleted = Promise.resolve();

if (options.timeout) this._client.setTimeout(options.timeout);

Expand Down Expand Up @@ -132,6 +133,7 @@
this._client.on("connect", function() {
self.openFlag = true;
modbusSerialDebug("TCP port: signal connect");
self._client.setNoDelay();
handleCallback();
});

Expand Down Expand Up @@ -174,10 +176,10 @@
* @param {(err?: Error) => void} callback
*/
open(callback) {
if(this._externalSocket === null) {
if (this._externalSocket === null) {
this.callback = callback;
this._client.connect(this.connectOptions);
} else if(this.openFlag) {
} else if (this.openFlag) {
modbusSerialDebug("TCP port: external socket is opened");
callback(); // go ahead to setup existing socket
} else {
Expand Down Expand Up @@ -214,7 +216,7 @@
* @param {Buffer} data
*/
write(data) {
if(data.length < MIN_DATA_LENGTH) {
if (data.length < MIN_DATA_LENGTH) {
modbusSerialDebug("expected length of data is to small - minimum is " + MIN_DATA_LENGTH);
return;
}
Expand All @@ -240,7 +242,26 @@
});

// send buffer to slave
this._client.write(buffer);
let previousWritePromise = this._writeCompleted;
let newWritePromise = new Promise((resolveNewWrite, rejectNewWrite) => {

Check failure on line 246 in ports/tcpport.js

View workflow job for this annotation

GitHub Actions / test (18.x)

'newWritePromise' is never reassigned. Use 'const' instead.

Check failure on line 246 in ports/tcpport.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'newWritePromise' is never reassigned. Use 'const' instead.
// Wait for the completion of any write that happened before.
previousWritePromise.finally(() => {
try {
// The previous write succeeded, write the new buffer.
if (this._client.write(buffer)) {
// Mark this write as complete.
resolveNewWrite();
} else {
// Wait for one `drain` event to mark this write as complete.
this._client.once("drain", resolveNewWrite);
yaacov marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (error) {
rejectNewWrite(error);
}
});
});
// Overwrite `_writeCompleted` so that the next call to `TcpPort.write` will have to wait on our write to complete.
this._writeCompleted = newWritePromise;

// set next transaction id
this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS;
Expand Down
7 changes: 6 additions & 1 deletion test/mocks/netMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
}
}

setNoDelay(noDelay) {
return this;
}

end() {
this.emit("close", false);
}

write(data) {
this._data = data;
return true;
}

receive(buffer) {
Expand Down Expand Up @@ -55,6 +60,6 @@

exports.Server = Server;

exports.createServer = function(options, connectionListener) {
exports.createServer = function (options, connectionListener) {

Check failure on line 63 in test/mocks/netMock.js

View workflow job for this annotation

GitHub Actions / test (18.x)

Unexpected space before function parentheses.

Check failure on line 63 in test/mocks/netMock.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Unexpected space before function parentheses.
return new Server(options, connectionListener);
};
Loading