Skip to main content

Modbus Tutorials

Modbus tutorials for FairCom Edge

Requirements:
  • Ensure the FairCom server is installed and running.

  • Confirm server access by running the FairCom API Explorer. The typical URL is https://localhost:8443/ .

This page will contain tutorials on how to connect FairCom Edge to Modbus

FairCom EdgetutorialsModbusModbus tutorials
  1. Ensure that the Modbus service is enabled in the FairCom Edge server.

    1. Navigate to and open the services.json file in the config folder.

    2. Ensure the "enabled" property is set to true.

      "integrationServices": [
      	{
      		"serviceName": "modbus",
      		"serviceLibrary": "modbusservice.dll",
      		"enabled": true
              }
      
    3. Save any changes.

  2. Launch the open-source Modbus simulator that is included with the package to change the simulated data.

    1. Open a command prompt.

    2. Navigate to the simulator in <FairCom Folder>/server/modbus/test/<OS folder>/diagslave.<#.##>/<OS version folder>/.

    3. Run the following command at the command prompt to start the Modbus simulator that will answer Modbus requests.

      diagslave -m tcp -p 1505

    4. Connect to the "diagslave" and set the defined Modbus data item.

      1. Open another command prompt.

      2. Navigate to <FairCom Folder>/server/modbus/test/<OS folder>/modpoll-<#.##>/<OS version folder>/.

      3. Run modpoll to create data.

        Note

        You can run it multiple times with different parameters to change the Modbus data.

        • modpoll -m tcp -p 1505 -a 5 -r 1200 -c 1 localhost 65

        • modpoll -m tcp -p 1505 -a 5 -r 1200 -t 0 -c 1 localhost 1

        • modpoll -m tcp -p 1505 -a 5 -r 1300 -c 2 localhost 0x47F1 0x2000

Note

The Modbus simulator will answer the Modbus TCP requests with the following data items:

  • "temperature"

    Temperature is a register containing an integer number.

  • "volume"

    Volume is a register containing a floating point number.

  • "status"

    Status is a coil containing a Boolean.

  1. Start the FairCom browser-based tools.

    The tools icons appear.

  2. Select and log into Data Explorer.

    The Data Explorer interface window appears.

  3. Click the API Explorer tab.

    The API Explorer interface opens and the Server navigation window shows that you are connected to the FairCom database and the FairCom server. Use the FairCom database or another database you create to store the "customer tables".

This procedure creates an input named "modbusTCP" that takes Modbus TCP data and stores it in JSON format in the payload field of records in a table named "modbusTableTCP" in the "faircom" database.

  1. Select Hub API from the Select API dropdown menu.

  2. Select createInput from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "api": "hub",
      "action": "createInput",
      "params": {
        "inputName": "modbusTCP",
        "serviceName": "modbus",
        "settings": {
          "modbusProtocol": "TCP",
          "modbusServer": "127.0.0.1",
          "modbusServerPort": 1505,
          "modbusDataCollectionIntervalMilliseconds": 15000,
          "propertyMapList": [
            {
              "propertyPath": "temperature",
              "modbusDataAddress": 1199,
              "modbusDataAccess": "inputRegister",
              "modbusUnitId": 5,
              "modbusDataLen": 1
            },
            {
              "propertyPath": "volume",
              "modbusDataAddress": 1299,
              "modbusDataAccess": "inputRegister",
              "modbusUnitId": 5,
              "modbusDataLen": 2
            },
            {
              "propertyPath": "status",
              "modbusDataAddress": 1199,
              "modbusDataAccess": "coil",
              "modbusUnitId": 5,
              "modbusDataLen": 1
            }
          ]
        },
        "schemaName": "admin",
        "tableName": "modbusTableTCP",
        "databaseName": "faircom",
        "retentionPolicy": "autoPurge",
        "retentionPeriod": 30,
        "retentionUnit": "day",
        "metadata": {
        }
      },
      "authToken": "replaceWithAuthTokenFromCreateSession"
    }
  4. Click Apply defaults to JSON request (Apply.PNG) to replace the "authToken" value with the valid one from your session.

  5. Click Send request (Runbutton.png) to issue the JSON-based request.

  6. Observe the response and ensure the action is completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

  1. Start the FairCom Data Explorer.

  2. Navigate to and select modbustabletcp in the Server navigation window through faircom>admin>Tables.

  3. Click the Table Records tab (Table Records Tab).

    Note

    The table records are displayed as one record per row.

  4. Select a record and click the source_payload field.

  5. Observe the JSON contents of the field. Click the "edit" icon of the source_payload field to see the content formatted as JSON.

This procedure creates a transform named "transform_modbus1" that will transform the JSON-based data from the "modbusTableTCP" table into fields in a new table called "modbus1" in the FairCom database.

  1. Select Hub API from the Select API dropdown menu.

  2. Select "createTransform" from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "api":"hub",
      "action":"createTransform",
      "params":
      {
        "transformName":"transform_modbus1",
        "transformActions":[
          {
            "inputFields":["source_payload"],
            "transformActionName":"jsonToDifferentTableFields",
            "outputFields":[
              "temperature",
              "volume",
              "status"
            ],
            "transformParams":
            {
              "targetTableName":"modbus1",
              "mapOfPropertiesToFields":[
                {
                  "propertyPath":"temperature",
                  "name":"temperature",
                  "type":"integer"
                },
                {
                  "propertyPath":"volume",
                  "name":"volume",
                  "type":"double"
                },
                {
                  "propertyPath": "status",
                  "name": "status",
                  "type": "bit"
                }
              ]
            }
          }
        ]
      },
      "authToken": "replaceWithAuthTokenFromCreateSession"
    }
    
  4. Click Send request (Runbutton.png).

  5. Observe the response and ensure the action is completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

Once the transform has been created, alter the input so that data added to the "modbusTableTCP" table will also be stored in rows in the "modbus1" table.

  1. Select Hub API from the Select API dropdown menu.

  2. Select alterInput from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
       "api": "hub",
       "action": "alterInput",
       "params": {
           "inputName": "modbusTCP",
           "transformName": "transform_modbus1"
       },
       "authToken": "replaceWithAuthTokenFromCreateSession"
    }
    
  4. Click Send request (Runbutton.png).

  5. Observe the response and ensure the action is completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

View the "modbus1" table in the "faircom" database for the transformed data.

Both of the following procedures will let you view the transformed data. You can use either of these procedures to view the transformed data.

View transformed data through the Server navigation window

  1. Select the faircom database in the Server navigation window.

  2. Select Connect from the JSON Actions dropdown menu.

  3. Navigate to and select the modbus1 table in the Server navigation window through faircom>admin>Tables>.

  4. Click the Table Records tab (Table Records Tab).

    Note

    The table records are displayed as one record per row.

  5. Observe the table records.

View transformed data through a SQL query

  1. Click the SQL Queries tab (SQL Queries Tab).

  2. Enter the following SQL query in the textbox.

    select * from modbus1 where temperature > 60;
  3. Click the Execute SQL statement button (Runbutton.png).

  4. Observe the response and ensure the action is completed successfully.

Once Modbus TCP data is flowing into a table as JSON and is being transformed into discrete fields in another table, Forward the data out over MQTT.

  1. Select MQ API from the Select API dropdown menu.

  2. Select configureTopic from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "api": "mq",
      "action": "configureTopic",
      "params":
      {
        "topic": "modbusForward",
        "databaseName": "faircom",
        "tableName": "modbusTableTCP"
      },
      "authToken": "replaceWithAuthTokenFromCreateSession"
    }
    
  4. Click Send request (Runbutton.png).

  5. Observe the response and ensure the action is completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

  1. Start the FairCom Browser-Based tools.

  2. Select and connect to MQ Explorer.

  3. Select MQTT Management from the Control menu (Control menu).

  4. Click Subscribe (Subscribe) to open the Manage Subscriptions window.

  5. Select the topic you subscribed to from the Topic dropdown menu.

  6. Click + Subscribe (+Subscribe).

  7. Close the Manage Subscriptions window.

  8. Observe your message, displayed under Incoming Messages every 10 seconds.

  9. Click Subscribe (Subscribe).

  10. Delete your subscription from the list.

  1. Select DB API from the Select API dropdown menu.

  2. Select the getRecordsByIndex from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "api": "db",
      "action": "getRecordsByIndex",
      "params": {
        "databaseName": "faircom",
        "tableName": "modbus1",
        "indexName": "ts_index"
      },
      "authToken": "replaceWithAuthTokenFromCreateSession"
    }
  4. Click Send request (Runbutton.png).

  5. Observe the API Response response and ensure the action is completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

The following example configures a Modbus connector to output data to a Modbus device. When MQTT, SQL, JSON DB, or an input connector inserts a record into the "modbus1" integration table, the FairCom server delivers the record to the Modbus connector. This example configures the Modbus connector to extract the value of the temperature property from the record’s source_payload field and write it to a device using the Modbus protocol.

  1. Select Hub API from the Select API dropdown menu.

  2. Select createOutput from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "api": "hub",
      "action": "createOutput",
      "params": {
        "outputName": "writeTemperatureToModbus",
        "serviceName": "modbus",
    
        "databaseName": "faircom",
        "ownerName": "admin",
        "tableName": "modbus1",
        
        "sourceFields": ["source_payload"],
    
        "settings": {
          "modbusProtocol": "TCP",
          "modbusServer": "127.0.0.1",
          "modbusServerPort": 1505,
          
          "propertyMapList": [
            {
              "propertyPath": "source_payload.temperature",
              "modbusDataAddress": 1199,
              "modbusDataAccess": "holdingregister",
              "modbusUnitId": 5,
              "modbusDataLen": 1
            }
          ]
        }
      },
      "authToken": "replaceWithAuthTokenFromCreateSession"
    }
  4. Click Apply defaults to JSON request (Apply.PNG) to replace the "authToken" value with the valid one from your session.

  5. Click Send request (Runbutton.png) to issue the JSON-based request.

  6. Observe the response and ensure the action is completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

This procedure creates an Input with OPC to collect data that will then be used to write to Modbus. So, FairCom Edge will work as a converter between those protocols. This tutorial is geared toward the windows operating system. However, similar operations will work on Linux and other platforms.

  1. Launch the OPC UA server simulator

    1. Open a command prompt and find the server in <faircom>/server/opc/test/open62541_v0.30/<OS version folder>/.

    2. Run the program called server_ctt to emulate an OPC UA device and answer OPC UA requests.

    3. Leave the OPC UA server running.

  2. Capture OPC UA data as JSON records in the database

    This procedure creates an input named opcDS2. The opcDS2 input takes OPC UA data and stores it in the JSON format in the payload field of records in a table. The table is named opctable2 in the FairCom database.

    1. Start the FairCom Data Explorer and go to API Explorer.

    2. Replace the JSON in the API Request editor with the following JSON:

      {
        "api": "hub",
        "action": "createInput",
        "params": {
          "inputName": "opcDS2",
          "serviceName": "opcua",
          "settings": {
            "opcServerUrl": "opc.tcp://localhost:4840",
            "opcDataCollectionIntervalMilliseconds": 5000,
            "propertyMapList": [
              {
                "propertyPath": "the_answer",
                "opcNamespace": 1,
                "opcNodeName": "the.answer"
              }
            ]
          },
          "ownerName": "admin",
          "tableName": "opcTable2",
          "databaseName": "faircom",
          "retentionPolicy": "autoPurge",
          "retentionPeriod": 30,
          "retentionUnit": "day",
          "metadata": {
          }
        },
        "authToken": "replaceWithAuthTokenFromCreateSession"
      }
      
    3. Click Apply defaults to JSON request (ApplyDefaultsToJSONRequestIcon.png) to replace the "authToken" with a valid token from your session.

    4. Click Send request (Runbutton.png) to issue the JSON-based command.

    5. Observe the response and ensure the action is completed successfully.

      Note

      An "errorCode" with a value of 0 indicates success. An "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

  3. View the captured OPC UA data in the opcTable2 table

    1. Navigate to and select opctable2 in the Server navigation window through faircom>admin>Tables>opctable2.

    2. Click the Table Records tab (Table Records Tab).

    3. Click the "source_payload" field for the record you wish to view.

    4. Observe the JSON contents of the field in the Edit Record window by clicking "open Edit" on the "source_payload" field.

      {
        "create_ts":"2025-03-27T21:11:52.117Z",
        "the_answer":42
      }
      
  4. Start the Modbus Server simulator

    1. Open a new command prompt.

    2. Navigate to the simulator in <FairCom Folder>/server/modbus/test/<OS folder>/diagslave.<#.##>/<OS version folder>/.

    3. Run diagslaveTCP.

    4. Leave diagslaveTCP running.

  5. Define an integration to write data to Modbus from the same table (createOutput)

    Similar to step 2, execute this API request:

    {
      "api": "hub", 
      "action": "createOutput", 
      "params": 
      {
        "outputName": "opc_to_modbus",
        "serviceName": "modbus",
        "tableName": "opcTable2",
        "sourceFields": ["source_payload"],
        "settings": {
          "modbusProtocol": "TCP",
          "modbusServer": "127.0.0.1",
          "modbusServerPort": 1505,
          "modbusDataAddressType": "oneBased",
          "propertyMapList": [
            {
              "propertyPath": "source_payload.the_answer",
              "modbusDataAddress": 1200,
              "modbusDataAccess": "holdingregister",
              "modbusSlave": 5,
              "modbusDataType": "int16SignedAB",
              "modbusDataLen": 1
            }
          ]
        }
      },
      "authToken": "replaceWithAuthTokenFromCreateSession"
    }
    

    This command will read opcTable2 and write "the_answer" value to modbus at address 1200.

  6. Read data at Modbus address 1200

    1. Open a new command prompt.

    2. Navigate to the simulator in <FairCom Folder>/server/modbus/test/<OS folder>/modpoll-<#.##>/<OS version folder>/.

    3. Run this:

      Modpoll -m tcp -p1505  -r 1200 -c 3 -1 -a 5 localhost
      

      You will see:

      [1200]: 42
      [1201]: 0
      [1202]: 0
  7. Change OPC data and observe that Modbus will receive a new value

    1. Open a command prompt and find the Modbus client in <faircom>/server/opc/test/open62541_v0.30/<OS version folder>/.

    2. Run the program called client to connect to server_ctt and increment the defined OPC UA variables by 2.

    3. Read Modbus data again to see the new value (Step 6)

      Modpoll -m tcp -p1505  -r 1200 -c 3 -1 -a 5 localhost
      

      You will see:

      [1200]: 44
      [1201]: 0
      [1202]: 0
Would you like to provide feedback?