Modbus TCP writing in OH2 doesn't work

Hello community
I have a problem with Modbus writing. Reading works!

•Hardware: RPi 3 B+
•OS: openhabian
•openHAB version: openHAB 2.4

I defined an Item:
Number PVWertzumSchreiben “Wert [%.3f kW]” { channel=“modbus:data:TwinCat:holding:write0000:number” }

For testing, i wrote a value in (for ex. 34.21) and show it in the sitemap.
If i do a comment in front of the {, the 34.21 are showed. But if I activat the {}, it doesn’t work anymore.
If the problem is the Item or something later, I don’t know.

And I defined a Thing:
Bridge modbus:tcp:TwinCat [ host=“192.168.1.99”, port=502, id=1 ] {
Bridge poller holding [ start=12288, length=46, refresh=1000, type=“holding” ] {
Thing data holding0000 [ readStart=“12288”, readValueType=“float32_swap” ]
Thing data holding0002 [ readStart=“12290”, readValueType=“float32_swap” ]
Thing data holding0004 [ readStart=“12292”, readValueType=“float32_swap” ]
// geht so weiter…
Thing data holding0042 [ readStart=“12330”, readValueType=“float32_swap” ]
Thing data holding0044 [ readStart=“12332”, readValueType=“float32_swap” ]
}

Bridge poller items [ start=12388, length=2, refresh=1000, type=“discrete” ] {
Thing data write0000 [ writeStart=“12388”, writeValueType=“float32_swap”, writeType=“holding” ]
}
}

The first part, the reading, works!
Later, the writing, doesn’t work.

I’m sure, the 12388 are correct. I can read and write it with “Modbus Poll”-program and see the canges in the PLC and the other way.

But I don’t know if the type=“discrete” is correct or if they “holding” should be.
And if “poller items” is correct, I don’t know as well…

In the “event.log” I see something “strange”, but I can’t understand:
2019-08-26 21:29:17.042 [ome.event.ItemCommandEvent] - Item ‘PVWertzumSchreiben’ received command 34.21
2019-08-26 21:29:17.047 [nt.ItemStatePredictedEvent] - PVWertzumSchreiben predicted to become 44.2

He is receiving the command, to write 34.21. But somehow, the 44.2 from later stay there from a test without {}.

Can someone help me, please?

In Modbus, “discrete” refers to read-only binary, a one-bit data element that you can only read from the device. Like a coil that you cannot write to,

That has nothing to do with your writing problem - but it probably means your defined poller Thing has failed unless your PLC happens to have two discrete inputs at 12388. I’d guess it probably fails to read poll, you’d see that in openhab.log
I’m not sure if a data Thing can write if its “parent” poller is offline/failed.

You probably just want to get rid of that discrete poller altogether.

Okay, so the magic thing about the Modbus binding is that it writes on demand, when you command your openHAB Item. That means that writing is not related to read polling at all.
That in turn, means that you can set up data Things for write addresses and types that have nothing to do with the read addresses for that Thing.

You can just add that write-only data Thing to the existing “holding” poller Thing.
Remember you will need to change the Item channel to match the new name.

There is one other thing you need to look at. Because you are dealing with 32-bit float format, you must remember that is actually two 16-bit Modbus registers.
Unless you tell it otherwise, the binding will write those registers one at a time.
That’s almost certainly not acceptable to your PLC which will want both parts of the float in one go.
I think you will need to set the writeMultipleEvenWithSingleRegisterOrCoil parameter for your Float32 to force writing in one go.

Bridge modbus:tcp:TwinCat [ host=“192.168.1.99”, port=502, id=1 ] {
   Bridge poller holding [ start=12288, length=46, refresh=1000, type=“holding” ] {
      Thing data holding0000 [ readStart=“12288”, readValueType=“float32_swap” ]
      ...
      Thing data write0000 [ writeStart=“12388”, writeValueType=“float32_swap”, writeType=“holding”, writeMultipleEvenWithSingleRegisterOrCoil=true ]

Please use code fences for this sort of thing in forum posts.

2 Likes

Awesome post rossko57, as always :slight_smile:

One clarification: the 32 bit data types are always written using FC 16 (write multiple holding registers) [ref], no matter writeMultipleEvenWithSingleRegisterOrCoil.

In other words, the writeMultipleEvenWithSingleRegisterOrCoil parameter affects the writing only when we are writing single coil or single holding register.

2 Likes

Thankyou Sami, I think we had that discussion and I forgot :slight_smile:

For the OP, you can just remove writeMultipleEvenWithSingleRegisterOrCoil (but it won’t hurt if you leave it in)

Thanks!
Excellent work. I see now the change in my Modbus Tool.

And because I know now, that it calls “code fences”, I know now how to post code nice :wink:

Code

Thanks a lot!

2 Likes