Help with conversion formula

I'm using Modbus to read 16 bit registers, and I'm using the AB Micro820 Plc. Let me give you my example, when I read a "REAL" number from one Micro820 to another, it takes 2 registers (32 bits) and I have to swap the 16 bit "WORD", then I run it through this formula (shown below) to get my "REAL NUMBER".
I am reading the registers just fine with my Node Red Flow, But I can't figure out to write the formula to do the conversion so that my msg.payload has the correct REAL NUMBER.

Input1:=ANY_TO_UINT(Word2);
Input2:=ANY_TO_UINT(Word1);

temp_exponent.0:=Input1.7;
temp_exponent.1:=Input1.8;
temp_exponent.2:=Input1.9;
temp_exponent.3:=Input1.10;
temp_exponent.4:=Input1.11;
temp_exponent.5:=Input1.12;
temp_exponent.6:=Input1.13;
temp_exponent.7:=Input1.14;

exponent:=temp_exponent-127;

index_exp:=-23;

FOR index := 1 TO 23 BY 1 DO
	mantissa[index]:=EXPT(2.0,index_exp);
	index_exp:=index_exp+1;
END_FOR;

mantissa_result:=1.0;

IF NOT(Input2.0) THEN
	mantissa[1]:=0.0;
END_IF;

IF NOT(Input2.1) THEN
	mantissa[2]:=0.0;
END_IF;

IF NOT(Input2.2) THEN
	mantissa[3]:=0.0;
END_IF;

IF NOT(Input2.3) THEN
	mantissa[4]:=0.0;
END_IF;

IF NOT(Input2.4) THEN
	mantissa[5]:=0.0;
END_IF;

IF NOT(Input2.5) THEN
	mantissa[6]:=0.0;
END_IF;

IF NOT(Input2.6) THEN
	mantissa[7]:=0.0;
END_IF;

IF NOT(Input2.7) THEN
	mantissa[8]:=0.0;
END_IF;

IF NOT(Input2.8) THEN
	mantissa[9]:=0.0;
END_IF;

IF NOT(Input2.9) THEN
	mantissa[10]:=0.0;
END_IF;

IF NOT(Input2.10) THEN
	mantissa[11]:=0.0;
END_IF;

IF NOT(Input2.11) THEN
	mantissa[12]:=0.0;
END_IF;

IF NOT(Input2.12) THEN
	mantissa[13]:=0.0;
END_IF;

IF NOT(Input2.13) THEN
	mantissa[14]:=0.0;
END_IF;

IF NOT(Input2.14) THEN
	mantissa[15]:=0.0;
END_IF;

IF NOT(Input2.15) THEN
	mantissa[16]:=0.0;
END_IF;

IF NOT(Input1.0) THEN
	mantissa[17]:=0.0;
END_IF;

IF NOT(Input1.1) THEN
	mantissa[18]:=0.0;
END_IF;

IF NOT(Input1.2) THEN
	mantissa[19]:=0.0;
END_IF;

IF NOT(Input1.3) THEN
	mantissa[20]:=0.0;
END_IF;

IF NOT(Input1.4) THEN
	mantissa[21]:=0.0;
END_IF;

IF NOT(Input1.5) THEN
	mantissa[22]:=0.0;
END_IF;

IF NOT(Input1.6) THEN
	mantissa[23]:=0.0;
END_IF;


FOR index_add := 1 TO 23 BY 1 DO
	mantissa_result:=mantissa_result+mantissa[index_add];
END_FOR;

IF Input1.15 THEN
	Output_sign:=-1.0;
ELSE
	Output_sign:=1.0;
END_IF;



Output_expt:=exponent;
Output_mantissa:=mantissa_result;

exponent_real:=expt(2.0,exponent);

Output_result:=output_sign*exponent_real*mantissa_result;

I believe that what you want is node-red-contrib-buffer-parser. There are a number of examples of how to use it included with the node.

Thanks, I can see where this Node is going to come in handy for a lot of my flows.

1 Like

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