From c6241121cbb272e4db8a00627d26caaeddad49c4 Mon Sep 17 00:00:00 2001 From: LWi Date: Mon, 8 Aug 2022 09:28:24 +0200 Subject: [PATCH 1/2] fixed timeout issue for big sdo block transfers --- source/device.js | 3 ++- source/protocol/sdo.js | 3 ++- source/protocol/sdo_client.js | 17 +++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/source/device.js b/source/device.js index 6d81915..c0b22ec 100644 --- a/source/device.js +++ b/source/device.js @@ -132,13 +132,14 @@ class Device extends EventEmitter { * @param {number} message.id - CAN message identifier. * @param {Buffer} message.data - CAN message data; * @param {number} message.len - CAN message length in bytes. + * @return {number} number of bytes sent or -1 for error * @protected */ send(message) { if(this._send === null) throw ReferenceError("please call setTransmitFunction() first"); - this._send(message); + return this._send(message); } /** diff --git a/source/protocol/sdo.js b/source/protocol/sdo.js index 5beca8e..70ecd2e 100644 --- a/source/protocol/sdo.js +++ b/source/protocol/sdo.js @@ -311,9 +311,10 @@ class SdoTransfer { * Send a data buffer. * * @param {Buffer} data - data to send. + * @return {number} Number of bits sent or -1 for error */ send(data) { - this.device.send({ + return this.device.send({ id: this.cobId, data: data, }); diff --git a/source/protocol/sdo_client.js b/source/protocol/sdo_client.js index 055b599..a56e166 100644 --- a/source/protocol/sdo_client.js +++ b/source/protocol/sdo_client.js @@ -618,6 +618,11 @@ class SdoClient { transfer.refresh(); } + /** + * Minimum timeout for the sdo block download. + */ + blockDownloadTimeout = 1 + /** * Download a data block. * @@ -641,6 +646,10 @@ class SdoClient { return; } + if(this.blockDownloadTimeout > 1){ + this.blockDownloadTimeout = this.blockDownloadTimeout >> 1; + } + const sendBuffer = Buffer.alloc(8); const offset = 7 * (transfer.blockSequence + (transfer.blockCount * transfer.blockSize)); @@ -652,7 +661,11 @@ class SdoClient { } transfer.data.copy(sendBuffer, 1, offset, offset + 7); - transfer.send(sendBuffer); + const ret = transfer.send(sendBuffer); + if(ret < 0 ){ + this.blockDownloadTimeout = this.blockDownloadTimeout << 8; + transfer.blockSequence -= 1 + } transfer.refresh(); if(transfer.blockFinished @@ -660,7 +673,7 @@ class SdoClient { clearInterval(transfer.blockInterval); transfer.blockInterval = null; } - }); + }, this.blockDownloadTimeout); } /** From 4f2f4e24a3bf1a9e4d237bbfc9559fa855fb6cd8 Mon Sep 17 00:00:00 2001 From: LWi Date: Mon, 8 Aug 2022 07:59:48 +0000 Subject: [PATCH 2/2] updated linter fix --- source/device.js | 2 +- source/protocol/sdo.js | 2 +- source/protocol/sdo_client.js | 15 ++++++--------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/source/device.js b/source/device.js index c0b22ec..4524f71 100644 --- a/source/device.js +++ b/source/device.js @@ -132,7 +132,7 @@ class Device extends EventEmitter { * @param {number} message.id - CAN message identifier. * @param {Buffer} message.data - CAN message data; * @param {number} message.len - CAN message length in bytes. - * @return {number} number of bytes sent or -1 for error + * @returns {number} number of bytes sent or -1 for error * @protected */ send(message) { diff --git a/source/protocol/sdo.js b/source/protocol/sdo.js index 70ecd2e..e2fccf5 100644 --- a/source/protocol/sdo.js +++ b/source/protocol/sdo.js @@ -311,7 +311,7 @@ class SdoTransfer { * Send a data buffer. * * @param {Buffer} data - data to send. - * @return {number} Number of bits sent or -1 for error + * @returns {number} Number of bits sent or -1 for error */ send(data) { return this.device.send({ diff --git a/source/protocol/sdo_client.js b/source/protocol/sdo_client.js index a56e166..f8996a7 100644 --- a/source/protocol/sdo_client.js +++ b/source/protocol/sdo_client.js @@ -96,6 +96,8 @@ class SdoClient { this.servers = {}; this.transfers = {}; this._blockSize = 127; + // Minimum timeout for the sdo block download. + this._blockDownloadTimeout = 1; } /** @@ -618,11 +620,6 @@ class SdoClient { transfer.refresh(); } - /** - * Minimum timeout for the sdo block download. - */ - blockDownloadTimeout = 1 - /** * Download a data block. * @@ -646,8 +643,8 @@ class SdoClient { return; } - if(this.blockDownloadTimeout > 1){ - this.blockDownloadTimeout = this.blockDownloadTimeout >> 1; + if(this._blockDownloadTimeout > 1){ + this._blockDownloadTimeout = this._blockDownloadTimeout >> 1; } const sendBuffer = Buffer.alloc(8); @@ -663,7 +660,7 @@ class SdoClient { transfer.data.copy(sendBuffer, 1, offset, offset + 7); const ret = transfer.send(sendBuffer); if(ret < 0 ){ - this.blockDownloadTimeout = this.blockDownloadTimeout << 8; + this._blockDownloadTimeout = this._blockDownloadTimeout << 8; transfer.blockSequence -= 1 } transfer.refresh(); @@ -673,7 +670,7 @@ class SdoClient { clearInterval(transfer.blockInterval); transfer.blockInterval = null; } - }, this.blockDownloadTimeout); + }, this._blockDownloadTimeout); } /**