ASCII WRITE and floating integers

Skidood

Member
Join Date
Oct 2016
Location
Ontario
Posts
215
Hi again,

Using RSLogix 500 with a MicroLogix 1400 and not sure yet which HMI I will be using.
I am using a serial printer connected to one of the 2 serial ports on my PLC.
From time to time, I need the system to print out a floating integer which is always changing. I was going to use the AIC instruction to convert the floating integer to a string and then an AWA instruction to send the string to the printer.
The problem is that RSLogix will not allow floating integers to be used as a source for a string. Only integers and long words are allowed.
On top of this hurdle, the floating integers I need printed must show up on the printer with one decimal place (ie 245.3)
Plus each line of print needs to also contain a 2nd (different) floating integer and a little predetermined text.
Can anyone provide some guidance on a workaround? Thanks in advance. ;)
 
Last edited:
If there isn't a better way (probably is), you can always split up the floating point into whole number and fraction, convert to integers then strings, etc.

E.g. MOV real to long to get whole number, subtract whole number from original to get fraction, multiply fraction by let's say 10000 to get 4 digits, convert that to INT. Convert ints/longs to strings, put a . in between them
 
If there isn't a better way (probably is), you can always split up the floating point into whole number and fraction, convert to integers then strings, etc.

E.g. MOV real to long to get whole number, subtract whole number from original to get fraction, multiply fraction by let's say 10000 to get 4 digits, convert that to INT. Convert ints/longs to strings, put a . in between them

My head exploded reading and trying to understand that. Ugh. (but I appreciate the reply).
Is there a way to get the HMI involved in solving this problem? The 2 different floating integers are going to be displayed with one decimal place. is there a way to get these numbers written to a string?
 
I'd try to do it all in the PLC. Much neater and you're not involving a third device that doesn't actually play any part in the scenario.

Get to know the CONCATENATE instruction (CONCAT on the Logix 5000 platform, not sure if the same holds true for Logix 500). That is basically an instruction that appends strings to one another.

So, your desired string has the following parts:
1. Predetermined text
2. Pre-decimal-place component of your first floating point value
3. Decimal point
4. Post-decimal-place component of your first floating point value
5. Carriage return
6. Predetermined text
7. Pre-decimal-place component of your second floating point value
8. Decimal point
9. Post-decimal-place component of your second floating point value

Break it up into those sections.

Items 1 & 6 you presumably already have ("pre-determined" text). Let's store them in ST9:1 and ST9:6 to make life easy.

Items 2 & 7. To get the pre-decimal-point part of a floating point number, you would truncate it and store it in an integer data type (as opposed to rounding it). In Logix 5000, you can truncate using the expression "TRN(Floating_Point_Value)" inside a CPT instruction. There may also be a standalone instruction, or a different way of doing it in RSLogix 500. Have a play with it, and then once you have a whole number in an integer, you can convert it to a string. Let's put them in ST9:2 and ST9:7.

Items 3, 5 & 8. Let's put a decimal point in ST9:3 and ST9:8, and a carriage return in ST9:5, and then go to the pub.

Items 4 & 9 are a little bit more fun, so it's a good thing we've been to the pub and had something to take the edge off. When you truncated your values in a previous step, you ended up with an integer that represents the "whole number" component of your FP value. Let's say your FP value was 123.4567 - somewhere, you have an integer reading just "123", which you converted to a string. If you subtract this integer from your initial FP value (123.4567 - 123), you now have 0.4567. Multiply this by 10, and now you have 4.567. Truncate this, and now you have simply 4 - which is the decimal place value you were after all along. Let's stick these two values in ST9:4 and ST9:9 and go back to the pub.

Once we've been at the pub long enough that smashing half a dozen or so strings together into one mega-string and sending it to a printer seems like a good idea, let's break out our new friend the CONCATENATE instruction, and stitch ST9:1 through ST9:9 into one big string, and send it off to the printer. If you've done it right, you'll hopefuly end up with a printout something like:
Code:
BEERS THAT I CURRENTLY HAVE: 0.0
BEERS THAT I WOULD LIKE TO HAVE: 999.9
🍻
 
Amazing. I had a good laugh too. Thank you mate! I will digest that after the beer has been processed by my liver. But I popped back on here to post this:

I was thinking I could take my FP and multiply it by 100 and store the result in an integer file. I assume (but I'm not sure) that doing this will simply chop off anything after the decimal place. Then I can create a string using that 5 digit number. Then use the ASCII write instructions to send to the printer the following bits of data:

1. The first 3 digits in the string
2. a decimal place
3. the 4th digit in the string.
It would be nice if the processor performed rounding somewhere along the line. That's why I included the 5th digit here in my ramblings. I need to do some more reading, dammit.

BTW yes RSLogix 500 has the Concatenate instruction. Who the hell came up with that term? Someone looking for a raise at Rockwell? "Combine" would have sufficed.
 
Last edited:
No problem!

Yes, your method above is more or less what I was suggesting - but slightly trickier in that you've still got to insert the decimal place in the right spot once you convert to a string. Easier to just split the two parts out entirely, and stitch them back together with a DP in between.

Rounding is also a good idea, perhaps once you multiply your 0.456 by 10, instead of truncating it, you could round it. So instead of 4.56 becoming 4, it could become 5. But...

WARNING! Different PLC's handle rounding in different ways. Test thoroughly before you settle on a method. One example that catches a lot of people out - in a Control Logix, it rounds down below 0.5, up above 0.5, or if exactly 0.5, rounds to the nearest even number. So:
1.5 rounds to 2
2.5 also rounds to 2
3.5 rounds to 4
4.5 also rounds to 4

I don't believe Micrologix behaves in the same way - but my point is, test extensively on every possible scenario you can imagine before you decide on your approach for getting your "rounded" value, and don't assume that the same approach will work in a different PLC.

I *think* that storing a FP value in an integer will round, rather than truncate - but again, test test test!

As for "concatenate" - it's not just a rockwell thing. It's actually a legitimate English word meaning "to link things together in a chain or series". In the context of joining strings together in programming, that is the correct term to use 👨🏻‍🏫
 
Last edited:
No problem!

Yes, your method above is more or less what I was suggesting - but slightly trickier in that you've still got to insert the decimal place in the right spot once you convert to a string. Easier to just split the two parts out entirely, and stitch them back together with a DP in between.

Rounding is also a good idea, perhaps once you multiply your 0.456 by 10, instead of truncating it, you could round it. So instead of 4.56 becoming 4, it could become 5. But...

WARNING! Different PLC's handle rounding in different ways. Test thoroughly before you settle on a method. One example that catches a lot of people out - in a Control Logix, it rounds down below 0.5, up above 0.5, or if exactly 0.5, rounds to the nearest even number. So:
1.5 rounds to 2
2.5 also rounds to 2
3.5 rounds to 4
4.5 also rounds to 4

I don't believe Micrologix behaves in the same way - but my point is, test extensively on every possible scenario you can imagine before you decide on your approach for getting your "rounded" value, and don't assume that the same approach will work in a different PLC.

I *think* that storing a FP value in an integer will round, rather than truncate - but again, test test test!

As for "concatenate" - it's not just a rockwell thing. It's actually a legitimate English word meaning "to link things together in a chain or series". In the context of joining strings together in programming, that is the correct term to use 👨🏻‍🏫


I use the "Concatenate" command in Excel all the time to build scripts for indirect tags.
Great way to combine cell info.
 
Last edited:
I just confirmed my system will round off when sending a FP to an integer file. And yes I will test things out.
I appreciate the help very much. I will now concatenate the beer bottle and my lips.
Peace to all.
 

Similar Topics

I'm using serial communication between an AB 1769-L35E PLC and a Nachi robot. 90%+ of the time the ASCII Read and Write commands work perfectly...
Replies
0
Views
1,799
Hey Guys, Thanks in advance for any help. We are converting our Compactlogix L32E application into the 1769-L18ER-B1BB. One major hurdle left...
Replies
8
Views
6,412
Hi Everybody, I need to know, how to programming read and write "Hex Value" to the equipment by port RS-232 "Ascii" (CJ1W-SCU41-V1 Unit ID.1) of...
Replies
0
Views
1,073
Hi All, I wrote some code for a small hydro scheme a couple of months ago. Part of the project was to read data from a grid analyser via RS232...
Replies
0
Views
1,651
Hi guys I have a big work cell that has 2 turntables and 4 fixtures, we have the Alpha signs(like you see in the restaurants the scrolling ones)...
Replies
8
Views
6,498
Back
Top Bottom