Modbus Binding: Implemented TCP connection mechanism

  • Platform information:
    • OS: Windows 10
    • openHAB version: 2.5M1

Hello, can you please tell how the TCP connections are done with the Modbus Binding?
It seems to be that the TCP connection is opening and closing with every MODBUS/TCP transaction/poll.
Is there any way (in configuration?) to keep the TCP connection opened?

My problem: I want to connect to a Modbus Server from a PLC (Implemented in OSCAT Lib) and poll the data in an intervall of < 1s. Now the server is not able to handle the ‘fast’ connects/disconnects. So to keep the connection opened might be the best to do. This is also described in the implementation guide.

http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf

Implementation Rules:

  1. Without explicit user requirement, it is recommended to implement the automatic TCP connection management
  2. It is recommended to keep the TCP connection opened with a remote device and not to open and close it for each MODBUS/TCP transaction. Remark: However the MODBUS client must be capable of accepting a close request from the server and closing the connection. The connection can be reopened when required.

What do you think? Thank you in advance.
BR Sebastian

That is the default situation, chosen because it is safest
There are devices out there that support only one connection, so doing otherwise could block other users.

Of course, it’s in the docs. reconnectAfterMillis

1 Like

Thank you very much. Works great.
The description of the parameter in the PaperUI was missleading me a little bit.

In the docs it is more clear in my opinion.
Maybe there a way to also descripe in the PaperUI what value = ‘0’ exactly means?

For me is perfect now.

What would you suggest? I have the text

The connection is kept open at least the time specified here. Value of zero means that connection is disconnected after every MODBUS transaction. In milliseconds.

To be fair, the PaperUI descriptions are just memory joggers. You’re not going to set up a Modbus config without referring to the docs, it is very labour intensive.

Sorry, I need to roll back my statement or there are some discrepancies I don’t unterstand…

So in my configuration I set the reconnectAfterMillis to 10 000 ms.
The poller is polling in an intervall of 100 ms.
So in this case the polled value should be not older than 100 ms? Is that correct? In my test it seems to be still a ‘big’ delay of seconds.
I will do some more testing on different platform by the end of week.
Thanky you. BR

I don’t know what that means.

Okay, so TCP connection will be dropped and remade every 10 seconds. If you need to avoid overheads at the slave end I would go longer.

That would be fairly ambitious, and could show up any performance bottlenecks in your system depending on configuration.
Note poller parameter timeBetweenTransactionsMillis will limit poll rates as you approach it, default 60mS

See also, each data thing has a parameter updateUnchangedValuesEveryMillis which reduces OH updates if target register doesn’t change, default 1000mS
90% of your polls probably don’t produce OH updates - that’s a Good Thing.

Hi,
I set the reconnectAfterMillis now to 60 000 ms.
The poller is set to an intervall of 1 000 ms.
updateUnchangedValuesEveryMillis is now set to 1 000 ms.

So in this case I would expect that, if data is changing from the modbus master, it will update within few seconds in OpenHAB.
But in this case the data ist not updating…
Only when a new TCP connection is new done (after 60 seconds) the value is updating.

I logged the connection on WireShark and it seems to be that the TCP Connection is still closing…
In my opinion it should not close. See Screenshot:
192.168.100.5 is OpenHAB, 192.168.100.2 is the ModBus Master.

So what can be the problem here? Did I missunderstood something general?
Thank you in advance.
BR

PS: I updated my testsystem to 2.5 M2.

Maybe. You might have to show your things configuration. TCP and poller things please.

Hi, I created the things in PaperUI. So here is the config (nothing special in my opinion):



For your TCP thing -
Time between reconnections
means what it says. When OH disconnects a TCP session, it will wait this long before trying the next connection. You probably want that back at the default 0 instead of one minute.

Disconnect and establish the connection again after this time
is set to zero. You’d want to set this to a minute to keep TCP connection open.`

For your poller thing
Poll interval
is as you wanted at one second. But it cannot poll at one second, because the TCP thing is set to disconnect after every poll and then wait a minute before reconnecting.

Hi, thank you.
So for me this means that a poll can only be done with a new session.
There is no way to do continuous polling while keeping the TCP connection open?

Yes, you just need to put the settings correctly.

Make the box in PaperUI for TCP thing labelled Time between reconnections = 0

Make the box in PaperUI for TCP thing labelled Disconnect and establish the connection again after this time = 60000

Thank you very much.
With this setting it is working very good.