How do I convert a number to two s compliment (2 s compliment) format?

How do I convert a number to two's compliment (2's compliment) format?

Observed Behavior

PLC outputs an unsigned integer (0 to 255) but you want the number to be a signed integer using 2's compliment (-127 to 127)

Solution/Workaround

In any MatrikonOPC Server you can use the Alias functionality to accomplish this, as follows:
1) Open the OPC Server GUI
2) Create a basic alias (the User Manual may assist you here) and choose the OPC tag you wish the alias to access
3) Set the scaling type to be "expression"
4) If you do not intend to write to the created alias, set the read-only option
5) Set the data type to SHORT
6) In the INPUT field type "IF(INPUT>127,(INPUT-256),INPUT)" (without the quotes)
7) If you do not intend to write back from the alias to the PLC, just put the word OUTPUT in the OUTPUT field.  If you do intend to write back, type "IF(OUTPUT<0,(OUTPUT+256),OUTPUT)" (without the quotes)

Expected Behavior

After creating the Alias, use MatrikonOPC Explorer to add both the raw tag and the alias tag.  You should see that if you change the raw tag to anything under 127, that is what you see in the alias tag.  However, if you change to above 127, you will see the number minus 256, for example if the raw value is 130, the alias should show -126.

 

MatrikonOPC

Topic Information
  • Topic #: 4164-3158
  • Date Created: 12/08/2016
  • Last Modified Since: 12/08/2016
  • Viewed: 206