Jump to content



Photo
- - - - -

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

  • Please log in to reply
7 replies to this topic

#1 Nicolas

Nicolas

    Sparky

  • MrPLC Member
  • PipPipPip
  • 14 posts
  • Location:Houston TX
  • Country:United States
    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

  • MrPLC Admin
  • 461 posts
  • Gender:Male
  • Location:Charlotte, NC
  • Country:United States
    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:

Short int.jpg
Michael
Charlotte, NC

#3 Nicolas

Nicolas

    Sparky

  • MrPLC Member
  • PipPipPip
  • 14 posts
  • Location:Houston TX
  • Country:United States
    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:

Short int.jpg


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

ScreenShot188.jpg

#4 Nicolas

Nicolas

    Sparky

  • MrPLC Member
  • PipPipPip
  • 14 posts
  • Location:Houston TX
  • Country:United States
    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:
ScreenShot189.jpg
Result:
ScreenShot190.jpg

#5 Michael Walsh

Michael Walsh

    Omron Expert

  • MrPLC Admin
  • 461 posts
  • Gender:Male
  • Location:Charlotte, NC
  • Country:United States
    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! :lookingaround:
Michael
Charlotte, NC

#6 Sergei Troizky

Sergei Troizky

    Expert

  • MrPLC Member
  • PipPipPipPip
  • 613 posts
  • Gender:Male
  • Location:Montreal
  • Country:Canada
    Canada

Posted 14 September 2012 - 06:02 PM

:-)

Attached Thumbnails

  • Short Int.jpg

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


#7 Michael Walsh

Michael Walsh

    Omron Expert

  • MrPLC Admin
  • 461 posts
  • Gender:Male
  • Location:Charlotte, NC
  • Country:United States
    United States

Posted 14 September 2012 - 07:50 PM

Ooooh! Ultra elegant! Nice!

:clap:
Michael
Charlotte, NC

#8 BITS N BYTES

BITS N BYTES

    Expert

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

Posted 17 September 2012 - 09:15 AM

Structured Text conversion of Single Signed Integer.

Attached Thumbnails

  • SingleSignedData.JPG



Download » CX-Programmer

    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