Sign in to follow this  
Followers 0
TL7791

INT to HEX to DINT and Confusion

12 posts in this topic

Hello all, I have been given a 'unique' project that I'm hoping someone can help me shed some light on. I am using an AB CompactLogix L18ER PLC I have a Generic ENet module that I am receiving data from in the form of INT (kinda). It is actually sending DINT, but the generic module has to be set up as INT in order to properly communicate. Here's my issue... Let's say I have rec'd result data. It is placed in registers GenericModule:I.Data.[0] and GenericModule:I.Data.[1] In .0 the data reads -28532 In .1 the data reads 6 The result needs to end up as 430.2202 If I get out my handy-dandy windows calc, put it in Programmer mode and perform the following steps, I get the proper result. I'm just not certain how to achieve in L5K. 1. Convert INT result of GenericModule:I.Data.[0] to HEX = 908C 2. Convert INT result of GenericModule:I.Data.[1] to HEX = 6 3. Put the calc on Dword + HEX and enter Second result then first result (Like MSD, LSD) = 6908C 4. Convert to Dec = 430220 5. Divide by result of step 4 by 1000 = 430.220 Thoughts?

Share this post


Link to post
Share on other sites
COP GenericModule:I.Data.[0] MyDINT 1 CPT MyREAL MyDint / 1000.0

Share this post


Link to post
Share on other sites
use bit field distribute instruction to build 2 INT into 1 DINT, make sure MSData and LSData are in the proper order

Share this post


Link to post
Share on other sites
This will take two BFD instructions which you might or might not want triggered on every scan

Share this post


Link to post
Share on other sites
HKU_MS's method above is how I usually do it, but just to make a small correction, the instruction is BTD not BFD ;)

Share this post


Link to post
Share on other sites
I second using the COP instruction. Since you're moving the whole DINT with one instruction it's less confusing to future programmers.

Share this post


Link to post
Share on other sites
JRoss I read this as moving two INT's GenericModule:I.Data.[0] and GenericModule:I.Data.[1] into one DINT MYDINT_1 I think the BTD instruction would be the more compact way of building MYDINT_1

Share this post


Link to post
Share on other sites
One instruction - the COP - moves the 2 INTs into the DINT. The second instruction divides the DINT by 1000.0 to produce the Real value. is your total solution using BTD to transform from the 2 INTs to the final Real less than 2 instructions?

Share this post


Link to post
Share on other sites
I admit I had to look up the BTD instruction. I've seen it before in programs I've had to modify, but I've never used it in one of my programs. It looks like it could be useful for certain applications, but since all you're doing is moving data from one place to another, a COP works and is more common so other programmers are more likely to recognize it. Plus, as Bernie said, it has the advantage of moving both INT into the DINT in one instruction (rather than two, as with the BTD), so it's the more compact solution.

Share this post


Link to post
Share on other sites
I guess if it works however I had read that the source and destination should be of the same type in COP instructions. I assumed that meant they had to be but after reading the text again "Important: the Source and Destination operands should bethe same data type, or unexpected results may occur." From RSL 5000 Instruction Manual just means things might not work out if you copy say a REAL into a DINT then try to manipulate so I now concur with JRoss and b_carlton I will try to remember this next time I run into a similar situation

Share this post


Link to post
Share on other sites
Yes, the COP instruction must be used with care. It's a bit-by-bit copy from one location to another regardless of data type. Most of the time I use it to copy user defined data types (UDT) from one location to another (e.g. a recipe file).

Share this post


Link to post
Share on other sites
Yes, the power in using the COP command is understanding exactly what it does. It is a blind copy of the bit values. The length is in terms of the target length. In this particular instance the start of the source is on the first of two INTs. The destination is a DINT. We COPy 1 of the target lengths. So two INTs which happen to be adjacent in memory get copied to 1 DINT. For efficient use learn the layout of memory of your variables. Totally different tags have no guarantee of any physical relationship to one another. But members of an array do. Study and be comfortable with the layout of memory then the COP command can be your 'friend'. Also, be aware of CPS. If it is possible that you are using physical input or HMI or networked values as the source then use CPS to ensure that the source values can't change during the operation. Edited by b_carlton

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0