UNPKG

simple-modbus

Version:

A simple library for working with Modbus with Typescript bindings.

453 lines 20.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var typed_event_1 = require("./util/typed-event"); var modbus_errors_1 = require("./error/modbus-errors"); var ModbusFunctionCode; (function (ModbusFunctionCode) { ModbusFunctionCode[ModbusFunctionCode["READ_COIL_STATUS"] = 1] = "READ_COIL_STATUS"; ModbusFunctionCode[ModbusFunctionCode["READ_INPUT_STATUS"] = 2] = "READ_INPUT_STATUS"; ModbusFunctionCode[ModbusFunctionCode["READ_HOLDING_REGISTERS"] = 3] = "READ_HOLDING_REGISTERS"; ModbusFunctionCode[ModbusFunctionCode["READ_INPUT_REGISTERS"] = 4] = "READ_INPUT_REGISTERS"; ModbusFunctionCode[ModbusFunctionCode["FORCE_SINGLE_COIL"] = 5] = "FORCE_SINGLE_COIL"; ModbusFunctionCode[ModbusFunctionCode["PRESET_SINGLE_REGISTER"] = 6] = "PRESET_SINGLE_REGISTER"; ModbusFunctionCode[ModbusFunctionCode["FORCE_MULTIPLE_COILS"] = 15] = "FORCE_MULTIPLE_COILS"; ModbusFunctionCode[ModbusFunctionCode["PRESET_MULTIPLE_REGISTERS"] = 16] = "PRESET_MULTIPLE_REGISTERS"; })(ModbusFunctionCode = exports.ModbusFunctionCode || (exports.ModbusFunctionCode = {})); var ModbusCommandException; (function (ModbusCommandException) { ModbusCommandException[ModbusCommandException["ILLEGAL_FUNCTION"] = 1] = "ILLEGAL_FUNCTION"; ModbusCommandException[ModbusCommandException["ILLEGAL_DATA_ADDRESS"] = 2] = "ILLEGAL_DATA_ADDRESS"; ModbusCommandException[ModbusCommandException["ILLEGAL_DATA_VALUE"] = 3] = "ILLEGAL_DATA_VALUE"; ModbusCommandException[ModbusCommandException["SERVER_DEVICE_FAILURE"] = 4] = "SERVER_DEVICE_FAILURE"; ModbusCommandException[ModbusCommandException["ACKNOWLEDGE"] = 5] = "ACKNOWLEDGE"; ModbusCommandException[ModbusCommandException["SERVER_DEVICE_BUSY"] = 6] = "SERVER_DEVICE_BUSY"; ModbusCommandException[ModbusCommandException["NEGATIVE_ACKNOWLEDGE"] = 7] = "NEGATIVE_ACKNOWLEDGE"; ModbusCommandException[ModbusCommandException["MEMORY_PARITY_ERROR"] = 8] = "MEMORY_PARITY_ERROR"; ModbusCommandException[ModbusCommandException["GATEWAY_PATH_UNAVAILABLE"] = 10] = "GATEWAY_PATH_UNAVAILABLE"; ModbusCommandException[ModbusCommandException["GATEWAY_TARGET_FAILED_TO_RESPOND"] = 11] = "GATEWAY_TARGET_FAILED_TO_RESPOND"; })(ModbusCommandException = exports.ModbusCommandException || (exports.ModbusCommandException = {})); var CoilStatus; (function (CoilStatus) { CoilStatus[CoilStatus["ON"] = 0] = "ON"; CoilStatus[CoilStatus["OFF"] = 1] = "OFF"; })(CoilStatus = exports.CoilStatus || (exports.CoilStatus = {})); var ModbusCommand = /** @class */ (function () { function ModbusCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) { /** * Fires on either success or failure, with the response bytes. Mainly used by the server to send a response. */ this.onComplete = new typed_event_1.TypedEvent(); /** * Fires on a call of the success method. */ this.onSuccess = new typed_event_1.TypedEvent(); /** * Fires on a call of the fail method. */ this.onFailure = new typed_event_1.TypedEvent(); this._rawPacket = rawPacket; this._unitIdGetter = unitIdGetter; this._functionCodeGetter = functionCodeGetter; this._successGetter = successGetter; this._failureGetter = failureGetter; } Object.defineProperty(ModbusCommand.prototype, "unitId", { /** * If RTU, unitId is equivalent to slaveId */ get: function () { return this._unitIdGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ModbusCommand.prototype, "functionCode", { /** * Modbus function code */ get: function () { return this._functionCodeGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ModbusCommand.prototype, "responsePacket", { /** * This function will give you the response packet bytes that will be sent on the emitting server. Before calling this function, the `success` or `fail` method must be called in order to set the response. * * @returns A buffer of the bytes representing the response to the server * @throws ModbusCommandError if success or fail hasn't been called yet */ get: function () { if (!this._responsePacket) { throw new modbus_errors_1.ModbusCommandError('Tried to read response packet, but success or fail has not been called.', this._rawPacket); } return this._responsePacket; }, enumerable: true, configurable: true }); /** * Set a failure on this command to return an exception response to the emitting server. * * @param exception - The reason for the failure */ ModbusCommand.prototype.fail = function (exception) { this._responsePacket = this._failureGetter(this._rawPacket, exception); this.onComplete.emit(this); this.onFailure.emit(this); }; return ModbusCommand; }()); exports.ModbusCommand = ModbusCommand; var ReadCoilStatusCommand = /** @class */ (function (_super) { __extends(ReadCoilStatusCommand, _super); /** * @hidden */ function ReadCoilStatusCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, coilAddressGetter, coilLengthGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._coilAddressGetter = coilAddressGetter; _this._coilLengthGetter = coilLengthGetter; return _this; } Object.defineProperty(ReadCoilStatusCommand.prototype, "coilStartAddress", { get: function () { return this._coilAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ReadCoilStatusCommand.prototype, "numberOfCoils", { get: function () { return this._coilLengthGetter(this._rawPacket); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. * * @param data - Boolean coil data, starting at `coilStartAddress`, of length `numberOfCoils`. */ ReadCoilStatusCommand.prototype.success = function (data) { // TODO: Throw error here if data length doesn't equal requested length this._responsePacket = this._successGetter(this._rawPacket, data); this.onComplete.emit(this); this.onSuccess.emit(this); }; return ReadCoilStatusCommand; }(ModbusCommand)); exports.ReadCoilStatusCommand = ReadCoilStatusCommand; var ReadInputStatusCommand = /** @class */ (function (_super) { __extends(ReadInputStatusCommand, _super); /** * @hidden */ function ReadInputStatusCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, inputAddressGetter, inputLengthGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._inputAddressGetter = inputAddressGetter; _this._inputLengthGetter = inputLengthGetter; return _this; } Object.defineProperty(ReadInputStatusCommand.prototype, "inputStartAddress", { get: function () { return this._inputAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ReadInputStatusCommand.prototype, "numberOfInputs", { get: function () { return this._inputLengthGetter(this._rawPacket); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. * * @param data - Input status data of requested discrete inputs. `true` = ON, `false` = off */ ReadInputStatusCommand.prototype.success = function (data) { // TODO: Throw error here if data length doesn't equal requested length this._responsePacket = this._successGetter(this._rawPacket, data); this.onComplete.emit(this); this.onSuccess.emit(this); }; return ReadInputStatusCommand; }(ModbusCommand)); exports.ReadInputStatusCommand = ReadInputStatusCommand; var ReadHoldingRegistersCommand = /** @class */ (function (_super) { __extends(ReadHoldingRegistersCommand, _super); /** * @hidden */ function ReadHoldingRegistersCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, registerAddressGetter, registerLengthGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._registerAddressGetter = registerAddressGetter; _this._registerLengthGetter = registerLengthGetter; return _this; } Object.defineProperty(ReadHoldingRegistersCommand.prototype, "registerStartAddress", { get: function () { return this._registerAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ReadHoldingRegistersCommand.prototype, "registerLength", { get: function () { return this._registerLengthGetter(this._rawPacket); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. * * @param data - Array of values of the requested holding registers. Register values are 16 bits. Array length must equal `registerLength`. `data[0]` should be the value of the register at `registerStartAddress`. */ ReadHoldingRegistersCommand.prototype.success = function (data) { // TODO: Throw error here if data length doesn't equal requested length this._responsePacket = this._successGetter(this._rawPacket, data); this.onComplete.emit(this); this.onSuccess.emit(this); }; return ReadHoldingRegistersCommand; }(ModbusCommand)); exports.ReadHoldingRegistersCommand = ReadHoldingRegistersCommand; var ReadInputRegistersCommand = /** @class */ (function (_super) { __extends(ReadInputRegistersCommand, _super); /** * @hidden */ function ReadInputRegistersCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, registerAddressGetter, registerLengthGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._registerAddressGetter = registerAddressGetter; _this._registerLengthGetter = registerLengthGetter; return _this; } Object.defineProperty(ReadInputRegistersCommand.prototype, "registerStartAddress", { get: function () { return this._registerAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ReadInputRegistersCommand.prototype, "registerLength", { get: function () { return this._registerLengthGetter(this._rawPacket); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. * * @param data - Array of values of the requested input registers. Register values are 16 bits. Array length must equal `registerLength`. `data[0]` should be the value of the register at `registerStartAddress`. */ ReadInputRegistersCommand.prototype.success = function (data) { // TODO: Throw error here if data length doesn't equal requested length this._responsePacket = this._successGetter(this._rawPacket, data); this.onComplete.emit(this); this.onSuccess.emit(this); }; return ReadInputRegistersCommand; }(ModbusCommand)); exports.ReadInputRegistersCommand = ReadInputRegistersCommand; var ForceSingleCoilCommand = /** @class */ (function (_super) { __extends(ForceSingleCoilCommand, _super); /** * @hidden */ function ForceSingleCoilCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, coilAddressGetter, coilStatusGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._coilAddressGetter = coilAddressGetter; _this._coilStatusGetter = coilStatusGetter; return _this; } Object.defineProperty(ForceSingleCoilCommand.prototype, "coilAddress", { get: function () { return this._coilAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ForceSingleCoilCommand.prototype, "coilStatus", { get: function () { return this._coilStatusGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ForceSingleCoilCommand.prototype, "coilStatusAsCoilStatus", { get: function () { return this.coilStatus === true ? CoilStatus.ON : CoilStatus.OFF; }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. */ ForceSingleCoilCommand.prototype.success = function () { this._responsePacket = this._successGetter(this._rawPacket); this.onComplete.emit(this); this.onSuccess.emit(this); }; return ForceSingleCoilCommand; }(ModbusCommand)); exports.ForceSingleCoilCommand = ForceSingleCoilCommand; var PresetSingleRegisterCommand = /** @class */ (function (_super) { __extends(PresetSingleRegisterCommand, _super); /** * @hidden */ function PresetSingleRegisterCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, registerAddressGetter, registerValueGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._registerAddressGetter = registerAddressGetter; _this._registerValueGetter = registerValueGetter; return _this; } Object.defineProperty(PresetSingleRegisterCommand.prototype, "registerAddress", { get: function () { return this._registerAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(PresetSingleRegisterCommand.prototype, "registerValue", { get: function () { return this._registerValueGetter(this._rawPacket); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. */ PresetSingleRegisterCommand.prototype.success = function () { this._responsePacket = this._successGetter(this._rawPacket); this.onComplete.emit(this); this.onSuccess.emit(this); }; return PresetSingleRegisterCommand; }(ModbusCommand)); exports.PresetSingleRegisterCommand = PresetSingleRegisterCommand; var ForceMultipleCoilsCommand = /** @class */ (function (_super) { __extends(ForceMultipleCoilsCommand, _super); /** * @hidden */ function ForceMultipleCoilsCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, coilAddressGetter, coilLengthGetter, coilStatusesGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._coilAddressGetter = coilAddressGetter; _this._coilLengthGetter = coilLengthGetter; _this._coilStatusesGetter = coilStatusesGetter; return _this; } Object.defineProperty(ForceMultipleCoilsCommand.prototype, "coilStartAddress", { get: function () { return this._coilAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ForceMultipleCoilsCommand.prototype, "coilLength", { get: function () { return this._coilLengthGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ForceMultipleCoilsCommand.prototype, "coilStatuses", { get: function () { return this._coilStatusesGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(ForceMultipleCoilsCommand.prototype, "coilStatusesAsCoilStatusArray", { get: function () { return this.coilStatuses.map(function (x) { return (x === true ? CoilStatus.ON : CoilStatus.OFF); }); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. */ ForceMultipleCoilsCommand.prototype.success = function () { this._responsePacket = this._successGetter(this._rawPacket); this.onComplete.emit(this); this.onSuccess.emit(this); }; return ForceMultipleCoilsCommand; }(ModbusCommand)); exports.ForceMultipleCoilsCommand = ForceMultipleCoilsCommand; var PresetMultipleRegistersCommand = /** @class */ (function (_super) { __extends(PresetMultipleRegistersCommand, _super); /** * @hidden */ function PresetMultipleRegistersCommand(rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter, registerAddressGetter, registerLengthGetter, registerValuesGetter) { var _this = _super.call(this, rawPacket, unitIdGetter, functionCodeGetter, successGetter, failureGetter) || this; _this._registerAddressGetter = registerAddressGetter; _this._registerLengthGetter = registerLengthGetter; _this._registerValuesGetter = registerValuesGetter; return _this; } Object.defineProperty(PresetMultipleRegistersCommand.prototype, "registerStartAddress", { get: function () { return this._registerAddressGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(PresetMultipleRegistersCommand.prototype, "registerLength", { get: function () { return this._registerLengthGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(PresetMultipleRegistersCommand.prototype, "registerValues", { get: function () { return this._registerValuesGetter(this._rawPacket); }, enumerable: true, configurable: true }); Object.defineProperty(PresetMultipleRegistersCommand.prototype, "registerValuesAsUint16Array", { get: function () { return new Uint16Array(this.registerValues); }, enumerable: true, configurable: true }); /** * Set success on this command to return a valid response to the emitting server. */ PresetMultipleRegistersCommand.prototype.success = function () { this._responsePacket = this._successGetter(this._rawPacket); this.onComplete.emit(this); this.onSuccess.emit(this); }; return PresetMultipleRegistersCommand; }(ModbusCommand)); exports.PresetMultipleRegistersCommand = PresetMultipleRegistersCommand; //# sourceMappingURL=modbus-commands.js.map