cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Modbus PLC Simulator, ThingWorx and You

No ratings

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

Comments
tbo

Your explanation could connect me to a Modbus simulation.
Thank you very much.

Please tell me.

Does Modbus packages Modbus in Thingworx support extension register (It's addressed 6 digits.)?

example: Address : 400001

TU BO, extended registers are not included by default. You might need to write your custom code for that. The handler lua file for the usage/ configuration can be found under etc\thingworx\scripts\modbus_handler.lua

Hello,

Not sure what your overall goal is but Kepware has interfaces for most any device out there, including multiple Modbus solutions..

Modbus Plus Driver for KEPServerEX | Kepware

In the microserver/etc/custom/templates/modbusExample.lua file, if there is an error about the module thingworx.shapes.filetransfer not found, just rem out line 16 where it says: require "thingworx.shapes.filetransfer". It is not needed for this exercise.

Do1

If you are using a Raspberry Pi for your edge microserver, then you should be aware that there are some extra steps that you need to take: https://community.thingworx.com/docs/DOC-3718

Hi Aanjan,

The download links are broken, could you take a look maybe?

Thanks

Version history
Last update:
‎Dec 13, 2015 11:16 PM
Updated by:
Labels (1)