RobotStudio event

Group Input negative values ?

Options
Hello,
 

As I understand, 'Group inputs' are only interpreted as positive integer values.

 

Does anybody know if there is a conversion function so that I can convert to integer with sign.

 

I send values from a Siemens PLC via Profibus/Devicenet gateway and have defined them as an 16 bit Group input.

 

I send them as 16 bit integers with sign [-32768;32767] as twos complement 

 

Do I have to write a conversion function myself or is there a smarter way?

 

Thank You,

lakris

Comments

  • msalminen
    Options
    I have use following function I made. Remember to select correct notation you need and change it inside my function.
     

    I guess you can also try to change your group input to analog input and use it directly. I think you can search this forum to find thread about it.

     

      FUNC num GetSIntGi(
        VAR signalgi giRead)

        ! Function: GetSIntGi 4.9.2006 
        ! Get signed integer from 16bit (bits 0-15) group input.
        ! Bit 15 is the sign bit, so values equal or bigger than 32768 are negatives
        ! and needed to be converted to corresponding negative decimal value
      
        VAR num notation;
        VAR num value;

        ! There are 2 interpretations for negative binary numbers.
        ! Select which notation to use for negative binary numbers.
        ! notation:=1 -- sign magnitude notation - Returned values are between -32767 to 32767
        ! notation:=2 -- two's complement notation - Returned values are between -32768 to 32767
        notation:=2;

        ! Get the integer value from group input (from PLC)
        value:=0;
        value:=GInput(giRead);
     
        ! Convert unsignad integer to the signed integer according the notation used by PLC
        IF notation=1 THEN
          ! Sign magnitude notation
          ! bit15 is sign bit, otherwise same as positive binary numbers
          IF value>32767 THEN
            value:=32768-value;
          ENDIF
        ELSEIF notation=2 THEN
          ! Two's complement notation
          ! bit15 is sign bit, negative values are inverted and added 1
          IF value>32767 THEN
            value:=value-65536;
          ENDIF
        ELSE
          ! Notation not set.
          ! Return value as it is read.
        ENDIF
        RETURN value;
      ENDFUNC

    Regards,

    Mika
  • Thilbi
    Options
    Hi,

    you didnA't need a function.
    Declare a analog input like this

         -Name "aiShiftingX" -SignalType "AI" -Unit "PB_KARTE" -UnitMap "80-95"
          -MaxPhys 3276.7 -MaxBitVal 32767 -MinPhys -3276.7 -MinBitVal -32767

    and you can use the value directly in your program. The range is in this configuration from -3276.7 up to 3276.7.

    It is possible to declare an analog input instead a groupinput.

    BR
    Thilbi

  • lakris
    Options

    Thank You very much.

    I have defined my inputs as analog inputs instead of group inputs, and it works.