Logic Machine Forum
Modbus RTU - Writing Registers - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Modbus RTU - Writing Registers (/showthread.php?tid=5714)



Modbus RTU - Writing Registers - Dmitry.T - 31.10.2024

Hello everybody.

Trying to implement communication between LM5 and Deye Hybrid Solar Inverter.
Reading of holding registers is working fine, objects are assigned and being updated normally. However writing is not working.

I had an assumption that writing into object will automatically update corresponding register in slave device. Looks like this assumption was wrong, please confirm?
If so, what is the procedure to write into holding register in slave device? Only trough LUA scripting?

Here is a piece of code I'm trying to use to write a register from LUA script however register is not updated. No any errors in the log. What could be wrong here:

Code:
if not mb then
    require('luamodbus')
    mb = luamodbus.rtu()
    mb:open('/dev/RS485-1', 9600, 'N', 8, 1, 'H')
    mb:connect()
end

--writing 10 into register 168
mb:writeregisters(168, 10)

mb:close()

Thank you.


RE: Modbus RTU - Writing Registers - admin - 31.10.2024

Try using writemultipleregisters (function code 16):
Code:
require('luamodbus')
mb = luamodbus.rtu()
mb:open('/dev/RS485-1', 9600, 'N', 8, 1, 'H')
mb:connect()

--writing 10 into register 168
res, err = mb:writemultipleregisters(168, 10)
log(res, err)

mb:close()

If your script is a resident then your original code will only work once because the connection is closed after the first write and since mb variable is still defined new connection won't be open.

If you want to share RTU connection between multiple scripts you should either create a profile or use mbproxy: https://forum.logicmachine.net/showthread.php?tid=839&pid=22637#pid22637


RE: Modbus RTU - Writing Registers - Dmitry.T - 31.10.2024

Hi Admin.

Still not working, no any error outputs in the log. Code is being executed from Scheduled script. Is your first statement applicable in this case?

Profile for slave device has been created. At the moment modbus connection is being opened only in this script.


RE: Modbus RTU - Writing Registers - admin - 31.10.2024

Have you run the code that I've provided? The write result can be seen in Logs tab. Do you have GND connected between LM and Modbus device?


RE: Modbus RTU - Writing Registers - Dmitry.T - 31.10.2024

Yes, was trying your provided code. Log is empty.

GND is connected between LM and slave device.

Following code outputs only "Before Write". "After Write" line can't be reached, like write procedures hangs.

Code:
require('luamodbus')
mb = luamodbus.rtu()
mb:open('/dev/RS485-1', 9600, 'N', 8, 1, 'H')
mb:connect()

log("Before Write")

--writing 10 into register 168
res, err = mb:writemultipleregisters(168, 10)
log(res, err)

log("After Write")

mb:close()



RE: Modbus RTU - Writing Registers - Daniel - 31.10.2024

Did you disable RTU1 in modbus tab settings?


RE: Modbus RTU - Writing Registers - Dmitry.T - 31.10.2024

No, RTU1 is enabled in RTU settings. Data is being polled from slave successfully every 5 seconds.


RE: Modbus RTU - Writing Registers - Daniel - 31.10.2024

Disable it when you use the script or use proxy script.
https://forum.logicmachine.net/showthread.php?tid=5608&highlight=modbus+proxy


RE: Modbus RTU - Writing Registers - Dmitry.T - 31.10.2024

Alright, finally I got it working trough mbproxy. Paldies.