Skip to content

Commit

Permalink
Merge pull request #205 from yaacov/remove-new-buffer
Browse files Browse the repository at this point in the history
Replace new Buffer with Buffer.alloc
  • Loading branch information
yaacov authored May 23, 2018
2 parents 8b18b16 + 25eced3 commit 65bb62b
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 265 deletions.
2 changes: 1 addition & 1 deletion examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var client = new ModbusRTU();
var networkErrors = ["ESOCKETTIMEDOUT", "ETIMEDOUT", "ECONNRESET", "ECONNREFUSED", "EHOSTUNREACH"];

// open connection to a serial port
client.connectRTU("/dev/ttyUSB2")
client.connectRTU("/dev/ttyUSB0", { hupcl: false, dsrdtr: false })
//client.connectTCP("modbus.local", { port: 502 })
.then(setClient)
.then(function() {
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code
};

var codeLength = 6;
var buf = new Buffer(codeLength + 2); // add 2 crc bytes
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes

buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
Expand Down Expand Up @@ -492,7 +492,7 @@ ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code
};

var codeLength = 6;
var buf = new Buffer(codeLength + 2); // add 2 crc bytes
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes

buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
Expand Down Expand Up @@ -538,7 +538,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
};

var codeLength = 6;
var buf = new Buffer(codeLength + 2); // add 2 crc bytes
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes

buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
Expand Down Expand Up @@ -589,7 +589,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
};

var codeLength = 6; // 1B deviceAddress + 1B functionCode + 2B dataAddress + 2B value
var buf = new Buffer(codeLength + 2); // add 2 crc bytes
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes

buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
Expand Down Expand Up @@ -638,7 +638,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {

var dataBytes = Math.ceil(array.length / 8);
var codeLength = 7 + dataBytes;
var buf = new Buffer(codeLength + 2); // add 2 crc bytes
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes

buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
Expand Down Expand Up @@ -698,7 +698,7 @@ ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) {
};

var codeLength = 7 + 2 * array.length;
var buf = new Buffer(codeLength + 2); // add 2 crc bytes
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes

buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
Expand Down
10 changes: 5 additions & 5 deletions ports/asciiport.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function _asciiEncodeRequestBuffer(buf) {
buf.writeUInt8(calculateLrc(buf.slice(0, -2)), buf.length - 2);

// create a new buffer of the correct size
var bufAscii = new Buffer(buf.length * 2 + 1); // 1 byte start delimit + x2 data as ascii encoded + 2 lrc + 2 end delimit
var bufAscii = Buffer.alloc(buf.length * 2 + 1); // 1 byte start delimit + x2 data as ascii encoded + 2 lrc + 2 end delimit

// create the ascii payload

Expand All @@ -50,7 +50,7 @@ function _asciiEncodeRequestBuffer(buf) {
function _asciiDecodeResponseBuffer(bufAscii) {

// create a new buffer of the correct size (based on ascii encoded buffer length)
var bufDecoded = new Buffer((bufAscii.length - 1) / 2);
var bufDecoded = Buffer.alloc((bufAscii.length - 1) / 2);

// decode into new buffer (removing delimiters at start and end)
for (var i = 0; i < (bufAscii.length - 3) / 2; i++) {
Expand Down Expand Up @@ -112,7 +112,7 @@ var AsciiPort = function(path, options) {
options.autoOpen = false;

// internal buffer
this._buffer = new Buffer(0);
this._buffer = Buffer.from("");
this._id = 0;
this._cmd = 0;
this._length = 0;
Expand All @@ -133,7 +133,7 @@ var AsciiPort = function(path, options) {
var sdIndex = modbus._buffer.indexOf(0x3A); // ascii for ':'
if(sdIndex === -1) {
// if not there, reset the buffer and return
modbus._buffer = new Buffer(0);
modbus._buffer = Buffer.from("");
return;
}
// if there is data before the start delimiter, remove it
Expand Down Expand Up @@ -163,7 +163,7 @@ var AsciiPort = function(path, options) {
}
}
// reset the buffer now its been used
modbus._buffer = new Buffer(0);
modbus._buffer = Buffer.from("");
} else {
// otherwise just wait for more data to arrive
}
Expand Down
2 changes: 1 addition & 1 deletion ports/c701port.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ UdpPort.prototype.write = function(data) {
}

// build C701 header
var buffer = new Buffer(data.length + 116);
var buffer = Buffer.alloc(data.length + 116);
buffer.fill(0);
buffer.writeUInt16LE(600, 2); // C701 magic for serial bridge
buffer.writeUInt16LE(0, 36); // C701 RS485 connector (0..2)
Expand Down
2 changes: 1 addition & 1 deletion ports/rtubufferedport.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var RTUBufferedPort = function(path, options) {
options.autoOpen = false;

// internal buffer
this._buffer = new Buffer(0);
this._buffer = Buffer.alloc(0);
this._id = 0;
this._cmd = 0;
this._length = 0;
Expand Down
4 changes: 2 additions & 2 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var TcpPort = function(ip, options) {
length = data.readUInt16BE(4);

// cut 6 bytes of mbap and copy pdu
buffer = new Buffer(length + CRC_LENGTH);
buffer = Buffer.alloc(length + CRC_LENGTH);
data.copy(buffer, 0, MIN_MBAP_LENGTH);

// add crc to message
Expand Down Expand Up @@ -165,7 +165,7 @@ TcpPort.prototype.write = function(data) {
this._cmd = data[1];

// remove crc and add mbap
var buffer = new Buffer(data.length + MIN_MBAP_LENGTH - CRC_LENGTH);
var buffer = Buffer.alloc(data.length + MIN_MBAP_LENGTH - CRC_LENGTH);
buffer.writeUInt16BE(this._transactionIdWrite, 0);
buffer.writeUInt16BE(0, 2);
buffer.writeUInt16BE(data.length - CRC_LENGTH, 4);
Expand Down
4 changes: 2 additions & 2 deletions ports/tcprtubufferedport.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var TcpRTUBufferedPort = function(ip, options) {
modbus.port = options.port || MODBUS_PORT;

// internal buffer
modbus._buffer = new Buffer(0);
modbus._buffer = Buffer.alloc(0);

// handle callback - call a callback function only once, for the first event
// it will triger
Expand Down Expand Up @@ -225,7 +225,7 @@ TcpRTUBufferedPort.prototype.write = function(data) {
}

// remove crc and add mbap
var buffer = new Buffer(data.length + MIN_MBAP_LENGTH - CRC_LENGTH);
var buffer = Buffer.alloc(data.length + MIN_MBAP_LENGTH - CRC_LENGTH);
buffer.writeUInt16BE(this._transactionIdWrite, 0);
buffer.writeUInt16BE(0, 2);
buffer.writeUInt16BE(data.length - CRC_LENGTH, 4);
Expand Down
2 changes: 1 addition & 1 deletion ports/telnetport.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var TelnetPort = function(ip, options) {
this.port = options.port || TELNET_PORT; // telnet server port

// internal buffer
this._buffer = new Buffer(0);
this._buffer = Buffer.alloc(0);
this._id = 0;
this._cmd = 0;
this._length = 0;
Expand Down
14 changes: 7 additions & 7 deletions ports/testport.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(3 + parseInt((length - 1) / 8 + 1) + 2);
buffer = Buffer.alloc(3 + parseInt((length - 1) / 8 + 1) + 2);
buffer.writeUInt8(parseInt((length - 1) / 8 + 1), 2);

// read coils
Expand All @@ -123,7 +123,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(3 + length * 2 + 2);
buffer = Buffer.alloc(3 + length * 2 + 2);
buffer.writeUInt8(length * 2, 2);

// read registers
Expand All @@ -143,7 +143,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(3 + length * 2 + 2);
buffer = Buffer.alloc(3 + length * 2 + 2);
buffer.writeUInt8(length * 2, 2);

// read registers
Expand All @@ -163,7 +163,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(8);
buffer = Buffer.alloc(8);
buffer.writeUInt16BE(address, 2);
buffer.writeUInt16BE(state, 4);

Expand All @@ -185,7 +185,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(8);
buffer = Buffer.alloc(8);
buffer.writeUInt16BE(address, 2);
buffer.writeUInt16BE(value, 4);

Expand All @@ -203,7 +203,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(8);
buffer = Buffer.alloc(8);
buffer.writeUInt16BE(address, 2);
buffer.writeUInt16BE(length, 4);

Expand All @@ -230,7 +230,7 @@ TestPort.prototype.write = function(data) {
}

// build answer
buffer = new Buffer(8);
buffer = Buffer.alloc(8);
buffer.writeUInt16BE(address, 2);
buffer.writeUInt16BE(length, 4);

Expand Down
Loading

0 comments on commit 65bb62b

Please sign in to comment.