Skip to content

Commit

Permalink
- Removed deprecated Promise.defer from WriteMultipleCoils handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpoeter committed Nov 30, 2016
1 parent cad4e7b commit 078f554
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions src/handler/client/WriteMultipleCoils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,50 @@ module.exports = stampit()
}.bind(this)

this.writeMultipleCoils = function (startAddress, coils, N) {
var defer = Promise.defer()
var fc = 15
var basePdu = Buffer.allocUnsafe(6)
var pdu

basePdu.writeUInt8(fc, 0)
basePdu.writeUInt16BE(startAddress, 1)

if (coils instanceof Buffer) {
basePdu.writeUInt16BE(N, 3)
basePdu.writeUInt8(coils.length, 5)
pdu = Buffer.concat([basePdu, coils])
} else if (coils instanceof Array) {
if (coils.length > 1968) {
defer.reject()
return
}
return new Promise(function (resolve, reject) {
var fc = 15
var basePdu = Buffer.allocUnsafe(6)
var pdu

basePdu.writeUInt8(fc, 0)
basePdu.writeUInt16BE(startAddress, 1)

if (coils instanceof Buffer) {
basePdu.writeUInt16BE(N, 3)
basePdu.writeUInt8(coils.length, 5)
pdu = Buffer.concat([basePdu, coils])
} else if (coils instanceof Array) {
if (coils.length > 1968) {
reject()
return
}

var byteCount = Math.ceil(coils.length / 8)
var curByte = 0
var curByteIdx = 0
var cntr = 0
var payloadPdu = Buffer.allocUnsafe(byteCount)
var byteCount = Math.ceil(coils.length / 8)
var curByte = 0
var curByteIdx = 0
var cntr = 0
var payloadPdu = Buffer.allocUnsafe(byteCount)

basePdu.writeUInt16BE(coils.length, 3)
basePdu.writeUInt8(byteCount, 5)
basePdu.writeUInt16BE(coils.length, 3)
basePdu.writeUInt8(byteCount, 5)

for (var i = 0; i < coils.length; i += 1) {
curByte += coils[i] ? Math.pow(2, cntr) : 0
for (var i = 0; i < coils.length; i += 1) {
curByte += coils[i] ? Math.pow(2, cntr) : 0

cntr = (cntr + 1) % 8
cntr = (cntr + 1) % 8

if (cntr === 0 || i === coils.length - 1) {
payloadPdu.writeUInt8(curByte, curByteIdx)
curByteIdx = curByteIdx + 1
curByte = 0
if (cntr === 0 || i === coils.length - 1) {
payloadPdu.writeUInt8(curByte, curByteIdx)
curByteIdx = curByteIdx + 1
curByte = 0
}
}
}

pdu = Buffer.concat([basePdu, payloadPdu])
}

this.queueRequest(fc, pdu, defer)
pdu = Buffer.concat([basePdu, payloadPdu])
}

return defer.promise
this.queueRequest(fc, pdu, { resolve: resolve, reject: reject })
}.bind(this))
}

init()
Expand Down

0 comments on commit 078f554

Please sign in to comment.