Convert Long to Float with Micrologix 1400

josehr

Member
Join Date
Jun 2011
Location
CA
Posts
2
Hi all,

I am communicating with a modbus device which sends data in 32 bits format.
The MSW represent the integer part of the value and the LSW represent the fractional part as an UINT16.

How can I transform that long into a float number with the right result?


Scale X .Y indicates a binary fraction where…
X specifies the number of integer bits
Y specifies the number of fractional bits
value = integer part + (fractional part / 2^Y)

Example:

Scale = 16.16 revs X = 16, Y = 16
Raw Value = 131109
131109 decimal = 10 00000000 00100101 B
integer part = 10 B = 2 decimal
fractional part = 00000000 00100101 B = 37 decimal

Final value = 2 + 37/2^16 = 2+16/32 = 2.000564

Thanks

Jose
 
Welcome to the forum.

Since MODBUS is 16 bit by default I suggest you start by placing the information in two consecutive N words first. You can do this with a CPW #L9:0 #N7:0 1

If N7:0 contains the MSW and N7:1 contains the LSW then it gets pretty easy to compute.
First step is to determine how many digits are in N7:1. The LOG function can do this EXCEPT it is going to have problems with 0 values and rounding when casting the result to an integer, so instead of trying to trap and fix the exceptions I recommend using five compares.

LES N7:1 10 MOV .1 F8:0
LIM 10 N7:1 99 MOV .01 F8:0
LIM 100 N7:1 999 MOV .001 F8:0
LIM 1000 N7:1 9999 MOV .00001 F8:0
GEQ N7:1 10000 MOV .000001 F8:0
(The final compare may not be necessary, refer to your device documentation regarding its range)



Now a CPT statement can assemble your float.
CPT F8:1 N7:0 + (N7:1 * F8:0)

This is assuming that the data format is what you have described and not an IEEE-754 float format. If its the latter then the solution is easy, a CPW instruction with a floating point destination will do the trick.
 
Last edited:
With the fractional part in N7:1 (after Alaric's CPW) preserve a copy of N7:1/15 (true or false) then set N7:15 to 0.

Then the fractional part is N7:1/65536.0 (obviously the '.0' is to force it to a float calculation.)

Finally, if N7:1/15 WAS SET then add .5 to the fractional result.

The resetting of /15 is done because otherwise, if it was set, the value of N7:1 passed to the calculation would be negative.
 
Thanks a lot for your help.
I tried Bernie approach and it is working perfect!!

I need to do the same in the opposite direction now to write the setpoints to the device but I got the idea.

Regards

Jose
 

Similar Topics

Hi all, I'm communicating with a modbus device which sends data in 32 bits data. You can see the structure in this example: N7:4...
Replies
6
Views
4,582
I have an Allen Bradley MicroLogix 1400. I am trying to display a value onto an HMI, however my HMI will not accept a Long (i.e. L10:2) as an...
Replies
6
Views
10,599
Using Logix 500 with a AB Micrologix 1100, I need to convert a 5 character string to a long. example 99999(string) to 99999 (long). The function...
Replies
7
Views
3,189
Hello all, I'm currently working on a servo motor linear positioning system (ball screw). I'm all set up regarding communication between my HMI...
Replies
1
Views
94
I have an application using an incremental encoder and then I convert it to degree (0-360) using calculation program. For a while, the calculation...
Replies
8
Views
328
Back
Top Bottom