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 RTU - Writing Registers
#1
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.
Reply
#2
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/showthrea...7#pid22637
Reply
#3
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.
Reply
#4
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?
Reply
#5
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()
Reply
#6
Did you disable RTU1 in modbus tab settings?
------------------------------
Ctrl+F5
Reply
#7
No, RTU1 is enabled in RTU settings. Data is being polled from slave successfully every 5 seconds.
Reply
#8
Disable it when you use the script or use proxy script.
https://forum.logicmachine.net/showthrea...dbus+proxy
------------------------------
Ctrl+F5
Reply
#9
Alright, finally I got it working trough mbproxy. Paldies.
Reply


Forum Jump: