Skip to content

Modbus Registers Advanced Topics

Cloud-Automation edited this page Oct 4, 2014 · 3 revisions

Modbus Register - Advanced Topics

Let's say you have a counter. You can count up or down and have low and high limits. You want to send the unit a command to count up or down. Stateflag one and two indicate if the lower or upper limit is reached.

First of all we create a ´CounterRegister´ class and inherit from the Register class.

CounterRegister = function (client, loop, offset) {

    Register.call(this, client, loop, offset);

}

CounterRegister.inherits(Register);

Then we setup the increase and decrease commands.

CounterRegister.method('inc', function () {
    
    return this.execute(1); // 1 is the command id for increase

});

CounterRegister.method('dec', function () {

    return this.execute(2); // 2 is the command id for decrease

});

We also need two methods for the upper and lower limits reached.

CounterRegister.method('lowerLimitReached', function () {

    return this.getStateflag_1();

});

CounterRegister.method('upperLimitReached', function () {

    return this.getStateflag_2();

});

What we also need is a way to be informed about updates. We want to know when the upper/lower limit is reached of when these limits are left. We also want to know about updates on the counter value that we find in the status arguments.

CounterRegister = function (client, loop, offset) {

    ...

    this._upperLimit = null;
    this._lowerLimit = null;

    this._counterValue = null;

    this.on('update_status', function () {

        if (this._upperLimit !== this.status.stateflag_2) {
            this._upperLimit = this.status.stateflag_2;
            this.fire('upperLimitChanged');
        }

        if (this._lowerLimit !== this.status.stateflag_1) {
            this._lowerLimit = this.status.stateflag_1;
            this.fire('lowerLimitChanged');
        }

        if (this._counterValue !== this.status.arg) {
            this._counterValue = this.status.arg;
            this.fire('counterChanged');
        }


    });

}

That's all. Feel free to implement your own and let me hear about it!

Clone this wiki locally