Modbus Tutorials
Modbus tutorials for FairCom Edge
This page will contain tutorials on how to connect FairCom Edge to Modbus
Ensure the FairCom server is installed and running.
Confirm server access by running the FairCom API Explorer (see API Explorer to learn about the user interface).
Complete the tutorial requirements before this procedure.
Ensure that the Modbus service is enabled in the FairCom Edge server.
Navigate to and open the
services.json
file in theconfig
folder.Ensure the
"enabled"
property is set totrue
."integrationServices": [ { "serviceName": "modbus", "serviceLibrary": "modbusservice.dll", "enabled": true }
Save any changes.
Launch the open-source Modbus simulator that is included with the package to change the simulated data.
Open a command prompt.
Navigate to the simulator in
<FairCom Folder>/server/modbus/test/<OS folder>/diagslave.<#.##>/<OS version folder>/
.Run the following command at the command prompt to start the Modbus simulator that will answer Modbus requests.
diagslave -m tcp -p 1505
Connect to the
"diagslave"
and set the defined Modbus data item.Open another command prompt.
Navigate to
<FairCom Folder>/server/modbus/test/<OS folder>/modpoll-<#.##>/<OS version folder>/
.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.
Complete the tutorial requirements before this procedure.
Start the FairCom browser-based tools.
The tools icons appear.
Select and log into Data Explorer.
The Data Explorer interface window appears.
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.
Select Hub API from the Select API dropdown menu.
Select
createInput
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "ReplaceWithValidToken", "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": { } } }
Click Apply defaults to JSON request () to replace the
"authToken"
value with the valid one from your session.Click Send request () to issue the JSON-based request.
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Start the FairCom Data Explorer.
Navigate to and select modbustabletcp in the Server navigation window through
faircom>admin>Tables
.Click the Table Records tab ().
Note
The table records are displayed as one record per row.
Select a record and click the
source_payload
field.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.
Select Select API dropdown menu.
from theSelect
"createTransform"
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "ReplaceWithValidToken", "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" } ] } } ] } }
Click Send request ().
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
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.
Select Hub API from the Select API dropdown menu.
Select
alterInput
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "", "api": "hub", "action": "alterInput", "params": { "inputName": "modbusTCP", "transformName": "transform_modbus1" } }
Click Send request ().
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
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
Select the faircom database in the Server navigation window.
Select Connect from the JSON Actions dropdown menu.
Navigate to and select the modbus1 table in the Server navigation window through
faircom>admin>Tables>
.Click the Table Records tab ().
Note
The table records are displayed as one record per row.
Observe the table records.
View transformed data through a SQL query
Click the SQL Queries tab ().
Enter the following SQL query in the textbox.
select * from modbus1 where temperature > 60;
Click the Execute SQL statement button ().
Observe the response and ensure the action completed successfully.
Once Modus 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.
Select MQ API from the Select API dropdown menu.
Select
configureTopic
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "ReplaceWithValidToken", "api": "mq", "action": "configureTopic", "params": { "topic": "modbusForward", "databaseName": "faircom", "tableName": "modbusTableTCP" } }
Click Send request ().
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Start the FairCom Browser-Based tools.
Select and connect to MQ Explorer.
Select MQTT Management from the Control menu ().
Click Manage Subscriptions window.
() to open theSelect the topic you subscribed to from the Topic dropdown menu.
Click
().Close the Manage Subscriptions window.
Observe your message, displayed under Incoming Messages every 10 seconds.
Click
().Delete your subscription from the list.
Select DB API from the Select API dropdown menu.
Select the
getRecordsByIndex
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "api": "db", "authToken": "ReplaceWithValidToken", "action": "getRecordsByIndex", "params": { "databaseName": "faircom", "tableName": "modbus1", "indexName": "ts_index" } }
Click Send request ().
Observe the API Response response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.