Simple example of how to implement Modbus TCP slave via a resident script (sleep time = 0). It only supports binary objects as coils and 1-byte / 2-byte integer objects as registers. Number of coils and registers is not limited, object mapping can be set by filling coils, registers and regdt tables.
Thank you for this script!
Situation is like this -
My client is PLC, and it supports only 4byte float. I get data from KNX, where object is 2 byte float.
Is in LM4 possible conversation from 2byte float to 4 byte float (KNX->PLC) and 4byte float to 2 byte float (PLC->LM4)? When value changes from KNX device, I can see it in my PLC as 4 byte float, and change same object 4 byte float on PLC, then on KNX device changes data 2 byte float?
Lua uses 64-bit floating point numbers, so internally there's not difference between 2-byte or 4-byte float. Adding 4-byte float to Modbus is tricky because it only supports 2-byte registers, so there are additional encoding/decoding steps required. I'll try to provide an updated example later.
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.
-- modbus slave, listen on all interfaces and default port 502mb = luamodbus.tcp()
mb:open('0.0.0.0', 502)
-- setting slave id is optional-- mb:setslave(1)
With this (adjust serial settings and slave id as needed):
-- modbus slave, listen on all interfaces and default port 502mb = luamodbus.tcp()
mb:open('0.0.0.0', 502)
-- setting slave id is optional-- mb:setslave(1)
With this (adjust serial settings and slave id as needed):
(29.03.2017, 10:55)admin Wrote: Replace 'H' with 'F', flush is not needed
Strange...
Have tried half or full duplex no response. I use usb-COM port adapter. from DB9 i use 2,3 for rxtx and 5 for GND. LM started responding when i disconnect GND from RS port of LM just rx tx pins.
LM starts sending response back but not correct data for modbus just echos same query back. Modbus master fires illegal response exception then.
02.12.2017, 08:13 (This post was last modified: 02.12.2017, 08:21 by duvelken.)
Hi,
i tried the modbus example to read out some LM data to my PLC.
connection on TCP is fine and i don't get scanning errors when i use qmodmaster
but all values remain 0
only things i changed are this:
Code:
12345678
-- list of coil mapping, starting from 0coils = { '1/1/2' }
-- list of register mapping, starting from 0registers = { '1/1/1' }
-- list of register data types, element count must match registers tableregdt = { dt.uint16 }
1/1/2 is just a 1 bit boolean set to 1
i also changed the ip because i got an error "illegal data adress" on my qmodmaster when scanning.
do i need to change other things to get this to work ?
thx
for some reason i tried to inactivate and activate the script and the value came up.
is this normal ? do i have to reactivate the script everytime i make changes ?
Code is reloaded when script is saved but all previously defined variables remain in memory. Since mb is already defined, init code (if not mb then...) is not reached after changes are saved. The only way to do a full reload is to disable then enable the script.