Skip to content

Commit

Permalink
Merge pull request #111 from biancode/master
Browse files Browse the repository at this point in the history
better docs first step
  • Loading branch information
yaacov authored Jun 3, 2017
2 parents dfb3d4a + c99145d commit d006a58
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 72 deletions.
5 changes: 5 additions & 0 deletions apis/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,9 @@ var addConnctionAPI = function(Modbus) {
};
};

/**
* Connection API Modbus.
*
* @type {addConnctionAPI}
*/
module.exports = addConnctionAPI;
26 changes: 16 additions & 10 deletions apis/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
*
* @param {Function} f the function to convert
* @return a function that calls function "f" and return a promise.
* @private
*/
var convert = function(f) {
var _convert = function(f) {
var converted = function(address, arg, next) {
var client = this;
var id = this._unitID;
Expand Down Expand Up @@ -55,7 +56,7 @@ var convert = function(f) {
};

/**
* Adds promise API to a Modbus objext
* Adds promise API to a Modbus object.
*
* @param {ModbusRTU} Modbus the ModbusRTU object.
*/
Expand All @@ -72,14 +73,19 @@ var addPromiseAPI = function(Modbus) {
cl.getTimeout = function() {return this._timeout;};

// convert functions to return promises
cl.readCoils = convert(cl.writeFC1);
cl.readDiscreteInputs = convert(cl.writeFC2);
cl.readHoldingRegisters = convert(cl.writeFC3);
cl.readInputRegisters = convert(cl.writeFC4);
cl.writeCoil = convert(cl.writeFC5);
cl.writeRegister = convert(cl.writeFC6);
cl.writeCoils = convert(cl.writeFC15);
cl.writeRegisters = convert(cl.writeFC16);
cl.readCoils = _convert(cl.writeFC1);
cl.readDiscreteInputs = _convert(cl.writeFC2);
cl.readHoldingRegisters = _convert(cl.writeFC3);
cl.readInputRegisters = _convert(cl.writeFC4);
cl.writeCoil = _convert(cl.writeFC5);
cl.writeRegister = _convert(cl.writeFC6);
cl.writeCoils = _convert(cl.writeFC15);
cl.writeRegisters = _convert(cl.writeFC16);
};

/**
* Promise API Modbus library.
*
* @type {addPromiseAPI}
*/
module.exports = addPromiseAPI;
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ function _startTimeout(duration, next) {
}

/**
* Cancel the given timeout
* Cancel the given timeout.
*
* @param {number} timeoutHandle The handle of the timeout
* @private
*/
Expand Down
57 changes: 44 additions & 13 deletions ports/asciiport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ var MIN_DATA_LENGTH = 6;
*
* @param {Buffer} buf the data buffer to encode.
* @return {Buffer} the ascii encoded buffer
* @private
*/
function asciiEncodeRequestBuffer(buf) {
function _asciiEncodeRequestBuffer(buf) {

// replace the 2 byte crc16 with a single byte lrc
buf.writeUInt8(calculateLrc(buf.slice(0, -2)), buf.length - 2);
Expand All @@ -44,8 +45,9 @@ function asciiEncodeRequestBuffer(buf) {
*
* @param {Buffer} bufAscii the ascii data buffer to decode.
* @return {Buffer} the decoded buffer, or null if decode error
* @private
*/
function asciiDecodeResponseBuffer(bufAscii) {
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);
Expand Down Expand Up @@ -78,8 +80,9 @@ function asciiDecodeResponseBuffer(bufAscii) {
* @param {AsciiPort} modbus
* @param {Buffer} buf the buffer to check.
* @return {boolean} if the buffer can be an answer
* @private
*/
function checkData(modbus, buf) {
function _checkData(modbus, buf) {
// check buffer size
if (buf.length !== modbus._length && buf.length !== 5) {
modbusSerialDebug({ action: "length error", recive: buf.length, expected: modbus._length });
Expand All @@ -93,7 +96,11 @@ function checkData(modbus, buf) {
}

/**
* Simulate a modbus-ascii port using serial connection
* Simulate a modbus-ascii port using serial connection.
*
* @param path
* @param options
* @constructor
*/
var AsciiPort = function(path, options) {
var modbus = this;
Expand Down Expand Up @@ -143,12 +150,12 @@ var AsciiPort = function(path, options) {
}

// we have what looks like a complete ascii encoded response message, so decode
var _data = asciiDecodeResponseBuffer(modbus._buffer);
var _data = _asciiDecodeResponseBuffer(modbus._buffer);
modbusSerialDebug({ action: "got EOM", data: _data, buffer: modbus._buffer });
if(_data !== null) {

// check if this is the data we are waiting for
if (checkData(modbus, _data)) {
if (_checkData(modbus, _data)) {
modbusSerialDebug({ action: "emit data serial ascii port", data: data, buffer: modbus._buffer });
modbusSerialDebug(JSON.stringify({ action: "emit data serial ascii port strings", data: data, buffer: modbus._buffer }));
// emit a data signal
Expand All @@ -167,28 +174,36 @@ var AsciiPort = function(path, options) {
util.inherits(AsciiPort, EventEmitter);

/**
* Simulate successful port open
* Simulate successful port open.
*
* @param callback
*/
AsciiPort.prototype.open = function(callback) {
this._client.open(callback);
};

/**
* Simulate successful close port
* Simulate successful close port.
*
* @param callback
*/
AsciiPort.prototype.close = function(callback) {
this._client.close(callback);
};

/**
* Check if port is open
* Check if port is open.
*
* @returns {boolean}
*/
AsciiPort.prototype.isOpen = function() {
return this._client.isOpen();
};

/**
* Send data to a modbus slave
* Send data to a modbus slave.
*
* @param data
*/
AsciiPort.prototype.write = function(data) {
if(data.length < MIN_DATA_LENGTH) {
Expand Down Expand Up @@ -228,13 +243,29 @@ AsciiPort.prototype.write = function(data) {
}

// ascii encode buffer
var _encodedData = asciiEncodeRequestBuffer(data);
var _encodedData = _asciiEncodeRequestBuffer(data);

// send buffer to slave
this._client.write(_encodedData);

modbusSerialDebug({ action: "send serial ascii port", data: _encodedData });
modbusSerialDebug(JSON.stringify({ action: "send serial ascii port", data: _encodedData }));
modbusSerialDebug({
action: "send serial ascii port",
data: _encodedData,
unitid: this._id,
functionCode: this._cmd
});

modbusSerialDebug(JSON.stringify({
action: "send serial ascii port",
data: _encodedData,
unitid: this._id,
functionCode: this._cmd
}));
};

/**
* ASCII port for Modbus.
*
* @type {AsciiPort}
*/
module.exports = AsciiPort;
54 changes: 42 additions & 12 deletions ports/c701port.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ var MIN_DATA_LENGTH = 6;
var C701_PORT = 0x7002;

/**
* check if a buffer chunk can be a modbus answer
* or modbus exception
* Check if a buffer chunk can be a Modbus answer or modbus exception.
*
* @param {UdpPort} modbus
* @param {Buffer} buf the buffer to check.
* @return {boolean} if the buffer can be an answer
* @private
*/
function checkData(modbus, buf) {
function _checkData(modbus, buf) {
// check buffer size
if (buf.length !== modbus._length && buf.length !== 5) return false;

Expand All @@ -34,7 +34,11 @@ function checkData(modbus, buf) {
}

/**
* Simulate a modbus-RTU port using C701 UDP-to-Serial bridge
* Simulate a modbus-RTU port using C701 UDP-to-Serial bridge.
*
* @param ip
* @param options
* @constructor
*/
var UdpPort = function(ip, options) {
var modbus = this;
Expand Down Expand Up @@ -69,7 +73,7 @@ var UdpPort = function(ip, options) {
modbusSerialDebug(JSON.stringify({ action: "receive c701 upd port strings", data: data, buffer: buffer }));

// check the serial data
if (checkData(modbus, buffer)) {
if (_checkData(modbus, buffer)) {
modbusSerialDebug({ action: "emit data serial rtu buffered port", buffer: buffer });
modbusSerialDebug(JSON.stringify({ action: "emit data serial rtu buffered port strings", buffer: buffer }));

Expand All @@ -80,7 +84,7 @@ var UdpPort = function(ip, options) {
buffer = data.slice(data.length - 5);

// check the serial data
if (checkData(modbus, buffer)) {
if (_checkData(modbus, buffer)) {
modbusSerialDebug({ action: "emit data serial rtu buffered port", buffer: buffer });
modbusSerialDebug(JSON.stringify({
action: "emit data serial rtu buffered port strings",
Expand All @@ -105,15 +109,19 @@ var UdpPort = function(ip, options) {
util.inherits(UdpPort, EventEmitter);

/**
* Simulate successful port open
* Simulate successful port open.
*
* @param callback
*/
UdpPort.prototype.open = function(callback) {
if (callback)
callback(null);
};

/**
* Simulate successful close port
* Simulate successful close port.
*
* @param callback
*/
UdpPort.prototype.close = function(callback) {
this._client.close();
Expand All @@ -122,14 +130,18 @@ UdpPort.prototype.close = function(callback) {
};

/**
* Check if port is open
* Check if port is open.
*
* @returns {boolean}
*/
UdpPort.prototype.isOpen = function() {
return this.openFlag;
};

/**
* Send data to a modbus-tcp slave
* Send data to a modbus-tcp slave.
*
* @param data
*/
UdpPort.prototype.write = function(data) {
if(data.length < MIN_DATA_LENGTH) {
Expand Down Expand Up @@ -182,8 +194,26 @@ UdpPort.prototype.write = function(data) {
// send buffer to C701 UDP to serial bridge
this._client.send(buffer, 0, buffer.length, this.port, this.ip);

modbusSerialDebug({ action: "send c701 upd port", data: data, buffer: buffer });
modbusSerialDebug(JSON.stringify({ action: "send c701 upd port strings", data: data, buffer: buffer }));
modbusSerialDebug({
action: "send c701 upd port",
data: data,
buffer: buffer,
unitid: this._id,
functionCode: this._cmd
});

modbusSerialDebug(JSON.stringify({
action: "send c701 upd port strings",
data: data,
buffer: buffer,
unitid: this._id,
functionCode: this._cmd
}));
};

/**
* UDP port for Modbus.
*
* @type {UdpPort}
*/
module.exports = UdpPort;
Loading

0 comments on commit d006a58

Please sign in to comment.