Community discussions

MikroTik App
 
sjfp
just joined
Topic Author
Posts: 7
Joined: Fri Mar 04, 2022 2:10 pm

Modbus MQTT 2 Register Formats

Wed Apr 20, 2022 5:44 pm

Hi have a modbus device that uses 32bit signed big-endian formatted number
So in the script using
:local modbusread [/iot modbus read-holding-registers slave-id=1 num-regs=2 reg-addr=514 as-value once];

This gives the result as 15,3492 so how can this be formatted to give the actual register value of 986532 to send to the IoT MQTT
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Modbus MQTT 2 Register Formats

Wed Apr 20, 2022 6:33 pm

The help page shows how to get the values, but the "($output->"values")" is hidden unless you scroll:
[admin@device] > {:local output [/iot modbus read-holding-registers slave-id=0x03 num-regs=0x1 reg-addr=0x0 as-value once];:put [($output->"values")]}
2349
[admin@device] > {:local output [/iot modbus read-holding-registers slave-id=0x03 num-regs=0x5 reg-addr=0x0 as-value once];:put [($output->"values")]}
2353;3;500;75;38

In the 2nd example, it's an array type. So you may need to use a :foreach on the results of the "/iot read-holding-registers"
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Modbus MQTT 2 Register Formats

Sat Apr 23, 2022 4:27 pm

This gives the result as 15,3492 so how can this be formatted to give the actual register value of 986532 to send to the IoT MQTT
In the 2nd example, it's an array type. So you may need to use a :foreach on the results of the "/iot read-holding-registers"
To be more specific, each member of the array I think is 16-bit. So you can multiple the first element (15) * 65536, the add the 2nd 16-bits (3492) to that.

So 15*65536+3492 = 986532, which is what you're expecting.

I don't have a KNOT, but this should be pretty close:
{
:local modbusread [/iot modbus read-holding-registers slave-id=1 num-regs=2 reg-addr=514 as-value once];
:local reg514 ( ( ($modbusread->"values"->0) * 65536 ) + ($modbusread->"values"->1) )
:put $reg514
}
 
sjfp
just joined
Topic Author
Posts: 7
Joined: Fri Mar 04, 2022 2:10 pm

Re: Modbus MQTT 2 Register Formats

Mon Apr 25, 2022 9:31 pm

Thank for the detail, yes that was result we are looking for. Appreciated your input.

Who is online

Users browsing this forum: No registered users and 13 guests