You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old April 14th, 2021, 10:23 AM   #1
Udunit
Lifetime Supporting Member
Canada

Udunit is offline
 
Udunit's Avatar
 
Join Date: Mar 2014
Location: Woodstock, Ontario
Posts: 31
INT Conversion

I have a Controllogix program that the programmer used modbus to send scale data to the PLC. We recently replaced a piece of equipment on this and I need to convert INT data to a real number. This will only be a display weight on the HMI and now control. As an example, 32767 to the PLC represents 327.67 KG's. However, when it goes to negative this continues to add the weight. Will a COP instruction do this conversion for me? IE, when I get tto -32768 the COP will make this 65535? I'm going in remote now so I want to get it right for them. Thanks,
  Reply With Quote
Old April 14th, 2021, 10:37 AM   #2
Helliana
Member
United States

Helliana is offline
 
Join Date: Nov 2011
Location: Louisiana
Posts: 740
My understanding is you are reading an unsigned integer from a scale and need to deal with when the last bit is used? The COP instruction blindly copies bit for bit, so it will not work for converting from a INT to a REAL. You will get a random looking number because some of the bits are the base number and the rest is an exponent. The newest family of Controllogix processors have support for UINT data types, so if you happen to be using one of those processors, take advantage of that. If you do not have that option, you can COPy the unsigned integer into a DINT. That would make the bit 31 the sign bit, and the first 16 bits would be treated normally. You could then scale that DINT to a REAL to move your decimal place for your display.
  Reply With Quote
Old April 14th, 2021, 10:55 AM   #3
Udunit
Lifetime Supporting Member
Canada

Udunit is offline
 
Udunit's Avatar
 
Join Date: Mar 2014
Location: Woodstock, Ontario
Posts: 31
I can use the UINT. Can I copy or move the int to the uint then? This is a really poorly written program that I inherited and the programmer has done some strange things. I'm slowly trying to clean it up.
  Reply With Quote
Old April 14th, 2021, 11:04 AM   #4
OkiePC
Lifetime Supporting Member
United States

OkiePC is offline
 
OkiePC's Avatar
 
Join Date: Mar 2005
Location: ENE of Nowhere Oklahoma
Posts: 10,943
You might be able to COP the INT to a DINT and preserve the bit pattern and not have to worry about going negative until your scale hits 21474836.47 Kg. Then DIVide the DINT by 100 to get a Real number if so desired.
__________________
You've been taken, but you don't know it yet.
  Reply With Quote
Old April 14th, 2021, 11:18 AM   #5
Mispeld
Member
United States

Mispeld is offline
 
Mispeld's Avatar
 
Join Date: Feb 2017
Location: VA
Posts: 545
In order to make it unsigned, you will need to test the INT value. If GEQ zero, move to a DINT. If LES zero, add 65536 to the INT value into the destination DINT. Then divide the (unsigned) DINT by 100.0 into the scaled REAL result.
__________________
"The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge." -Stephen Hawking
  Reply With Quote
Old April 14th, 2021, 01:04 PM   #6
daba
Lifetime Supporting Member + Moderator
United Kingdom

daba is offline
 
daba's Avatar
 
Join Date: Jul 2004
Location: uk
Posts: 5,282
Helliana and OkiePC have both suggested COPy .....

That is incorrect.

To perform data conversion from an INT to a DINT, you must use MOV.
__________________
___________________________
ControlLogix & SLC Training
a-b train ltd.
abtrain@tiscali.co.uk
www.abtrain.co.uk
tel: 07506 73 9999
Everything works with smoke. Let it out, and it stops working.

Nil Carborundem Illegitimi



  Reply With Quote
Old April 14th, 2021, 11:25 PM   #7
Helliana
Member
United States

Helliana is offline
 
Join Date: Nov 2011
Location: Louisiana
Posts: 740
MOV instruction does a data type conversion. I haven't tested to verify if MOV is considered a logical instruction or not to see if converts using the zero-fill or sign-extension method.


COP does bit by bit conversion so you don't have to worry about the conversion..
  Reply With Quote
Old April 15th, 2021, 04:32 AM   #8
daba
Lifetime Supporting Member + Moderator
United Kingdom

daba is offline
 
daba's Avatar
 
Join Date: Jul 2004
Location: uk
Posts: 5,282
Quote:
Originally Posted by Helliana View Post
MOV instruction does a data type conversion. I haven't tested to verify if MOV is considered a logical instruction or not to see if converts using the zero-fill or sign-extension method.


COP does bit by bit conversion so you don't have to worry about the conversion..
YOU CANNOT USE COP !

COP does NO conversion !

The source and destination data-types must be the same.

If you use COP to copy into a DINT (4 bytes), then 4 bytes will be copied from the source, which is only 2 bytes long.

2 bytes will be pulled from whatever follows the INT in memory.
Attached Images
File Type: jpg 2021-04-15_092801.jpg (16.1 KB, 75 views)
__________________
___________________________
ControlLogix & SLC Training
a-b train ltd.
abtrain@tiscali.co.uk
www.abtrain.co.uk
tel: 07506 73 9999
Everything works with smoke. Let it out, and it stops working.

Nil Carborundem Illegitimi



  Reply With Quote
Old April 15th, 2021, 09:28 AM   #9
Helliana
Member
United States

Helliana is offline
 
Join Date: Nov 2011
Location: Louisiana
Posts: 740
MOV will not work because it will do a data type conversion, and the number will still be negative.


COP will not work because of the different size of the data types. I thought it would copy the int and ignore the last 16 bits, but i was mistaken. HOWEVER, BTD instruction does do that.


See attached.


Attached Images
File Type: png Uint.PNG (4.6 KB, 66 views)
  Reply With Quote
Old April 15th, 2021, 09:47 AM   #10
PreLC
Member
United States

PreLC is offline
 
PreLC's Avatar
 
Join Date: Apr 2019
Location: Mars
Posts: 340
Quote:
Originally Posted by Helliana View Post
MOV will not work because it will do a data type conversion, and the number will still be negative.


COP will not work because of the different size of the data types. I thought it would copy the int and ignore the last 16 bits, but i was mistaken. HOWEVER, BTD instruction does do that.


See attached.


+1, I had to do this recently to convert SINT to DINT. BTD works. Unfortunately because of sign-extension, MOV doesn't. Unfortunately because of neighboring memory locations having garbage values, COP doesn't.
__________________
Ask not what your PLC can do for you, ask what you can do for your PLC.

Last edited by PreLC; April 15th, 2021 at 09:52 AM.
  Reply With Quote
Old April 15th, 2021, 11:01 AM   #11
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,675
@daba is right: don't use COP; use MUL instead; correct for the negative 16-bit signed values as needed; see below.

The BTD solution by @PreLC is also correct.

xxx.png


---
Take care of the bits, and the bytes will take care of themselves.
  Reply With Quote
Old April 15th, 2021, 11:45 AM   #12
Udunit
Lifetime Supporting Member
Canada

Udunit is offline
 
Udunit's Avatar
 
Join Date: Mar 2014
Location: Woodstock, Ontario
Posts: 31
Here's what I ended up doing. This was my first thought and it works well. I was seeing if there was an easy instruction to do this. Thanks all for the input!
Attached Images
File Type: jpg SCALE WEIGHT.jpg (91.3 KB, 56 views)
  Reply With Quote
Old April 15th, 2021, 11:50 AM   #13
Mispeld
Member
United States

Mispeld is offline
 
Mispeld's Avatar
 
Join Date: Feb 2017
Location: VA
Posts: 545
Quote:
Originally Posted by Udunit View Post
Here's what I ended up doing. This was my first thought and it works well. I was seeing if there was an easy instruction to do this. Thanks all for the input!
What happens when MBTU_MB_3xx[2] equals zero (or one, for that matter)?

And, technically, the result is off by 2 bits when the INT is negative.
__________________
"The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge." -Stephen Hawking

Last edited by Mispeld; April 15th, 2021 at 11:54 AM.
  Reply With Quote
Old April 15th, 2021, 11:57 AM   #14
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,675
[Update: second!]
  1. The formula on Rung 26 has a typo:
    1. The first 32767 should be 32769.
    2. Or skip those two integer adds and add 655.36 after the divide.
  2. Neither rung converts a value of 0 or 1
    1. SourceB in the GRT on Rung 25 should be -1.
    2. Although it may not matter for this process and the programmed behavior is intentional.
  3. The two rungs can be combined with a branch after the NEQ on Rung 25
    1. There is no need to test that mode integer twice.

Last edited by drbitboy; April 15th, 2021 at 12:01 PM.
  Reply With Quote
Old April 15th, 2021, 12:02 PM   #15
Udunit
Lifetime Supporting Member
Canada

Udunit is offline
 
Udunit's Avatar
 
Join Date: Mar 2014
Location: Woodstock, Ontario
Posts: 31
This is for remote display only. The 2 bit difference is so little as far as the display goes it's not an issue. The actual filling controls is direct hard contacts from the scale. It's a very messy program that I inherited from the Italian company that provided the filler. I don't believe they had any AB experience at all based on what I see in the program.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
INT to Real Datatype Conversion ss4884 LIVE PLC Questions And Answers 14 March 24th, 2009 08:55 AM
Confused "again" S7 indirect Thim LIVE PLC Questions And Answers 17 July 22nd, 2008 05:47 PM
Simatic S7: DINT to INT conversion JumpyPLC LIVE PLC Questions And Answers 4 September 12th, 2007 10:20 AM
Ideas needed for Siemens S7 program tesalmin LIVE PLC Questions And Answers 12 March 4th, 2004 01:40 PM
Ab SLC FLOAT to Double Int Conversion wirehead LIVE PLC Questions And Answers 10 March 29th, 2003 10:56 AM


All times are GMT -4. The time now is 06:00 AM.


.