cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

2 Uint16 to Float conversion in JS - Need help

tcoufal
12-Amethyst

2 Uint16 to Float conversion in JS - Need help

Hi I am facing problem which I cannot solve.

I am working on Modbus example, I have successfully connected few devices through WSEMS and lua script and currently I am working on another.

Problem is that if I use standard expression like this:

     properties.speedCh1 = {key="input_register/1/30515?format=Float", handler="modbus_handler1", basetype="NUMBER"}

It will take 2 Uint16 and converts them into Float in ABCD order. Badly enough, vendor of this device has MSB-LSB in wrong order. I need to do the conversion in CD AB order. Is there a way how to change a Modbus handler to this kind of conversion.

Other way would be to sent every Uint16 separately and do the conversion in Thingworx with Javascript.

Does anyone now how to do that?

Thanks a lot guys and gals.

3 REPLIES 3
tcoufal
12-Amethyst
(To:tcoufal)

For anyone whos interested. Java script function on Thingworx side is as follows:

     var z1 = 38912; // choose your order by assigning a proper value

     var z2 = 16768; // choose your order by assigning a proper value

     var value = z2<<16 | z1;

     var b=value & 0x7fffff;

     var e=((value>>23) & 0xff)-127;

     var m=1+b*Math.pow(2, -23);

     var result=m*Math.pow(2, e);

     if (value & 0x80000000) {

result=-result;

     }

Hi Tomas,

is that let you read Float as CD AB ? That's great, I will try that. Have you made any changes in Modbus-example.lua file or Modbus-handler.lua file?

tcoufal
12-Amethyst
(To:pkeckes)

Hi Peter,

yes it should. It takes 2 INTs and does that conversion to Float.

You need to change Modbus-example.lue from:

properties.VoltageVan = {key="input_register/1/30002?format=Float", handler="modbus_handler1", basetype="NUMBER"}

which takes two UINT16 (30002+30003) and does that AB CD conversion on its own (big endian)

to:

properties.VoltageVan1 = {key="input_register/1/30002?format=Int16", handler="modbus_handler1", basetype="NUMBER"}

properties.VoltageVan2 = {key="input_register/1/30003?format=Int16", handler="modbus_handler1", basetype="NUMBER"}

then you just assign z1 and z2 accordingly and you do the conversion on ThingWorx side. Not very efficient, better would be to have that option on EDGE side though.

Tomas

Top Tags