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

7 replies to this topic

### #1 Nicolas

Nicolas

Sparky

• MrPLC Member
• 14 posts
• Location:Houston TX
• Country:United States

Posted 13 September 2012 - 05:40 PM

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?

### #2 Michael Walsh

Michael Walsh

Omron Expert

• 611 posts
• Gender:Male
• Location:Charlotte, NC
• Country:United States

Posted 13 September 2012 - 07:01 PM

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:

Michael
Charlotte, NC

### #3 Nicolas

Nicolas

Sparky

• MrPLC Member
• 14 posts
• Location:Houston TX
• Country:United States

Posted 14 September 2012 - 09:06 AM

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:

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

### #4 Nicolas

Nicolas

Sparky

• MrPLC Member
• 14 posts
• Location:Houston TX
• Country:United States

Posted 14 September 2012 - 10:24 AM

Here's the final answer attached for anyone interested, easy to put into a little function block:

Result:

### #5 Michael Walsh

Michael Walsh

Omron Expert

• 611 posts
• Gender:Male
• Location:Charlotte, NC
• Country:United States

Posted 14 September 2012 - 02:43 PM

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!
Michael
Charlotte, NC

### #6 Sergei Troizky

Sergei Troizky

Expert

• MrPLC Member
• 650 posts
• Gender:Male
• Location:Montreal

Posted 14 September 2012 - 06:02 PM

#### Attached Thumbnails

Edited by Sergei Troizky, 14 September 2012 - 06:06 PM.

### #7 Michael Walsh

Michael Walsh

Omron Expert

• 611 posts
• Gender:Male
• Location:Charlotte, NC
• Country:United States

Posted 14 September 2012 - 07:50 PM

Ooooh! Ultra elegant! Nice!

Michael
Charlotte, NC

### #8 BITS N BYTES

BITS N BYTES

Expert

• MrPLC Member
• 607 posts
• Gender:Male
• Location:Miami Beach FL
• Country:United States

Posted 17 September 2012 - 09:15 AM

Structured Text conversion of Single Signed Integer.

#### Attached Thumbnails

No categories found.

### Store » CX-Programmer

No categories found.

### Articles » CX-Programmer

No categories found.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users