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.
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
# 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.
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.
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
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
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.
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:
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))