Implement modbus tcp

Hello forumusers,

I have openhab2 on rasberrypi3 and I want to implement modbus tcp
When I install modbus (binding-modbus1 - 1.11.0)in paperUI, the inbox don’t show the modbus binding
when I configure modbus.cfg (/etc/openhab/services/modbus.cfg)
with this: tcp.slave1.connection=192.168.10.222:502:60:0:0:3:100
I see an error in the logging file:

2018-02-17 12:28:49.339 [ERROR] [org.apache.felix.configadmin ] - [org.osgi.service.cm.ManagedService, org.osgi.service.event.EventHandler, id=316, bundle=289/mvn:org.openhab.binding/org.openhab.binding.modbus/1.11.0]: Updating property tcp.slave1.connection of configuration org.openhab.modbus caused a problem: Exception when parsing configuration parameter tcp.slave1.connection = 192.168.10.222:510:60:0:0:3:100 – java.lang.IllegalArgumentException invalid baud rate: 510
org.osgi.service.cm.ConfigurationException: tcp.slave1.connection : Exception when parsing configuration parameter tcp.slave1.connection = 192.168.10.222:502:60:0:0:3:100 – java.lang.IllegalArgumentException invalid baud rate: 510

I think it is still configured as serial modbus because he gives an error for the baudrate.

Can somebody help me with this?

Thanx

Hello Luigi

the inbox don’t show the modbus binding

The modbus1 binding has no autodiscovery therefore not appers in the INBOX menu

Check if the Binding is shown in the Configuration > Bindings site.
If yes, post your modbus.cfg file complete or check the wiki there are more options as tcp.slave1.connection=192.168.10.222:502:60:0:0:3:100
at least tcp.slave1.type= is needed
And your posted configuration and your error Log seems to be not belong together.
In the Configuration Port 502 is used in the error Log Port 510 is shown.
Perhaps a duplicated Configuration?

Please use the “…code fence” option for a better reading when you post the code or config

Hello Jörg,
Thanks for your reply.
I actually want to connect to a plc via modbus tcp port 510
I tried via port 510 after that I tried 502, so when copying my log
I manually changed the port value, so I messed up here a little bit.
Today I tried with another program (Modbus poll) and I can communicate with
the plc at 192.168.10.222 port 510, so that is OK.
Then I changed the modbus config from ‘slave1’ into ‘plc’ and now
the error of the baudrate is solved.
Buth when I alter the config file sometimes ‘slave1’ shows up again instead of 'plc’
I seems like OH2 gets sometimes older configs to work with, its very confusing.
Please take a look to my config and logs

Thanks

Luigi

# sets refresh interval to Modbus polling service.
# Value in milliseconds (optional, defaults to 200)
poll=10000

## Example of Modbus TCP slave

# Connection parameters to Modbus TCP server ("slave"), values separated by colon (:)
# - host or ip of the modbus server ("slave"), mandatory
# - port, optional, default 502
# - interTransactionDelayMillis, optional, in milliseconds, default 60
# - reconnectAfterMillis, optional, in milliseconds, default 0
# - interConnectDelayMillis, optional, in milliseconds, default 0
# - connectMaxTries, optional, default 3
# - connectTimeout, optional, in milliseconds, default 0 (=infinite or OS default)
#
# As a general rule, usually only host needs to be specified. Parameters other than host 
# and port should be overridden only in cases when extreme performance is required, or when there are
# errors with the default parameter values.
#
# See wiki for more details.
#
#
#
#    (slave name)      (host or IP)
#    |                 |             (port)
#    |                 |             |   (interTransactionDelayMillis, in milliseconds)
#    |                 |             |   |  (reconnectAfterMillis, in milliseconds)
#    |                 |             |   |  | (interConnectDelayMillis, in milliseconds)
#    |                 |             |   |  | | (connectMaxTries)
#    |                 |             |   |  | | | (connectTimeout)
#    |                 |             |   |  | | | |
tcp.plc.connection=192.168.10.222:502:60:0:0:3:100
# The data type, can be "coil" "discrete" "holding" "input". See wiki for more details.
tcp.plc.type=holding

# The slave id (optional, defaults to '1')
tcp.plc.id=1

# The slave start address (optional, defaults to '0')
tcp.plc.start=1

# The number of data item to read
# (optional, defaults to '0' - but set it to something meaningful)
tcp.plc.length=1

# Value type, required for combined registers (details: http://www.simplymodbus.ca/FAQ.htm#Types)
# Can be "bit", "int8", "uint8", "int16", "uint16", "int32", "uint32", "float32"
# (optional, defaults to 'uint16')
#tcp.plc.valuetype=uint16

# For other slave parameters, consult the wiki.

## Example of Modbus Serial slave

# Connection parameters to Modbus Serial server ("slave"), values separated by colon (:)
# - serial port, mandatory. Example: /dev/ttyS0 (linux) or COM3 (windows)
# - baudRate, optional, default 9600
# - dataBits, optional, in milliseconds, default 8
# - parity, optional, default none
# - stopBits, optional, default 1
# - encoding, optional, default rtu
# - interTransactionDelayMillis, optional, in milliseconds, default 35
# - receiveTimeoutMillis, optional, in milliseconds, default 1500
# - flowControlIn, optional, default none
# - flowControlOut, optional, default none
#
# As a general rule, usually one needs to specify serial port, baudRate, dataBits, parity, stopBits, and encoding. Other parameters
# should be overriden only in cases when extreme performance is required, or when there are
# errors with the default parameter values.
#
# See wiki for more details.
#
#
#
#       (slave name)      (host or IP)
#       |                 |          (baud)
#       |                 |          |     (dataBits)
#       |                 |          |     | (parity)
#       |                 |          |     | |    (stopBits)
#       |                 |          |     | |    | (encoding)
#       |                 |          |     | |    | |   (interTransactionDelayMillis)
#       |                 |          |     | |    | |   |  (receiveTimeoutMillis)
#       |                 |          |     | |    | |   |  |    (flowControlIn)
#       |                 |          |     | |    | |   |  |    |    (flowControlOut)
#       |                 |          |     | |    | |   |  |    |    |
#       |                 |          |     | |    | |   |  |    |    |
#serial.slave1.connection=/dev/ttyS0:38400:8:none:1:rtu:35:1500:none:none

# The data type, can be "coil" "discrete" "holding" "input". See wiki for more details.
#serial.slave1.type=holding

# The slave id (optional, defaults to '1')
#serial.slave1.id=

# The slave start address (optional, defaults to '0')
#serial.slave1.start=501

# The number of data item to read
# (optional, defaults to '0' - but set it to something meaningful)
#serial.slave1.length=10

# Value type, required for combined registers (details: http://www.simplymodbus.ca/FAQ.htm#Types)
# Can be "bit", "int8", "uint8", "int16", "uint16", "int32", "uint32", "float32"
# (optional, defaults to 'uint16')
#serial.slave1.valuetype=

# For other slave parameters, consult the wiki.

here are the logs:

2018-02-18 11:04:29.530 [WARN ] [.binding.modbus.internal.ModbusSlave] - ModbusSlave (plc): Error getting a new connection for endpoint ModbusTCPSlaveEndpoint@55839e[address=192.168.10.222,port=502]. Error was: Unable to validate object
2018-02-18 11:04:29.532 [WARN ] [.binding.modbus.internal.ModbusSlave] - ModbusSlave (plc) not connected -- aborting read request net.wimpi.modbus.msg.ReadMultipleRegistersRequest@17fe992. Endpoint ModbusTCPSlaveEndpoint@55839e[address=192.168.10.222,port=502]


ten after changing only the poll interval;

2018-02-18 11:04:19.079 [WARN ] [.binding.modbus.internal.ModbusSlave] - ModbusSlave (slave1): Error getting a new connection for endpoint ModbusSerialSlaveEndpoint@14c08ae[portName=192.168.10.222]. Error was: Unable to validate object
2018-02-18 11:04:19.081 [WARN ] [.binding.modbus.internal.ModbusSlave] - ModbusSlave (slave1) not connected -- aborting read request net.wimpi.modbus.msg.ReadMultipleRegistersRequest@132eb0. Endpoint ModbusSerialSlaveEndpoint@14c08ae[portName=192.168.10.222]


Buth when I alter the config file sometimes ‘slave1’ shows up again instead of 'plc’
I seems like OH2 gets sometimes older configs to work with, its very confusing.
Please take a look to my config and logs

This was a known problem check out the Topic below, in post #2 you will find a solution.
I’m not shure if the problems still exist in OH2.2 or OH2.3?

or check this

Your Config file looks okay, perhaps you must correct the port number?
you say PLC port on 510 but in your modbus.cfg it is 502 therefore the first two errors are appears. I think.

Hello Jörg,
Your last reply was very helpfull, thank you for that.
I deleted the file modbus.config in org/openhab and did a reinstall of the modbusbinding.
My log looks now like this:

2018-02-19 18:23:15.930 [ERROR] [.binding.modbus.internal.ModbusSlave] - ModbusSlave (plc) error getting response from slave
java.lang.ArrayIndexOutOfBoundsException: 5
	at org.openhab.binding.modbus.internal.ModbusBinding.extractStateFromRegisters(ModbusBinding.java:368) [353:org.openhab.binding.modbus:1.11.0]
	at org.openhab.binding.modbus.internal.ModbusBinding.internalUpdateItem(ModbusBinding.java:243) [353:org.openhab.binding.modbus:1.11.0]
	at org.openhab.binding.modbus.internal.ModbusSlave.updateItem(ModbusSlave.java:458) [353:org.openhab.binding.modbus:1.11.0]
	at org.openhab.binding.modbus.internal.ModbusSlave.update(ModbusSlave.java:436) [353:org.openhab.binding.modbus:1.11.0]
	at org.openhab.binding.modbus.internal.ModbusBinding.execute(ModbusBinding.java:488) [353:org.openhab.binding.modbus:1.11.0]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [354:org.openhab.core.compat1x:2.2.0]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [354:org.openhab.core.compat1x:2.2.0]

So I have connection with the plc but I receive now an error in Java (error5)
I will try now some other modbus parameters in the hope to get it working.
Any ideas and suggestions are welcome.
Thanks again

Luigi

I’m not 100 % shure, but it seems to be a connection (Config) problem between the OH Binding und your plc.
The Modbus Binding gets an response from the plc who is out of the config range?
Check your data length.
Perhaps you look at the datasheet of the plc and try out some other config options.
good Luck!

What plc are you using?

Hello Jörg,

You were right again, it was a problem with the ranges in modbus.
I was polling adresses they dont exist. So the right range is adress 2001 lenght 20
My config looks now like this:

# Value in milliseconds (optional, defaults to 200)
poll=2000

tcp.plc.connection=192.168.10.222:510:60:0:0:3:100
# The data type, can be "coil" "discrete" "holding" "input". See wiki for more details.
tcp.plc.type=holding
tcp.plc.id=1
tcp.plc.start=2001
tcp.plc.length=20
# Value type, required for combined registers (details: http://www.simplymodbus.ca/FAQ.htm#Types)
# Can be "bit", "int8", "uint8", "int16", "uint16", "int32", "uint32", "float32"
# (optional, defaults to 'uint16')
tcp.plc.valuetype=uint16

And my Openhab log looks like this:

2018-02-21 14:22:24.256 [vent.ItemStateChangedEvent] - variable1 changed from 183 to 180
2018-02-21 14:22:26.335 [vent.ItemStateChangedEvent] - variable1 changed from 180 to 189
2018-02-21 14:22:28.414 [vent.ItemStateChangedEvent] - variable1 changed from 189 to 181
2018-02-21 14:22:30.493 [vent.ItemStateChangedEvent] - variable1 changed from 181 to 180
2018-02-21 14:22:32.572 [vent.ItemStateChangedEvent] - variable1 changed from 180 to 184
2018-02-21 14:22:34.648 [vent.ItemStateChangedEvent] - variable1 changed from 184 to 178
2018-02-21 14:22:36.725 [vent.ItemStateChangedEvent] - variable1 changed from 178 to 180

Thanks for all your help!

PS: Im using a Unitronics plc/hmi type V350

Luigi