This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Modbus TCP slave example
#4
Due to 2-byte <> 4-byte conversion, you need to use two uint16 objects: 2/1/2 and 2/1/3 in this example. Float16 object address is 2/1/1.

1. Add a script to float16 object, it will split the value and write it to 2/1/2 and 2/1/3. Change 15.15.255 to your LM physical address so this script is only triggered from KNX bus but not from LM internally.
Code:
if event.src ~= '15.15.255' and event.src ~= 'local' then
value = event.getvalue()
raw = knxdatatype.encode(value, dt.float32).dataraw

r1 = raw:byte(1) * 0x100 + raw:byte(2)
r2 = raw:byte(3) * 0x100 + raw:byte(4)

grp.update('2/1/2', r1, dt.uint16)
grp.update('2/1/3', r2, dt.uint16)
end

2. Attach a script to 2/1/3 (second uint16 object). It converts two uint16 regs to float32 internally and then writes the value to float16 object.
Code:
require('luamodbus')

r1 = grp.getvalue('2/1/2')
r2 = grp.getvalue('2/1/3')

value = luamodbus.convert('float32', r1, r2)
grp.write('2/1/1', value, dt.float16)


Messages In This Thread
Modbus TCP slave example - by admin - 08.01.2016, 08:46
RE: Modbus TCP slave example - by Deniss - 15.01.2016, 08:58
RE: Modbus TCP slave example - by admin - 15.01.2016, 14:39
RE: Modbus TCP slave example - by admin - 18.01.2016, 12:57
RE: Modbus TCP slave example - by automatikas - 02.02.2017, 11:30
RE: Modbus TCP slave example - by admin - 02.02.2017, 12:09
RE: Modbus TCP slave example - by automatikas - 02.02.2017, 13:35
RE: Modbus TCP slave example - by automatikas - 29.03.2017, 10:26
RE: Modbus TCP slave example - by admin - 29.03.2017, 10:37
RE: Modbus TCP slave example - by leondias - 29.03.2017, 10:40
RE: Modbus TCP slave example - by automatikas - 29.03.2017, 10:41
RE: Modbus TCP slave example - by admin - 29.03.2017, 10:55
RE: Modbus TCP slave example - by automatikas - 29.03.2017, 11:15
RE: Modbus TCP slave example - by Deniss - 20.04.2017, 06:47
RE: Modbus TCP slave example - by admin - 20.04.2017, 15:38
RE: Modbus TCP slave example - by duvelken - 02.12.2017, 08:13
RE: Modbus TCP slave example - by admin - 03.12.2017, 09:58
RE: Modbus TCP slave example - by FatMax - 13.12.2018, 14:44
RE: Modbus TCP slave example - by admin - 13.12.2018, 15:23
RE: Modbus TCP slave example - by FatMax - 13.12.2018, 15:53
RE: Modbus TCP slave example - by rw_echo - 16.03.2023, 07:57
RE: Modbus TCP slave example - by admin - 17.03.2023, 10:52

Forum Jump: