Modbus TCP Master binding

1,665 views
Skip to first unread message

khome

unread,
Sep 24, 2012, 3:16:15 PM9/24/12
to ope...@googlegroups.com

Hi all


I've created modbus tcp binding - works as master. Just put attached jar to your addons folder and make changes to openhab.cfg and .items file. If somebaody could explain me how to share sources I will happily share it. Please find short description of the binding below.

Modbus binding allows to connect to multiple Modbus slaves as TCP master. This implementation works with coils (boolean values) only.
Entries in openhab config file should look like below.
Most of config parameters are related to specific slaves, the only exception is

modbustcpmaster:poll=<value>

which sets refresh interval to Modbus polling service. Value is in milliseconds - optional, default is 200

 modbustcpmaster:<slave-name>:<slave-parameter>
 
 <slave-name> is unique name per slave you are connecting to.
 <slave-parameter> are pairs key=value
 
 Valid keys are
 host mandatory
 port TCP port, optional, default 502
 id  slave id, optional, default 1
 start slave start address, optional, default 0
 length number of data item to read, default 0 (but set it to something meaningful :)
 type data type, can be either "coil" or "register", now only "coil" is supported
 
 Minimal construction in openhab.config will look like
 
 modbustcpmaster:slave1.host=192.168.1.50
 modbustcpmaster:slave1.length=10
 modbustcpmaster:slave1.type=coil
 
 connects to slave on ip=192.168.1.51 and reads 10 coils starting from address 0
 More complex setup could look like

modbustcpmaster:slave1.host=192.168.1.50
modbustcpmaster:slave1.port=502
modbustcpmaster:slave1.id=41
modbustcpmaster:poll=300
modbustcpmaster:slave1.start=0
modbustcpmaster:slave1.length=32
modbustcpmaster:slave1.type=coil

ModbusBindingProvider provides binding for Openhab Items
There are two ways to bind an item to modbus coils/registers

 1) single coil/register per item
 Switch MySwitch "My Modbus Switch" (ALL) {modbustcpmaster="slave1:5"}
 
 This binds MySwitch to modbus slave defined as "slave1" in openhab.config reading/writing to the coil 5

 2) separate coils for reading and writing
 Switch MySwitch "My Modbus Switch" (ALL) {modbustcpmaster="slave1:<6:>7"}
 
 In this case coil 6 is used as status coil (readonly) and commands are put to coil 7 by setting coil 7 to true.
 You hardware should then set coil 7 back to false to allow further commands processing.

 

org.openhab.binding.modbus.tcp.master-1.1.0-SNAPSHOT.jar

Thomas Eichstädt-Engelen

unread,
Sep 24, 2012, 3:20:47 PM9/24/12
to ope...@googlegroups.com
Hi,

I've created modbus tcp binding - works as master. Just put attached jar to your addons folder and make changes to openhab.cfg and .items file. If somebaody could explain me how to share sources I will happily share it. 

thanks for sharing this!

Please checkin the sources to your private clone. We can then easily merge the new binding to the default branch after a thorough code review.

Regards,

Thomas E.-E.

Dmitry Krasnov

unread,
Sep 24, 2012, 3:58:10 PM9/24/12
to ope...@googlegroups.com
Thomas, sorry for my ignorance - how do i create that private clone?
(I came from visual studio unvers so eclipes is a scary thing to me
:))

Thanks
Dmitry

2012/9/24 Thomas Eichstädt-Engelen <teic...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "openhab" group.
> To post to this group, send email to ope...@googlegroups.com.
> To unsubscribe from this group, send email to
> openhab+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/openhab?hl=en.

jason....@gmail.com

unread,
Sep 24, 2012, 4:58:07 PM9/24/12
to ope...@googlegroups.com
From the link below, you can find a freeware of Modbus (Serial, TCP/IP and Plus) Master and Slave End software package:xMasterSlave.

Falk Sauer

unread,
Sep 25, 2012, 2:15:16 AM9/25/12
to ope...@googlegroups.com
Hi khome,

your binding works for me of the first look.

i tried it with moxa e1214, the next try i will make with e1212.

if i use only DO 0..5 it works, the modbus start address for them is 0

on the second look i had a understanding problem

if i use the DI 0..5 it dosn't works, the modbus start address is 10000

see also http://www.moxa.com/doc/man/ioLogik_E1200_Series_Users_Manual_v9.pdf

without the source i have questions:

1.) can i make a double slave definition on one IP/modbusgateway like:

# Moxa e1214 (only one hardware module!)
# outputs (6)
modbustcpmaster:poll=500
modbustcpmaster:slave1.host=192.168.1.1
modbustcpmaster:slave1.port=502
modbustcpmaster:slave1.start=0
modbustcpmaster:slave1.length=6
modbustcpmaster:slave1.type=coil

# inputs (6)
modbustcpmaster:slave2.host=192.168.1.1
modbustcpmaster:slave2.port=502
modbustcpmaster:slave2.start=10000
# i tried also 1,10,100,1000
modbustcpmaster:slave2.length=6
modbustcpmaster:slave2.type=coil

2.) how should i configure this?

thanks a lot!

regards
    Falk

Dmitry Krasnov

unread,
Sep 25, 2012, 5:05:51 AM9/25/12
to ope...@googlegroups.com
Falk hi
 
Yes, there was a bug in my code - thanks for finding it. I am attaching corrected package - please give it a try.
 
As I don't have your hardware, I am testing it with Modbus Slave emulator (http://www.modbustools.com/modbus_slave.asp).
 
I've created a slave with starting address 0 and length 10007.
 
openhab.config contains following lines
 
modbustcpmaster:poll=300
 
modbustcpmaster:slave1.host=127.0.0.1
modbustcpmaster:slave1.port=502
modbustcpmaster:slave1.id=1
modbustcpmaster:slave1.start=10001
modbustcpmaster:slave1.length=6
modbustcpmaster:slave1.type=coil
 
modbustcpmaster:slave2.host=127.0.0.1
modbustcpmaster:slave2.port=502
modbustcpmaster:slave2.id=1
modbustcpmaster:slave2.start=0
modbustcpmaster:slave2.length=6
modbustcpmaster:slave2.type=coil
 
.items file was
 
Group All
 
Contact Contact1 "Contact1 [MAP(en.map):%s]" (All)   {modbustcpmaster="slave1:0"}
Contact Contact2 "Contact2 [MAP(en.map):%s]" (All)   {modbustcpmaster="slave1:1"}
Contact Contact3 "Contact3 [MAP(en.map):%s]" (All)   {modbustcpmaster="slave1:2"}
Contact Contact4 "Contact4 [MAP(en.map):%s]" (All)   {modbustcpmaster="slave1:3"}
Contact Contact5 "Contact5 [MAP(en.map):%s]" (All)   {modbustcpmaster="slave1:4"}
Contact Contact6 "Contact5 [MAP(en.map):%s]" (All)   {modbustcpmaster="slave1:5"}
 
Switch Switch1 "Switch1" (All)   {modbustcpmaster="slave2:0"}
Switch Switch2 "Switch2" (All)   {modbustcpmaster="slave2:1"}
Switch Switch3 "Switch3" (All)   {modbustcpmaster="slave2:2"}
Switch Switch4 "Switch4" (All)   {modbustcpmaster="slave2:3"}
Switch Switch5 "Switch5" (All)   {modbustcpmaster="slave2:4"}
Switch Switch6 "Switch5" (All)   {modbustcpmaster="slave2:5"}
 
.sitemap file was
 
sitemap khome label="Main Menu"
{
Frame {
Text item=Contact1
Text item=Contact2
Text item=Contact3
Text item=Contact4
Text item=Contact5
Text item=Contact6
Switch item=Switch1
Switch item=Switch2
Switch item=Switch3
Switch item=Switch4
Switch item=Switch5
Switch item=Switch6
}
 
}
 
Thanks
 
2012/9/25 Falk Sauer <falk....@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "openhab" group.
> To view this discussion on the web visit
org.openhab.binding.modbus.tcp.master-1.1.0-SNAPSHOT.jar

Falk Sauer

unread,
Sep 25, 2012, 5:34:57 AM9/25/12
to ope...@googlegroups.com
Hi khome,

your code has always a little problem pls. look on this:

11:31:03.127 INFO  o.o.u.w.i.s.WebAppServlet[:99] - Started Classic UI at /openhab.app
11:31:31.203 INFO  runtime.busevents[:42] - Switch1 received command ON
11:31:31.809 INFO  runtime.busevents[:42] - Switch2 received command ON
11:31:32.362 INFO  runtime.busevents[:42] - Switch3 received command ON
11:31:32.970 INFO  runtime.busevents[:46] - Switch4 state updated to OFF
11:31:32.987 INFO  runtime.busevents[:42] - Switch4 received command ON
11:31:33.010 INFO  runtime.busevents[:46] - Switch4 state updated to ON
11:31:33.628 INFO  runtime.busevents[:46] - Switch5 state updated to OFF
11:31:33.645 INFO  runtime.busevents[:42] - Switch5 received command ON
11:31:33.668 INFO  runtime.busevents[:46] - Switch5 state updated to ON
11:31:34.186 INFO  runtime.busevents[:46] - Switch6 state updated to OFF
11:31:34.203 INFO  runtime.busevents[:42] - Switch6 received command ON
11:31:34.228 INFO  runtime.busevents[:46] - Switch6 state updated to ON

never i see any update on the contacts, ho wcan we debug this little problem?

Regards
   Falk

Falk Sauer

unread,
Sep 25, 2012, 6:00:40 AM9/25/12
to ope...@googlegroups.com
Hi home,

have you seen, the manual says that switches are using function 1,5,15 and contacts are using function 2,
is this the problem?

Regards
    Falk

Dmitry Krasnov

unread,
Sep 25, 2012, 6:14:49 AM9/25/12
to ope...@googlegroups.com
Falk hi
 
In my understanding Contacts represent read-only modbus registers, so you hardware is responsible for changing them. Switches represent read-write registers, so they can be changed by software commands. Hope this helps
 
Thanks
Dmitry
 


 
2012/9/25 Falk Sauer <falk....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/7MF-9DwhHG0J.

khome

unread,
Sep 25, 2012, 6:21:10 AM9/25/12
to ope...@googlegroups.com
Hi All
 
I've created a clone project with all the Modbus Sources
 
 
feel free to browse and comment
 
Thanks
Dmitry aka khome
 

понедельник, 24 сентября 2012 г., 23:16:16 UTC+4 пользователь khome написал:

Thomas Eichstädt-Engelen

unread,
Sep 25, 2012, 6:44:19 AM9/25/12
to ope...@googlegroups.com
Hi Dmitry,

i noticed you helped yourself ... great ;-)

Thanks for checking in!

Thomas E.-E.

Falk Sauer

unread,
Sep 25, 2012, 6:46:11 AM9/25/12
to ope...@googlegroups.com
Hi dimitry,

if i want to connect a openhab bit as contact with the first hardware bit in DI (digital input) mode i need
to read the modbus address 10000 on the moxa e1214 modul. In the manual link in this thread they write that DI bits
can be read with function 2 (whatever they mean with them). In the slave simulator from your link you can also simulate
such a function 2 mode if i have look right.

Regards

  Falk


Am Montag, 24. September 2012 21:16:16 UTC+2 schrieb khome:

Falk Sauer

unread,
Sep 25, 2012, 8:08:57 AM9/25/12
to ope...@googlegroups.com
Hi all,

we (dimitry an i) found that in modbusbinding are errors by reading modbus function 2 bits
http://www.simplymodbus.ca/FC02.htm (contact in openhab), this is not programmed in the moment,
switch bits with function code 1 working fine also on my hardware.  
Dimitry make a new version what can in the next step read with function code 2 and has optional a little bit more debug output.


Regards
    Falk


Am Montag, 24. September 2012 21:16:16 UTC+2 schrieb khome:

khome

unread,
Sep 25, 2012, 3:21:21 PM9/25/12
to ope...@googlegroups.com
Fixed in attached file
 

вторник, 25 сентября 2012 г., 16:08:57 UTC+4 пользователь Falk Sauer написал:
org.openhab.binding.modbus.tcp.master-1.1.0-SNAPSHOT.jar

veseo

unread,
Nov 18, 2012, 4:52:00 PM11/18/12
to ope...@googlegroups.com
Hi All,

I've downloaded the release 1.0.0 but I cannot find the Modbus binding into the addons. I saw that this thread is quite recent, so I would like to ask if and when Modbus will be included in the official release.

I would like to use Modbus to integrate openHAB with Souliss, that is a framework that runs over AVR/Arduino to build a distributed domotic network.

Thanks.

Regards,
Dario.

veseo

unread,
Nov 18, 2012, 5:05:39 PM11/18/12
to ope...@googlegroups.com
I apologize, I saw now the release note for 1.1 where Modbus is listed :)

Is yet planned the release date?

Thanks,
Dario.

Kai Kreuzer

unread,
Nov 18, 2012, 5:08:40 PM11/18/12
to ope...@googlegroups.com
Hi Dario,


Is yet planned the release date?

Check the "News" section on the project homepage:

November 14, 2012 -- The openHAB release 1.1 is scheduled for December 16, 2012. See the preliminary release notes here.

Best regards,
Kai

veseo

unread,
Nov 18, 2012, 5:15:47 PM11/18/12
to ope...@googlegroups.com, k...@openhab.org
Ops... was in the home page... sorry I missed.

Thanks,
Dario.

Alexey Dubashov

unread,
Nov 29, 2012, 12:40:19 AM11/29/12
to ope...@googlegroups.com
Hello, Dmitry!

I noticed that when I reset the PLC (switch off then switch it on) the modbus binding can not reconnect to it.
It shows the huge amount of errors in modbus binding.

Thanks,
Alexey.


понедельник, 24 сентября 2012 г., 23:16:16 UTC+4 пользователь khome написал:

Hi all

Dmitry Krasnov

unread,
Nov 29, 2012, 4:33:48 AM11/29/12
to ope...@googlegroups.com
Let me check it later today


2012/11/29 Alexey Dubashov <dubas...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/J86b-lnnrs0J.

Helmut Lehmeyer

unread,
Feb 9, 2013, 10:32:35 AM2/9/13
to ope...@googlegroups.com
Hello Dmitry,

same problem, the modbus binding can not reconnect after Network connection was interrupted or PLC gets restart.

Has
anyone solved the problem?


Thanks,
Helmut

Dmitry Krasnov

unread,
Feb 9, 2013, 1:50:01 PM2/9/13
to ope...@googlegroups.com
Yes, looks like I've solved this - will post new version to this thread tomorrow. Sorry for the delay


2013/2/9 Helmut Lehmeyer <helmut....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.

To post to this group, send email to ope...@googlegroups.com.

khome

unread,
Feb 10, 2013, 12:55:23 AM2/10/13
to ope...@googlegroups.com
Please find attached. Don't hesitate to send your comments

суббота, 9 февраля 2013 г., 22:50:01 UTC+4 пользователь khome написал:


2013/2/9 Helmut Lehmeyer <helmut....@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+unsubscribe@googlegroups.com.
org.openhab.binding.modbus-1.1.0-SNAPSHOT.jar

Helmut Lehmeyer

unread,
Feb 10, 2013, 6:21:18 AM2/10/13
to ope...@googlegroups.com
Hello khome,

first I have testet the binding on several ohab (1.0,1.1 and 1.2) versions on Ubuntu and Windows (java version "1.6.0_24"):
The
binding can not be
initialized by ohab and no message appears in log. It feels as it is not present in addon folder.

In a Windows or Linux Environment with java greater 1.6 (java version "1.7.0_05"), the binding works
as expected.
When I cut the Network-Cable the binding runs in
"java.net.SocketTimeoutException: Read timed out"  then
"java.net.SocketException: Connection reset" some
"net.wimpi.modbus.ModbusIOException: Premature end of stream (Header truncated)"
after connecting cable,
data delivered again.

THANK YOU! :-)

Helmut


Dmitry Krasnov

unread,
Feb 10, 2013, 8:16:30 AM2/10/13
to ope...@googlegroups.com
I believe you need Java 1.7 to use this bunding


2013/2/10 Helmut Lehmeyer <helmut....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.

Helmut Lehmeyer

unread,
Feb 10, 2013, 12:43:01 PM2/10/13
to ope...@googlegroups.com
Yes Java 1.7!
Your binding works great now! :-)

Found a Limitation/Bug: I have a Counter in my PLC which is counting over 65535 (short). I need minimum 86400 -> 24 h in seconds.

PLC is a moxa E2210

My Configuration:
# DI01 Counter
modbus:slave2.host=192.168.0.49
modbus:slave2.port=502
modbus:slave2.id=1
modbus:slave2.start=3
modbus:slave2.length=1
modbus:slave2.type=input

Does the binding need another Datatype for type=input ?

Hope, this is not a ohab Limitation, this would be a serious design flaw?!

Best regards,
Helmut

Dmitry Krasnov

unread,
Feb 11, 2013, 12:08:48 AM2/11/13
to ope...@googlegroups.com
I believe this is more of Modbus limitation.  Modbus registers are 16 bit, so max number stored in register is 65536. If you want something big, could you split your counter data into to registers and store them in the registers like
reg0 = counter_data % 0xffff
reg1 = counter_data / 0xffff
And do inverse transform in openhab
counter_data = 0xffff * reg1 + reg0
 
hope this helps


2013/2/10 Helmut Lehmeyer <helmut....@gmail.com>

Helmut Lehmeyer

unread,
Feb 12, 2013, 3:37:12 AM2/12/13
to ope...@googlegroups.com
Yes, this helps!

Thank You, khome.


Nathan Stratton

unread,
Feb 16, 2015, 3:38:53 PM2/16/15
to ope...@googlegroups.com
I am seeing state updated messages every 200 ms for each item regardless of the state actually changing. This also fills up my event.log with useless duplicates state updates when nothing has changed. Other then that Modbus TCP is working great, but I can't scale it without this bug fixed. I am willing to offer to pay to have it fixed if anyone wants to take a stab at it.

Nathan Stratton

unread,
Feb 27, 2015, 7:52:45 PM2/27/15
to ope...@googlegroups.com
I paid a developer to fix, or at least work around for this issue, the diff can be found at:

Reply all
Reply to author
Forward
0 new messages