Sign in to follow this  
Followers 0
ECSI

Convert 4 bytes of SINT to Decimal Value

9 posts in this topic

I have a CompactLogix connected to a Festo Servo Drive over Ethernet I/P. The data coming back from the servo is in signed integer (SINT) format. There are four bytes of data that tell the PLC the position of the unit: Shuttle:I.Data[4] = 112 (01110000) Low Byte Shuttle:I.Data[5] = -108 (10010100) Shuttle:I.Data[6] = 0 Shuttle:I.Data[7] = 0 High Byte The above data should convert to a value of 38000 which means 38.000 inches. For the life of me I cannot figure out how to convert the above data to give me 38000. Is there an easy way? Once I know how to convert this, then I will need to do the reverse function and convert a new target value, ie 40000, to SINT format and send it to the servo. The target and feedback positions will always be positive values. The SINT format was created when loading the EDS file of the Festo Drive Any help is appreciated. Thanks

Share this post


Link to post
Share on other sites
Using My_DINT as a target COP Shuttle:I.Data[4] My_DINT 1

Share this post


Link to post
Share on other sites
Well your data is "broken" into 8 bit words, so the binary to decimal conversion for your first word (Shuttle:I.Data[4]) is 112 the conversion for your second word is 148 (not -108 as shown) . If you multiply your second word by 256 (8-bit) 148 * 256 = 37888, then ADD the value of your first word: 37888 + 112 = 38000

Share this post


Link to post
Share on other sites
I see how you did that but the PLC sees the second word as a signed integer value -108 since the leftmost bit is a 1. Even though as an unsigned integer it would read 148.

Share this post


Link to post
Share on other sites
It shows up as signed because the data type is SINT, so it interprets the most significant bit as the negative sign (that's the quick and dirty but not 100% accurate explanation). The COP instruction is simply a bit mapping from one location to another. A SINT is 8 bits and a DINT is 32 bits. So the four 8-bit SINTs are being mapped into the one 32-bit DINT. So the most significant bit of the second SINT is now in the middle of the DINT, and no longer interpreted as a negative sign. That make sense? Messaging typically forces you to pick a specific data type and make everything fit. Often this is done with the INT data type (16-bit), but occasionally (as in this case) is done with SINT. The Festo is taking the 32-bit data and breaking it into 8-bit chunks so that it matches the pre-determined data type for the messaging. You need to re-assemble it at the other end to make sense of it.

Share this post


Link to post
Share on other sites
Responding to the second part of your question - assuming your out data is in a DINT - let's say My_Target_DINT COP My_Target_DINT Shuttle:O.Data[x] 4 (x being the first of 4 indexes) will distribute the DINT to the 4 SINTs. As another note you may want to consider using CPS instead of COP in each movement of data. Because of the asynchronous nature of I/O data transfer the use of CPS prevents one from happening in the middle of the movement of the 4 bytes which may yield temporairly mixed results. Edited by b_carlton

Share this post


Link to post
Share on other sites
Not sure if this will work, I'm not able to try it at the moment. Steve

Share this post


Link to post
Share on other sites
Nothing wrong with b_carlton's suggestion to use COP or more preferably CPS. This is the fastest and cleanest way to do this, and avoids any possible problems with "interrupting" tasks, as mentioned. The main thing to remember is the Length setting, which is the number of elements, of the destination data-type. The cop(cps) mulitiplies this length by the number of bytes of the destination data-type, to determine how many bytes of data to quickly copy from one memory area to another. So ... CPS Shuttle:I.Data[4] My_DINT 1 .... length is 1 because the destination data-type is 1 DINT element - 4 bytes are copied. CPS My_DINT Shuttle:I.Data[4] 4 .... length is 4 because the destination data-type is a SINT (one byte) - 4 bytes are copied.

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