Modbus is a commonly used communications protocol that allows data transfer between computers and PLCs. This is intended to be a simple guide on setting up and using a Modbus PLC Simulator with ThingWorx.

 

ThingWorx provides Modbus packages for Windows, Linux and Linux ARM. The Modbus Package contains libraries and lua files intended to be used along with the Edge Microserver.

 

Note: The Modbus package is not intended as an out of the box solution

 

Requirements:

 

  • ThingWorx Platform
  • Edge Microserver
  • Modbus Package
  • Modbus PLC Simulator

 

In this guide, a free Modbus PLC Simulator is used. Here is the direct download link for their v8.20 binary release.

 

Configuring the EMS:

 

The first step is to configure the EMS as a gateway. This is done via adding an auto_bind section in the config.json:

 

"auto_bind": [ {
    "name": "ModbusGateway",
    "gateway": true
}]

 

This creates an ephemeral Thing that only exists when the EMS is running.

 

The next step is to modify the config.lua to include the Modbus configuration. Copy over the contents of the etc folder of the Modbus Package over to the etc folder of the EMS. A sample config_modbus.lua is provided in the Modbus Package as a reference. The following code defines a Thing called MyPLC (which is a Remote Thing created on the Platform):

 

scripts.MyPLC = {
    file = "thing.lua",
    template = "modbusExample",
    identifier = "plc",
    updateRate = 2000
}

scripts.Thingworx = {
    file = "thingworx.lua"
}

scripts.modbus_handler = {
    file = "modbus_handler.lua",
    name = "modbus_handler",
    host = "localhost"
}

 

Adding 'modbusExample' to the above script enables the usage of the same located at /etc/custom/templates/. 'modbusExample' is a reference point for creating a script to add the registers of the PLC. The given template has examples for different basetypes. The different types of available registers are noted and referenced in the modbus.lua file available under /etc/thingworx/lua/.

 

Setting up the PLC Simulator:

 

Extract the mod_RSsim to a folder and run the executable. Since we are 'simulating' a PLC connection, set the protocol to Modbus TCP/IP. Change the I/O to Holding Registers (or any other relevant option), with the Address set to Dec. In the Simulation menu, select 'No animation' if you want to enter values manually or use 'Increment BYTES' to automatically generate values. This PLC Simulator will run at port 502.

 

The Connection:

 

With the EMS & luaScriptResource running, the PLC Simulator should have a connection to the platform with activity on the received/sent section.

 

Now if you open the Remote Thing 'MyPLC' in the platform, the isConnected property (under the Properties section) should be true. (If not, go back to General Information, click on Browse in the Identifier section and select 'plc').

 

Go back to the Properties section, and click on Manage Bindings. Click on the Remote tab and the list of defined properties should appear. For example, the following code from the modbusExample.lua:

 

properties.Int16HoldRegExample = {key="holding_register/1/40001?format=Int16", handler="modbus_handler", basetype="NUMBER"}

 

denotes a property named Int16HoldRegExample at register 40001. The value at the address 40001 in the PLC Simulator should correspond with the value at the platform once this property is added and the Thing saved.

 

If you are running into any errors when connecting with a Raspberry Pi, please take a look atDuan Gauche's follow up document/ guide - Using your Raspberry Pi with the Edge Microserver and Modbus