Modbus Example OH3

Hi,

I have tried the Modbus Rollershutter example from the Binding documentation.

I have adapted the Things file according to my configuration.

Every time I execute a command the write transformation writes to the wrong Modbus address. Is it the Write Transformation or the Thing configuration?

things

Bridge modbus:tcp:localhostTCPRollerShutter [ host="192.168.178.25", port=502 ] {
    Bridge poller holding [ start=32006, length=3, refresh=1000, type="holding" ] {
        // Since we are using advanced transformation outputting JSON,
        // other write parameters (writeValueType, writeStart, writeType) can be omitted
        Thing data rollershutterData [ readStart="32006", readValueType="int16",writeStart="32006",  writeTransform="JS:rollershutter.js", writeValueType="int16", writeType="holding"  ]

        // For diagnostics
        Thing data rollershutterDebug0 [ readStart="32006", readValueType="int16", writeStart="32006", writeValueType="int16", writeType="holding" ]
        Thing data rollershutterDebug1 [ readStart="32007", readValueType="int16" ]
        Thing data rollershutterDebug2 [ readStart="32008", readValueType="int16" ]

items

// We disable auto-update to make sure that rollershutter position is updated from the slave, not "automatically" via commands
Rollershutter RollershutterItem "Roller shutter position [%.1f]" <temperature> { autoupdate="false", channel="modbus:data:localhostTCPRollerShutter:holding:rollershutterData:rollershutter" }

// For diagnostics
Number RollershutterItemDebug0 "Roller shutter Debug 0 [%d]" <temperature> { channel="modbus:data:localhostTCPRollerShutter:holding:rollershutterDebug0:number" }
Number RollershutterItemDebug1 "Roller shutter Debug 1 [%d]" <temperature> { channel="modbus:data:localhostTCPRollerShutter:holding:rollershutterDebug1:number" }
Number RollershutterItemDebug2 "Roller shutter Debug 2 [%d]" <temperature> { channel="modbus:data:localhostTCPRollerShutter:holding:rollershutterDebug2:number" }

sitemap

sitemap modbus_ex_rollershutter label="modbus_ex_rollershutter" {
    Switch item=RollershutterItem label="Roller shutter [(%d)]" mappings=[UP="up", STOP="X", DOWN="down", MOVE="move"]

    // For diagnostics
    Setpoint item=RollershutterItemDebug0 minValue=0 maxValue=100 step=20
    Text item=RollershutterItemDebug0
    Text item=RollershutterItemDebug1
    Text item=RollershutterItemDebug2

Write Transformation

// Wrap everything in a function
// variable "input" contains data passed by openHAB
(function(cmd) {
    var cmdToValue = {"UP": 1,  "DOWN": -1, "MOVE": 1, "STOP": 0};
    var cmdToAddress = {"UP": 1, "DOWN": 1, "MOVE": 2, "STOP": 2};

    var value = cmdToValue[cmd];
    var address = cmdToAddress[cmd];
    if(value === undefined || address === undefined) {
        // unknown command, do not write anything
        return "[]";
    } else {
        return (
            "["
              + "{\"functionCode\": 6, \"address\":" + address.toString() + ", \"value\": [" + value +  "] }"
            + "]"
        );
    }
})(input)

log

2021-05-11 12:50:57.110 [WARN ] [rt.modbus.internal.ModbusManagerImpl] - Try 1 out of 3 failed when executing request (ModbusWriteRegisterRequestBlueprint [slaveId=1, reference=1, registers=ModbusRegisterArray(0001), maxTries=3, getFunctionCode()=WRITE_SINGLE_REGISTER]). Will try again soon. Error was: net.wimpi.modbus.ModbusSlaveException Error Code = 2 [operation ID 27663d15-548b-4977-8829-89d15a02b067]
2021-05-11 12:50:57.173 [WARN ] [rt.modbus.internal.ModbusManagerImpl] - Try 2 out of 3 failed when executing request (ModbusWriteRegisterRequestBlueprint [slaveId=1, reference=1, registers=ModbusRegisterArray(0001), maxTries=3, getFunctionCode()=WRITE_SINGLE_REGISTER]). Will try again soon. Error was: net.wimpi.modbus.ModbusSlaveException Error Code = 2 [operation ID 27663d15-548b-4977-8829-89d15a02b067]
2021-05-11 12:50:57.238 [ERROR] [rt.modbus.internal.ModbusManagerImpl] - Last try 3 failed when executing request (ModbusWriteRegisterRequestBlueprint [slaveId=1, reference=1, registers=ModbusRegisterArray(0001), maxTries=3, getFunctionCode()=WRITE_SINGLE_REGISTER]). Aborting. Error was: net.wimpi.modbus.ModbusSlaveException Error Code = 2 [operation ID 27663d15-548b-4977-8829-89d15a02b067]

In the log you can see that it wants to write to register 0001. But in the Thing config I have set register 30006.

Setup:
Openhab 3.1m3 on Windows

Thanks for Answers

It’s doing what you told it to do in your transformation.

Because the transform returns JSON for ‘advanced’ action, the target address will be as given by the JSON, and data Thing writeStart ignored.

This is the whole point of the JSON special action, to give you bare-bones access to the inner workings of the Modbus transfer.

End result, modify your transformation for the correct register address.

Thanks,

sorry for the question but where can I change the address in the transformation, in the area where I declare the variables ?

var value = cmdToValue[cmd];
var address = cmdToAddress[cmd];

Ok, i found the line!

var cmdToAddress = {"UP": 1, "DOWN": 1, "MOVE": 2, "STOP": 2};

Bear in mind this example is about how to use the JSON feature to route one openHAB command to different Modbus registers, depending on context. This is rarely needed.

It’s not “how to use a rollershutter”, which will depend on what your device wants to happen.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.