Permalink
Browse files

Don't call `cleanup()` on ModbusResponseStack until after `writeRespo…

…nse()` or `writeException()` has been called.
  • Loading branch information...
1 parent 2e81229 commit 002e60f0d2e0bd5732018a346b0ce3fa38413401 @TooTallNate committed Jan 1, 2011
Showing with 8 additions and 5 deletions.
  1. +8 −5 lib/modbus-stack.js
View
@@ -187,7 +187,6 @@ ModbusResponseStack.prototype._onData = function(chunk) {
//console.log('bufferlist.length: ' + this.bufferlist.length);
this._gotRequest = true;
this.emit('request', this.request);
- this.cleanup();
}
}
@@ -200,29 +199,33 @@ ModbusResponseStack.prototype.writeResponse = function() {
throw new Error('"RESPONSES['+this.functionCode+']" in "server.js" is not implemented!');
}
var pdu = server.RESPONSES[this.functionCode].apply(this, arguments);
- return Put()
+ var rtn = this.stream.write(Put()
.word16be(this.request.transactionId)
.word16be(this.request.protocolVersion)
.word16be(pdu.length+2)
.word8(this.request.unitIdentifier)
.word8(this.functionCode)
.put(pdu)
- .write(this.stream);
+ .buffer());
+ this.cleanup();
+ return rtn;
}
// Sends back an exception response after receiving a request.
ModbusResponseStack.prototype.writeException = function(exceptionCode) {
if (!this._gotRequest) {
throw new Error('Can\'t call "writeException" until after the "request" event');
}
- return Put()
+ var rtn = this.stream.write(Put()
.word16be(this.request.transactionId)
.word16be(this.request.protocolVersion)
.word16be(3)
.word8(this.request.unitIdentifier)
.word8(this.functionCode | EXCEPTION_BIT)
.word8(exceptionCode)
- .write(this.stream);
+ .buffer());
+ this.cleanup();
+ return rtn;
}

0 comments on commit 002e60f

Please sign in to comment.