Monokot Server 1.x
  • What is Monokot Server?
  • Quickstart
  • 🐸Basics
    • Supported OS and Hardware
    • Installation
    • Licensing
    • OPC UA
      • UA TCP Endpoint
      • UA Settings
      • Client Certificates
      • Aliases
      • Access to Object Settings
      • Troubleshooting
    • Security Certififcate
    • Users & Roles
    • Administrator GUI
      • Event Log
      • Users in Monokot Server Administrator
      • Roles in Monokot Server Administrator
    • Startup Parameters
  • 🦊Tags & Devices
    • Devices
      • Diagnostics
      • Devices in the Monokot Server Administrator
    • Tags
      • Parameters
      • Change Trigger
      • Tags in Monokot Server Administrator
        • Go Online
        • Group Action
        • Import & Export
    • Modbus Connectivity
      • Parameters
      • Addressing
      • Diagnostics
    • Siemens Connectivity
      • Parameters
      • Addressing
      • Access to DBs area in S7-1200/S7-1500
    • IEC 60870-5-104 Connectivity
      • Parameters
      • Addressing
      • Time Conversion
      • Diagnostics and Commands
    • OPC UA Connectivity
      • Parameters
      • Addressing
      • Diagnostics
      • How to: Importing OPC UA items
      • How to: Pulling Security Certificate
    • InfluxDB Connectivity (Connector)
      • Parameters
      • Addressing
      • Query Result and Data Mapping
      • Diagnostics
      • How to: Configure for InfluxDB 2.x
    • SNMP Connectivity
      • Parameters
      • Addressing
      • UDP Considerations
  • 🐺Time Series & Stores
    • Stores
      • Backlog
      • Diagnostics
      • Stores in Monokot Server Administrator
    • Time Series
      • Parameters
      • Deadband
      • Sampling
      • Last Sample Repeat
      • Time Series in Monokot Server Administrator
        • Group Action
        • Import & Export
    • InfluxDB Connectivity (Store)
      • Parameters
      • Addressing
      • Data Structure
      • About Metadata
      • Diagnostics
      • How to: Configure for InfluxDB 2.x
    • PostgreSQL Connectivity
      • Parameters
      • Addressing
      • Database Design
      • Data Compression
    • REST Connectivity
      • Parameters
      • Addressing
      • Message Script
      • RestRequestMessage
      • DataContext
      • TimeSeries
  • 🐻Scripts
    • Overview
    • Expression
      • Parameters
      • Import & Export
      • Go Online
    • Programming Examples
      • How to: Calculate Arithmetic Mean
      • How to: DoNothing
      • How to: Writing to Tag
      • How to: Inverting Bits
      • How to: Execute SQL
      • How to: Run Ping
      • How to: Do Simulation
      • How to: String Formatting
      • How to: OPC UA Method
      • How to: Initialize Device Settings from File
    • API
      • Bundle
      • BundlePair
      • Context
      • DataMap
      • DataMapPair
      • DataTriggerInfo
      • Expression
      • MosCrypto
      • MosDirectories
      • MosFiles
      • MosOdbc
      • MosOdbcReader
      • MosProcess
      • MosProcessExecuteResult
      • MosText
      • MosUtils
      • ValueState
Powered by GitBook
On this page
  1. Scripts
  2. Programming Examples

How to: Inverting Bits

PreviousHow to: Writing to TagNextHow to: Execute SQL

Last updated 2 years ago

The guide demonstrates how to create an expression that will invert the bits of a given tag. Before starting, create a local Modbus device, a tag with the name source_tag and data format WORD (16-bit unsigned integer).

Open Monokot Server Administrator, double-click Scripts on the Server Explorer pane and go to the Modules tab. Click the New Module button and name the module BitsOp. Insert the following code into the code editor:

function bit_test(num, bit){
    return ((num>>bit) % 2 != 0)
}

function bit_set(num, bit){
    return num | 1<<bit;
}

function bit_clear(num, bit){
    return num & ~(1<<bit);
}

function bit_toggle(num, bit){
    return bit_test(num, bit) ? bit_clear(num, bit) : bit_set(num, bit);
}

Press Ctrl + Enter to apply the change in the code editor.

Thus, the following functions are defined in the BitsOp module:

  • bit_test: returns the state (true or false) of the bit number bit from the number num

  • bit_set: sets the bit number bit of the specified number num to true and returns the resulting number

  • bit_clear: resets the bit number bit of the specified number num to false and returns the resulting number

  • bit_toggle: inverts the bit number bit of the specified number num and returns the resulting number

Go to the Expressions tab and click the New Expression button. For the expression that appeared, set the name Inverse and specify the data type WORD (16-bit unsigned integer). Insert the following code into the expression code editor:

const NUMBER_OF_BIT = 16;

var value = context.data['source_tag_trig'].state.value;

for (var i = 0; i < NUMBER_OF_BIT; i++)
  value = bit_toggle(value, i);

return value;

Press Ctrl + Enter to apply the change in the code editor.

While in the code editor you can check the syntax by pressing the F9 key

Go to the Triggers tab and click Add Data.... Select the previously created tag (source_tag) as the data source for the trigger. Enter source_tag_trig as the name (key) of the trigger.

Go to the Imports tab and click the checkbox of the BitsOp module.

In order for the changes to take effect on the server, click the Sync button. If you switch to Go Online mode, you will see a picture similar to the following (in my case, the number 21845 is inverted).

🐻