Lua-libmodbus on TRB145

Dear Community,

I would like to call modbus directly via separate service, because I need to force heating up a humidity sensor in case of high humidity to dry it. When heater is on, of course it is not advised to read temperature, so I must disable this reading, which I can’t achive by webUI.

When I create a simple demo-rtu Lua script with lua-libmodbus like

local dev = mb.new_rtu(“/dev/rs485”, 9600, “none”, 8, 1)
dev:set_debug()
ok, err = dev:connect()
if not ok then error("Couldn’t connect: " .. err) end

but I get error

Opening /dev/rs485 at 9600 bauds (N, 8, 1)
lua: /usr/local/bin/s.lua:200: Couldn’t connect: No such file or directory`

@AndzejJ @anon65719490 I see that you can provide expert knowledge on this topic.

Any clue what’s wrong, why I can’t connect to rs485 ?
I have asked AI regarding to acces to openwrt resources (like user/groups rights) and disabled modbus service to be sure that device is not blocked, but nothing helped.

Regards,
Michal

Hello Michail,

Thank you for your inquiry. Your request is currently under review and analysis. Once the evaluation is complete, I will get back to you with further information, findings, or suggestions.

Thank you for your patience in the meantime.

Best regards,

Hello,

Apologies for the delay.

Could you please clarify what the intended goal of this Lua script is and elaborate on the script a bit more? As this inquiry is outside of my scope, and I’m not that familiar with Lua scripting, I can only provide you with insights/suggestions to check or try. From the code, I assume you are trying to open an RTU connection on /dev/rs485. Can you confirm if the device /dev/rs485 is actually present and visible under /dev?

Additionally, instead of using a custom Lua script, you can also call the Modbus service directly using ubus commands. For example (change parameters according to your requirements):

ubus call modbus_client.rpc serial.test '{"id":1,"timeout":1,"function":4,"first_reg":2,"reg_count":"2","data_type":"16bit_int_hi_first","no_brackets":1,"serial_type":"/dev/rs485","baudrate":19200,"databits":8,"stopbits":1,"parity":"none","flowcontrol":"none"}'

More useful information and examples for Modbus calls via ubus can be found in related forum topics here:

Best regards,

Hi @anon65719490,

Yes, rs485 is present and visible under ls -l /dev so it should be accessible.

lrwxrwxrwx    1 root     dialout         11 Jan  1  1970 rs485 -> /dev/ttyHS0

to use a ubus calls to Modbus Client I need to enable Modbus in WebUI (can be without any calls), with just a lua script I could ‘skip’ WebUI at all and use direct calls to lua-libmodbus.

Regards
Michal

Hello @MichalPankau,

Just to confirm, have you already tried using /dev/ttyHS0 directly? From what you’ve shared, I see that the rs485 interface is attached to this particular device node.

Best regards,

@anon65719490,

I used an example from github demo-rtu.lua with minor mods, but /dev/rs485 nor /dev/ttyHS0 works.

root@TRB145:~# lua demo-rtu.lua
using libmodbus runtime version:        3.1.6
using lua-libmodbus compiled against libmodbus:         3.1.6
byte timeout:   0       500000
response timeout:        0       500000
Opening /dev/rs485 at 9600 bauds (N, 8, 1)
lua: demo-rtu.lua:14: Couldn't connect: No such file or directory
stack traceback:
        [C]: in function 'error'
        demo-rtu.lua:14: in main chunk
        [C]: ?
root@TRB145:~# lua demo-rtu.lua
using libmodbus runtime version:        3.1.6
using lua-libmodbus compiled against libmodbus:         3.1.6
byte timeout:   0       500000
response timeout:        0       500000
Opening /dev/ttyHS0 at 9600 bauds (N, 8, 1)
lua: demo-rtu.lua:14: Couldn't connect: No such file or directory
stack traceback:
        [C]: in function 'error'
        demo-rtu.lua:14: in main chunk
        [C]: ?

Regards,
Michal

This topic was automatically closed after 60 days. New replies are no longer allowed.