Sign in to follow this  
Followers 0
Nicolas

How to read SINT (short signed Integer) with CJ2 PLC

8 posts in this topic

I am reading some joystick data over CAN-OPEN, all analog values are 8 bit signed integers or Short Int. I know the first bit is the sign... I can't find an easy way to read that Signed 8 Bit integer value into cx-programmer, using PLC instructions/conversion. I read about the NEG(160) two'd complement but I can't get it to work. Any ideas?

Share this post


Link to post
Share on other sites
The easiest way that I can figure to do it would be to use the ANDW function to mask out bits 7-15, leaving the absolute value of the short signed integer. Then look at the sign bit, if it is on, multiply the masked value by -1, if it is off, just move the value as shown below:

Share this post


Link to post
Share on other sites
Thanks for the reply. I tried that approach but that doesn't return the correct value...this is what you must get: for 1111 1111 two's complement value= -1 but using the mask and multiply method you get -127 Unless I am doing something wrong...

Share this post


Link to post
Share on other sites
Here's the final answer attached for anyone interested, easy to put into a little function block: Result:

Share this post


Link to post
Share on other sites
Ahh, yes, you are correct. I did not spend enough time on this and made a mistake. At least I got you headed in the right direction!

Share this post


Link to post
Share on other sites
Structured Text conversion of Single Signed Integer.

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