SpaceLogic KNX Forum
Schneider Electric SpaceLogic KNX forum to get support and share knowledge including selection, installation and troubleshooting for spaceLYnk, Wiser for KNX, eConfigure KNX, SpaceLogic KNX Hybrid module and other topics.
Link copied. Please paste this link to share this article on your social media post.
Hello,
Has anyone knowledge about where to find complementary information about or even better a LUA script example for configuring a spaceLynk logic controller as a Modbus TCP server or secondary a Modbus RTU slave and link KNX objects/GAs to Modbus registers?
The intention is to provide KNX data to a Modbus TCP client/RTU Master.
Link copied. Please paste this link to share this article on your social media post.
Hello Ole,
the most basic script for HL to act as modbus tcp server can looks like this:
-- modbus init
if not mb then
require('luamodbus')
mb = luamodbus.tcp()
--open port 502 for modbus tcp connection
mb:open('*', 502)
mb:connect()
-- slave id
mb:setslave(10)
-- init slave storage for coils, discrete inputs, holding registers and input registers
mb:setmapping(10, 10, 10, 10)
--initialize holding register address5=13, address6=15
mb:setregisters(5,13,15)
-- define register write callback
mb:setwriteregistercb(function(register, value)
alert("register %d has been changed to %d", register, value)
end)
end
--returns value of 2 registers starting by address 5, it is performed in every cycle of the script
reg5, reg6 = mb:getregisters(5,2)
log('reg5=',reg5)
log('reg6=',reg6)
--waits for incomming indication from master and sends reply
mb:handleslave()
You can access to your holding registers using fucntions getregisters(). But it may be usefull to use and define some callback function, which will execute everytime, there is some writing event to some register.
In homeLYnk user guide, there can be found more function, which can be use for your purposes. Refer to the chapter 3.5 Slave Mode Functions.
Link copied. Please paste this link to share this article on your social media post.
Hello,
here is a simple example of controlling the iACT24 via Smartlink TCP from homeLYnk script.
The address of Smartlink is 10.154.12.75.
Modbus TCP port of Smartlink is set to 502.
iAct24 contactor is connected to the port 6 oo Smartlink.
This resident script can be used for reading status of the contactor:
require('luamodbus')
mb = luamodbus.tcp()
-- IP: 192.168.1.2, port: 1234
mb:open('10.154.12.75', 502)
mb:connect()
--read status register of the contactor
cont_status = mb:readregisters(14400)
-- Write contactor status to the grp address
-- 0: comm. fault, 1: comm. fault, 2: cont. open, 3: cont. closed
grp.write('1/1/1', cont_status)
mb:close()
This event driven script can be used to create a open command to the contactor (driven by grp address 1/1/2):
require('luamodbus')
mb = luamodbus.tcp()
mb:open('10.154.12.75', 502)
mb:connect()
if grp.getvalue('1/1/2') then
mb:writeregisters(14401, 1)
log('Contactor has been opened.')
end
mb:close()
This event driven script can be used to create a close command to the contactor (driven by grp address 1/1/3):
require('luamodbus')
mb = luamodbus.tcp()
mb:open('10.154.12.75', 502)
mb:connect()
if grp.getvalue('1/1/3') then
mb:writeregisters(14401, 2)
log('Contactor has been closed.')
end
mb:close()
Best Regards
Tomas
Link copied. Please paste this link to share this article on your social media post.
Thank you for these interesting script examples which I believe demonstrates the homeLynk as an Modbus TCP client and the Smartlink is the Modbus TCP server.
Do you have examples of the other way around, meaning the homeLynk being the Modbus TCP server responding to TCP connections and Modbus reads and writes from a third-party Modbus TCP client?
-which function makes the homeLynk listening for connections on port 502?
-which functions defines registers and their values in the homeLynk (coils and discrete inputs, input and holding registers)?
Link copied. Please paste this link to share this article on your social media post.
Hello Ole,
the most basic script for HL to act as modbus tcp server can looks like this:
-- modbus init
if not mb then
require('luamodbus')
mb = luamodbus.tcp()
--open port 502 for modbus tcp connection
mb:open('*', 502)
mb:connect()
-- slave id
mb:setslave(10)
-- init slave storage for coils, discrete inputs, holding registers and input registers
mb:setmapping(10, 10, 10, 10)
--initialize holding register address5=13, address6=15
mb:setregisters(5,13,15)
-- define register write callback
mb:setwriteregistercb(function(register, value)
alert("register %d has been changed to %d", register, value)
end)
end
--returns value of 2 registers starting by address 5, it is performed in every cycle of the script
reg5, reg6 = mb:getregisters(5,2)
log('reg5=',reg5)
log('reg6=',reg6)
--waits for incomming indication from master and sends reply
mb:handleslave()
You can access to your holding registers using fucntions getregisters(). But it may be usefull to use and define some callback function, which will execute everytime, there is some writing event to some register.
In homeLYnk user guide, there can be found more function, which can be use for your purposes. Refer to the chapter 3.5 Slave Mode Functions.
Link copied. Please paste this link to share this article on your social media post.
Hi Tomas,
Thanks again for brilliant examples that are very helpful in understanding the use of modbus in homeLynk and spaceLynk.
Link copied. Please paste this link to share this article on your social media post.
Hi all,
We developed an Application Note (I attached it) to connect in both directions a spaceLYnk (or homeLYnk) with SmartStruxureLite (SSL) MPM controler via Modbus TCP: in this solution spaceLYnk (or homeLYnk) is a Modbus TCP server.
This Application Note is not fully validated (document has to be reviewed) but can be reused for your usage using only the scripts for spaceLYnk in spaceLYnk (or homeLYnk) = restore the script backup in AN package: AN009_v1-0_MBTCP_SL-SSL_script.tar
(we used it to exchange with a comX200 to read data in spaceLYnk and we have no issue).
All data presented to be read by a Modbus is 4bytes size but obviously you can set a boolean in a 4bytes register and your client has to know to read only the right bit (registers are seen with comX200 with LittleEndian)
So the principle to use the spaceLYnk script is quite simple:
* in spaceLYnk (or homeLYnk) you want to export a KNX object via Modbus TCP, the Modbus TCP client will read it
Principle: 1/1/1 object => you tag it via spaceLYnk object table with TAG 1 : NOTIFIED_TO_SSL_VIA_MBTCP + , + TAG 2: unique index number between 1 to 100)
real example for 3 KNX objects:
- 1/1/1 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 1
- 1/2/5 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 2
- 1/2/6 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 3
=> the second TAG is an index in Modbus register table of 4 bytes per line:
to read with a Modbus TCP client:
- 1/1/1 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 1 => set in Modbus TCP client: register = 1 (with 2 or 4 bytes register size)
- 1/1/5 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 2 => set in Modbus TCP client: register = 3 (with 2 or 4 bytes register size)
- 1/1/6 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 3 => set in Modbus TCP client: register = 5 (with 2 or 4 bytes register size)
* in spaceLYnk (or homeLYnk) you want to export a KNX object via Modbus TCP, the Modbus TCP client will write it:
Principle: 1/1/1 object => you tag it via spaceLYnk object table with TAG 1 : NOTIFIED_BY_SSL_VIA_MBTCP + , + TAG 2: unique index number between 1 to 100)
real example for 3 KNX objects:
- 1/1/1 object TAG = NOTIFIED_BY_SSL_VIA_MBTCP , 1
- 1/2/5 object TAG = NOTIFIED_BY_SSL_VIA_MBTCP , 2
- 1/2/6 object TAG = NOTIFIED_BY_SSL_VIA_MBTCP , 3
=> here be careful: we didn't test with a standard Modbus TCP client (SmartStruxureLite (SSL) MPM controler uses 2 Modbus messages to write Modbus registers so it means it may not work with a standard Modbus TCP client! (but we can do an evolution: futhermore we interested in a real business case to complete and validate a new Application Note: simple Modbus TCP server in spaceLYnk using our spaceLYnk to SSL MPM Application Note)
Full example with read registers and write registers (second TAG has to be unique between read registers and write registers) :
- 1/1/1 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 1
- 1/2/5 object TAG = NOTIFIED_BY_SSL_VIA_MBTCP , 2
- 1/2/6 object TAG = NOTIFIED_BY_SSL_VIA_MBTCP , 3
- 1/2/7 object TAG = NOTIFIED_TO_SSL_VIA_MBTCP, 4
Don't hesitate if you need more help and notify your need: we may also consider to industrialize a solution through an Application Note.
Regards,
Lionel
Create your free account or log in to subscribe to the board - and gain access to more than 10,000+ support articles along with insights from experts and peers.