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.

Convert int16 to float16
#1
Hi!

I'm working on with special modbus script. 
I have Corrigo ventilation controller and communicating over ModbusRTU. When I read register with profile then it is converted automatically thanks to this line:

{ "name": "Outside temp", "bus_datatype": "float16", "datatype": "int16", "type": "inputregister", "address": 0, "value_multiplier": 0.1, "units": "C" },

So this works.

Now I'm going to use scripting. How can I convert integer to float via script?

For example, received value is 65523, it should be -1.x degree.
Reply
#2
What is the point of using script when profile work?
------------------------------
Ctrl+F5
Reply
#3
float16 is supported by the modbus mapper. Just use "datatype": "float16" and it will work.
Reply
#4
(29.02.2020, 09:24)Daniel. Wrote: What is the point of using script when profile work?
I have 5 ventilation controllers. There is about 130 modbus points what I need to read per controller. From that, 100 is alarm points. If there is A or B alarm, I will read alarm points also. (A and B alarm is sum of all these) 
And ventilation controller can handle 10 answers per 1 fast request.

So it's quite messed up system on there but I can take control via scripting.

(02.03.2020, 07:42)admin Wrote: float16 is supported by the modbus mapper. Just use "datatype": "float16" and it will work.
Actually this way what I wrote it will work...

Now i'm scripting. I will add screenshot but answer is 65523 with minus degrees. If outside temp is over 0 then it will be correct number.
Also my variable is in 2 byte floating point format but grp.update gives 6552.3 or something like that.

Attached Files Thumbnail(s)
   
Reply
#5
If in profile value_multiplier": 0.1 works then you should do the same here
outside_te*0.1
I don't think the 65523 is a correct value. Usually such is send when No value or no sensor is on PLC side.
------------------------------
Ctrl+F5
Reply
#6
Real datatype is float32 not float16. Correct profile entry should be:
Code:
{ "name": "Outside temp", "bus_datatype": "float32", "datatype": "float32", "type": "inputregister", "address": 0, "units": "C" },
Reply
#7
(02.03.2020, 15:33)Daniel. Wrote: If in profile value_multiplier": 0.1 works then you should do the same here 
outside_te*0.1
I don't think the 65523 is a correct value. Usually such is send when No value or no sensor is on PLC side.
I figured out.

uint16 max value is 65535. If minus degrees, value is changed reversely.

65535-65523=12
12/10=1.2 
1.2*-1=-1.2°C
Reply
#8
Try reading it like this
Code:
value = mb:readinputregistervalue(0, "float32", "w")
------------------------------
Ctrl+F5
Reply
#9
I've edited Daniel's example to use internal conversion function. You might have to use different swap parameter (w) depending on your ModBus device. Possible values: n (no swap), w (word swap), b (byte swap), bw (byte and word swap).
Reply


Forum Jump: