# Varta memory - convert Modbus TCP value to watt

**URL:** <https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255>\
**Category:** Industrial\
**Created:** [27 August 2021 07:40 UTC](https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255 "2021-08-27T07:40:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![plaxtika](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plaxtika/32/47062_2.png) [@plaxtika](https://discourse.nodered.org/u/plaxtika)\
**Post date:** [27 August 2021 07:40 UTC](https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255/1 "2021-08-27T07:40:27Z")

</div>

Hi there,

can someone help me how to correctly convert the payload of a ModbusTCP return? I need the value in watts. Currently, a value of 65123 (from register 1066) or similar comes from the register. Unit is UNIT16.

Another register (2066) contains -\> active power = (value in # 1066) \* 10 ^ (value in # 2066) - which is a multiplication by a power of ten, right? But no idea how I do it ...

Modbus returns "0" from 2066.

What do I have to enter in a function node for the conversion?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 August 2021 17:59 UTC](https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255/2 "2021-08-27T17:59:35Z")

</div>

try the Javascript function [Math.pow()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow)

```javascript
let value = 65123; // replace with value from register 1066
let exponent = 0; // replace with value from register 2066

let activePower = value * Math.pow(10, exponent);
msg.payload = activePower;
return msg

```

---

<div class="post-metadata">

**Author:** ![plaxtika](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plaxtika/32/47062_2.png) [@plaxtika](https://discourse.nodered.org/u/plaxtika)\
**Post date:** [27 August 2021 18:22 UTC](https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255/3 "2021-08-27T18:22:15Z")

</div>

Hi, thx - but this isnt it.

Return is 65123 ... as number

I have a tcl script that does the job - I can't use it in node red, can I?

```auto
package provide modbus 1.0

namespace eval ::modbus {
    variable Priv
    array set Priv [list \
        sn 0x1234 \
        -mode "TCP" \
        -ip "127.168.178.8" \
        -port "502" \
        -com "/dev/ttyUSB0" \
        -settings "9600,n,8,1" \
        -timeout 2 \
    ]
}

proc ::modbus::configure {args} {
    variable Priv
    set names [lsort [array names Priv -*]]

    foreach {opt val} $args {
        if {[lsearch $names $opt] < 0} {
            puts "bad option \"$opt\": must be $names"
            exit
        }
        set Priv($opt) $val
    }
}

proc ::modbus::asc2bin {args} {
    # ex. asc2bin 0x02 0x01 0x00 0x00 0x01 0x01 0x03

    set ret ""
    foreach c $args {
        append ret [binary format c $c]
    }
    return $ret
}

proc ::modbus::bin2asc {data} {

    set len [string length $data]
    set str ""
    for {set i 0} {$i<$len} {incr i} {
         binary scan [string index $data $i] c ch
         if {$ch < 0} {incr ch 256}
         lappend str [format %02X $ch]
    }
    return $str
}

proc ::modbus::debug {data} {
    puts [::modbus::bin2asc $data]
}

proc ::modbus::cmd {fun args} {
    variable Priv

    set fun [string range 00[expr $fun] end-1 end]

    foreach {reqCmd rspLen} [eval ::modbus::cmd${fun}_pack $args] break
    
    set mode [string tolower $Priv(-mode)]

    foreach {reqCmd rspLen} [::modbus::pack_$mode $reqCmd $rspLen] break

    ::modbus::port_open
    set rspCmd [::modbus::port_send $reqCmd $rspLen]
	
    ::modbus::port_close

    set rspCmd [::modbus::unpack_$mode $reqCmd $rspCmd]

    if {$rspCmd == ""} {return ""}

    if {$mode == "tcp"} {set reqCmd [string range $reqCmd 6 end]}

    return [::modbus::cmd${fun}_unpack $reqCmd $rspCmd]
}

proc ::modbus::cmd03_pack {sta addr len} {

# function : read holding registers
    # station : 1 byte
    # function : 1 byte (always 0x03)
    # addr : 2 bytes
    # read len : 2 bytes (how much registers)

# response
    #station : 1 byte
    #function : 1 byte (always 0x03)
    #byte count : 1 byte
    #data : N*2 bytes (reg1_low...reg1_hi...reg2_low...reg2_hi...)

    # 1+1+1+2*len
    # sta=>1 fun=>1 dlen=>1
	# return [list [binary format ccSS $sta 0x03 $addr $len] [expr 1+1+1+2*$len]]
	return [list [binary format ccSS $sta 0x03 $addr $len] [expr 1+1+1+2*$len]]
}

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 August 2021 18:35 UTC](https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255/4 "2021-08-27T18:35:18Z")

</div>

sorry i have no idea how tcl scripts work or how to use them in node-red  
(i was under the impression that you had them in NR already)  
you cant use some Modbus nodes to get the values from you device ?

> **[node-red-contrib-modbus](https://flows.nodered.org/node/node-red-contrib-modbus)**
>
> The all in one Modbus TCP and Serial contribution package for Node-RED.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 October 2021 18:35 UTC](https://discourse.nodered.org/t/varta-memory-convert-modbus-tcp-value-to-watt/50255/5 "2021-10-26T18:35:53Z")

</div>

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