18.11.2020, 12:33
Here is a sample code after quick view on reference manual. Just to help you find a right way.
And my question to LM gurus: what will happens if there are device listed in 'ModBus' table located on the same RS485 bus. I assume that connection parameters are the same and there is no address conflict.
Code:
require('luamodbus')
mb1 = luamodbus.rtu()
-- RS-485 port name and number. here is RS485, but it can be RS485-1, RS485-2, ttyUSB0
-- connection parameters must to accord device settings
mb1:open('/dev/RS485', 38400, 'E', 8, 1, 'H')
mb1:connect()
-- select modbus slave ID for read/write operations (55 here)
mb1:setslave(55)
--[[
read values
we need to read hold register (command #3)
modbus we can to assess:
#1 - coil
#2 - discrete input
#3 - hold register
#4 - input register
]]--
-- read all registers related to first MP
i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16, i17, i18, i19 = mb1:readregisters(0, 20)
-- do something with this information
-- set setpoint to 50
mb1:writeregisters(0, 50)
mb1:close()
And my question to LM gurus: what will happens if there are device listed in 'ModBus' table located on the same RS485 bus. I assume that connection parameters are the same and there is no address conflict.