Float to String

averellen

Member
Join Date
Jan 2004
Location
Guelph
Posts
112
Is it possible to copy a float file to a string file.
I'm using a SLC5/04

I tried COP #F91:0 #ST90:3 1
But says something about not being able to create data file.

Any help is very much appreciated.

Adam
 
If your Float is between -9999.9999 and 9999.9999, I suggest
parsing the float into two integers. Left of dec point and right of dec point. Use MOV for that. MOV type casts.
Then use the AIC instruction to convert each integer to a separate string and then insert a decimal point to another string and then use ACN instruction to assemble the final string.
If you are using an HMI then I suggest that you do the conversion within the HMI it would be a lot easier in VB script.
 
When you are programming online you cannot create the data file if it does not exist and you cannot automatically expand the file if it is smaller than what you addressed. Also, make sure you do not have that file number already assigned. You will have to program those offline. That is the only thing I can think of that will give you that message.

When copying a float into a string what you are going to get is the IEEE floating point bit pattern in the first four character positions in your string, you are not going to get a character representation of the actual number. The four ascii characters that correspond to the bit patter in each of the four bytes of the floating point number is what you will see. Now there may be times when this is desirable, such as a need to transmit a IEEE formatted float over RS-232 with some other string data, but dont expect just to see the value of the number in the string register.
 
I didn't say that you should COP copy, I said MOV.
MOV will cast an Integer out of the Float.
Here is the exact detailed description.
First MOV the Float into Integer we will call say "IA".
You will see there all digits left of the decimal point.
Then you subtract "IA" from the original float number and put the result in a new float called "FB". You will find there all digits right of the decimal point.
Then MOV "FB" into a new Integer called "IC". you will find there all digits right of the decimal point.

After that just use the description I gave you above to concetenate all strings.
OOps. What about the negative numbers?
Well if you are going to use negative numbers you need to first determine
that the number is smaller than 0 and multiply it by -1. If you do that set a flag bit to indicate that you are operating on a negative number. Later on
after you have finished the conversion to String create another string containing a "-" sign, then assemble all the strings by using ACN.
 
1. Does the MOV instruction from a float to an integer truncate or round? If rounded then, after the following subtract you may have a negative number.

2. After the subtract you would probably have to multiply the float remainder by some factor (10, 100, 1000?) to set the number of decimal points in your final string representation. This is assuming you get around the possible negative number from observation #1.
 
Does the MOV instruction from a float to an integer truncate or round?

The MOV instruction rounds.

It depends on what the user is trying to do. If he wants to build a string that has a 32 bit IEEE formatted floating point number embedded in it, for example, to transmit it in IEEE format - not text format - then COP is the right way to do it. If he wants to create a text representation of the actual decimal value then he is going to have to dismantle the number into integer and fractional components, then convert it to ascii - he can't just stick the number into the string and expect so see the text characters '0' - '9'. J Toman described the basic procedure except that MOV rounds. You must compensate for this.
 
Last edited:
So if I had a float number such as 4040132 I would first divide by lets say 1000 or something that would break it down. Then move each half into an integer and reassemble into my string without the decimal. I havn't tried but I get the idea.

Thanks

Adam
 
good catch bernie

Bernie,
You are right on the money, "FB" would have to be multiplied by 10000 before it is moved into "IC"( I said that we would work with + / - 9999.9999 ).

Alaric,
Once the number is in two Integers you will use the AIC to convert to String and ACN to concatenate minus sign, whole number, decimal point and the fractional number.

Averellen,
The 4040132 exceeds the allowable range.
If your desired range was to exceed + / - 9999.9999 then you would have to come up with a whole new way of doing it.
I am sure that it can be done it's just going to be lot more involved.


This would be a nice class exersise, I am too busy to spend the time on actually programming it.
 
averellen said:
So if I had a float number such as 4040132 I would first divide by lets say 1000 or something that would break it down. Then move each half into an integer and reassemble into my string without the decimal. I havn't tried but I get the idea.

Thanks

Adam

Dividing 4040132 by 1000.0 would give you 4040.132. When you do the division use 1000.0 to force the type cast before the division operation, not just when the result is stored. While on the AB plc this really doesnt matter, it does matter on some PLCs, so it just a good practice. After you seperate the whole number from the fraction and then remultiply the fraction by 1000, you will have 4040 and 132 in two different registers. You can then assemble a string to represent these in text without the decimal point. What happens if the number is less than one thousand, are leading zeros OK? And will your floating point number always be a whole number as in your example? If its is 4040132.555555 then you will begin to loose precision if you use only two integers (thats why Jiri was talking about the range of +/-9999.9999). Theoretically you could convert as large a number as you wish by using more than two integers to dismantle the float, but that will start to get a lot more complicated.


The reason I was originally wondering if you were wanting to preserve the originaly IEEE binary format or use a text format is because I am running an application in a Modicon PLC here where we are reading four floating point numbers via RS-232 through the ABM. We receive a 17 byte string from the instrument which contains the four floating point numbers in binary. We just copy the first four bytes to the first floating point, the next four to the second, and so on. The last byte is the string terminator.

Here are two notes from AB on how floating point numbers are stored in your SLC.
http://domino.automation.rockwell.com/applications/kb/RAKB.nsf/0/85C2838CBFC456D285256AFB00695031?OpenDocument

http://domino.automation.rockwell.com/applications/kb/RAKB.nsf/0/C9B5EA04DF66D67B85256AFB006AAE6C?OpenDocument

This one shows how to convert a floating point to two intergers.
http://domino.automation.rockwell.com/applications/kb/RAKB.nsf/0/1D2DAB8E92220BD985256BCE005825F1?OpenDocument

Here is an example that shows you how to get an unsigned integer, allowing you to use numbers in the 0-65535 instead of -32768 to 32767. If your number will always be positive then this may help.
http://domino.automation.rockwell.com/applications/kb/RAKB.nsf/0/f0cf8ffbdc7d1b7785256afb0068690d/Solution/0.386?OpenElement&FieldElemFormat=gif

And here is a tech note on converting a string to a floating point number - opposite of what you are trying to do, but it may give you some ideas.
http://domino.automation.rockwell.com/applications/kb/RAKB.nsf/0/B22B7C707D7A1BA185256AFB00725E3D?OpenDocument
 

Similar Topics

Hi, I need to convert float to string in RS Logix 5000. I am using Comactlogix L32 E Processor. Pls advise how to convert values between 1.00...
Replies
3
Views
3,820
I try to write a float in a ASCII string with RSLogix500. Does someone already encountered the problem or would have an idea?
Replies
2
Views
1,941
I have a number that is too big to hold in an integer file and must be in a float. Now I have to convert that number to a string to sent to a...
Replies
3
Views
8,510
Does anyone know a easy method of converting a string to a float. The Ascii To Integer is no good because some of the numbers are above 32767...
Replies
2
Views
3,196
Hi. I'm using a Modbus ProSoft where I basically map data to a big INT array. Example, where GX_I_63HPU is a REAL, MNETC.DATA.WriteData[200] an...
Replies
21
Views
503
Back
Top Bottom