Help with modbus configs

Hi there! Im started to use openhab2 on raspberry pi 3. I want to connect my modbus device to openhub. I started as basic ui and make sample sitemap:

sitemap demo label="My home automation" {
    Frame label="Date" {
        Text item=Date
    }
    Frame label="Demo" {
        Switch item=Lights icon="dimmablelight" mappings=[OFF="All Off"] 
        Text item=IO_State 
    }
}

I have one Switch and text items.
Also i edit modbus settings in /service/modbus.cfg and set poll time = 2000 ms, serial settings, coil, start adr=0 - parameters. I can see that my tool isInterrogating.
item has two registers i have to read (340) and write (378).

now i want to write value 3840 to modbus reg 378 by switch ON action and poll reg 340 ant print bin data to text item, but do not understand how to.

Could not you help me?

Have you created an items file? (in /etc/openhab2/items/)
If yes: post the contents. If no: check http://docs.openhab.org/addons/bindings/modbus1/readme.html

i did it, but i cant understand it syntaxis, can you tell my what i have to do, some simple example?
i want to write reg 378 value 3840 by switch ON stat.
My item file has line:
Switch MySwitch β€œMy Modbus Switch” (ALL) {modbus=β€œslave1:5”}
5 is a register address?

Personally, I don’t use the modbus binding so I can’t help much…
Post also your modbus.cfg and I am sure that someone who has been using the binding will help you.
In general search the forum, read other people’s experiences, and experiment :slight_smile:

Here is my modbus.cfg

# sets refresh interval to Modbus polling service.
# Value in milliseconds (optional, defaults to 200)
 poll=2000
.....
.....
.....
 serial.slave1.connection=/dev/ttyUSB0:9600: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=coil

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

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

# The number of data item to read
# (optional, defaults to '0' - but set it to something meaningful)
 serial.slave1.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')
 serial.slave1.valuetype=uint16

# For other slave parameters, consult the wiki.

Hi

Check this post for similar configuration: [Solved] ModBus and cheap 4 relay module (D18994)

In the linked example, numerical value 256 (0x0100) is written when command ON is received. With command OFF numerical value 512 (0x0200) is written. Note that example is write only, it does not read the value from modbus. You can make it write with some more configuration.

See separate index for reading and writing and extended item configuration format for more info.

Btw, I noticed that you are now reading coils, but in your post you talk about registers. Which one is it?

Thanks!
But now i cant make any action…
sidemap:

sitemap demo label="My home" {
    Frame label="Demo" {
        Switch item=MyLight icon="dimmablelight" 
        Text item=MyState label="Main State [%s]"
    }
}

item:

Switch 	MyLight  "Main Light" 
Text 	MyState	 "Main State"

and rules:

import java.net.URI

val msg = "This is a message"

rule "Light ON"
when
  Item MyLight changed from OFF to ON
then
 postUpdate(MyState, msg) 
end

why When i turn on switch on sitemap text does not changing?

is there an error being logged in openhab.log?

By the way try this (no import line, and define val within the rule) to see if it works:

rule "Light ON"
when
	Item MyLight changed from OFF to ON
then
	val msg = "This is a message"
	MyState.postUpdate(msg) 
end

Yes i also solve it, thanks a lot!

1 Like

Now i can to send commands by modbus:

Switch 		SetRel1  	"Set rel  1"   	<wallswitch> {modbus=">[slave1:378:trigger=ON,transformation=1],  >[slave1:378:trigger=OFF,transformation=0]"}

second command turn off all DO

>[slave1:378:trigger=OFF,transformation=0]"

But this way i revrite PLC register with new value How can i do data OR operation to send PLC to turn on or off corresponding relay or all together?

A small comment, you do not need the transformation if you just want to write 0 or 1 with Switch items. It useful in the case you mentioned originally : writing e.g. value of 3840 with ON. But you have what works as well, it is just a bit complicated.

Are you now working with registers or coils? You have been talking of both.

im just working with holding registers. My PLC output register supported only 3, 4, 6, 16 functions.
Note: value 1, 2, 4, 8 (not 3840 that a mistake) in minor byte of register switch on output PLC’s relays 1 to 4 separately
image
I cant write single bits for correspondenting relays separately. every time i update register with new value, but i need to do like: reg | = value…and then write β€œreg” in PLC.

Is modbus discribing in rules?
Now i think about python scrypts to serve modbus in my case…is looks easily

Hi

As you have figured out there is no concept of writing individual bits of registers with those function codes in the actual modbus protocol.

Have you double checked that you cannot use modbus coils to refer to PLC output relays?

However, you can use rules to do the And/or bit operations necessary to work with holding registers. This has been discussed earlier here in the forum (I think by @rossko57, maybe he has the example somewhere ) but cannot find the post now. I think the basic idea is that you introduce items for each bit (16 relays/bit in each register). You would update the state of these items when the item bound to modbus register state changes . You would also handle the received commands to these bit items, use and/or operations to update the whole register value.

Best

In documentation says that DO register supports functions: 3, 4 , 6 , 16.
Now i’ve make python scrypt to launch itself with arguments to set DO bits directly.
My scrypt is launching by cmd: scrypt.py [relay number] -[state]
It works well.Ask a question please how can i use it in openhab rules by switch state update statement?

UPD: i found command executeCommandLine, i neet to exec it once, is it ritght to type comman like:

executeCommandLine("sudo python3 /etc/openhab2/scripts/relay.py 1 -on")

UPD: yes, it works well! now i can manage DO bits separatly

Now i try to read back scrypt retun data with executeCommandLine.
My rule has:

rule "IO state"
when
	Item MyLight received update
then
	if (MyLight.state == ON) {
		var String results = executeCommandLine("python3 /etc/openhab2/scripts/input.py 340")	
		//value = Integer.parseInt(readings)
		logInfo("MyResults:", results)
		IOState.sendCommand(results)
    } 
end

what the way python scrypt have to return data?
In pthon scrypt i tried to return data like
os.system("echo " + str(value))
print( str(value))

but no one has no effect

here the log:

2017-08-10 08:53:20.355 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'python3 /etc/openhab2/scripts/input.py 340’
2017-08-10 08:53:20.364 [INFO ] [se.smarthome.model.script.MyResults:] -