Jump to content

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


Recommended Posts

Posted
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?

Posted
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:
Posted
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...

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
×
×
  • Create New...