Nicolas Posted September 13, 2012 Report Posted September 13, 2012 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?
Michael Walsh Posted September 14, 2012 Report Posted September 14, 2012 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:
Nicolas Posted September 14, 2012 Author Report Posted September 14, 2012 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...
Nicolas Posted September 14, 2012 Author Report Posted September 14, 2012 Here's the final answer attached for anyone interested, easy to put into a little function block: Result:
Michael Walsh Posted September 14, 2012 Report Posted September 14, 2012 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!
Sergei Troizky Posted September 14, 2012 Report Posted September 14, 2012 (edited) Edited September 14, 2012 by Sergei Troizky
BITS N BYTES Posted September 17, 2012 Report Posted September 17, 2012 Structured Text conversion of Single Signed Integer.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now