Connecting a PLC over Modbus TCP

The simulator can act as a Modbus TCP server, the way a remote I/O rack does. Your PLC polls it, reading the sensors and writing the motors, and your PLC program decides what the line does.

In this guide you build a belt with a box and a sensor, and your PLC stops the belt when the sensor sees the box. It takes about fifteen minutes once your PLC is installed.

You need

1. Build the scene and give it I/O addresses

In the desktop app, start a new scene and place three parts from the library:

  1. A Belt Conveyor.
  2. A Box, sitting on the belt near the end it starts from.
  3. A Photo Eye beside the belt, about two thirds of the way along, facing across it.

Now give the two devices I/O addresses instead of names. Select each one, find the Comms group in the Inspector, and type the address into its tag field:

PartFieldType inMeaning
Photo EyePresence Tag%IX0.0An input to the PLC: true while the sensor sees the box.
Belt ConveyorRun Tag%QX0.0An output from the PLC: the belt runs while it is true.

%I is always something the scene tells the PLC (a sensor), and %Q is always something the PLC tells the scene (a motor). Only addresses travel to an external PLC. A tag that is a plain name, like Eye, is only seen by the program running inside the simulator.

The Inspector's Comms group for the Belt Conveyor, with the Run Tag set to %QX0.0.
The Belt Conveyor's Comms group, with its Run Tag on an output address.

If your scene is already wired by name, run Simulation > Assign I/O Addresses. It gives every named tag an address, starting at %IX0.0 and %QX0.0, rewrites the program in the PLC panel to declare them (Eye AT %IX0.0 : BOOL), and is one undo step. File > Export I/O List (.csv) then gives you the whole table to set the PLC up from, and File > Import I/O List… goes the other way, when the addresses are already decided.

2. Serve the I/O over Modbus TCP

Open Simulation > PLC Controllers… (or double-click the Controller node at the top of the PLC panel's organizer) and set:

  1. Runs on: An external PLC. On Play, the simulator hands the scene's I/O to your PLC instead of running the program in its own PLC panel.
  2. Under it, how the PLC connects: The PLC polls the simulator (the PLC is the Modbus master).
  3. Port: 5020 is the default. Any free port works, and the PLC needs the same number.
  4. Reachable from the network: leave it off if the PLC runs on this computer. Tick it if the PLC is a separate device, and allow the app through the Windows firewall when Windows asks.

The list underneath shows what Play will serve. For this scene it should read %IX0.0  discrete input 0 and %QX0.0  coil 0. If it says no part is wired yet, check that both parts have their address typed into the tag field.

The External PLC settings: Runs on An external PLC, connecting with The PLC polls the simulator, Port 5020, Reachable from the network off, and the list of served points: %IX0.0 discrete input 0 and %QX0.0 coil 0.
Set up to serve, with the two points this scene puts on the wire.

These settings are saved on this computer, not in the scene, so a colleague who opens the same scene keeps their own.

3. How addresses map to Modbus points

There is no mapping table to fill in. Every address lands on one Modbus point by a fixed rule, so you can work out the PLC's side from the addresses alone:

%IX1.3 is byte 1, bit 3, so it is discrete input 1 times 8 plus 3, which is 11. A REAL at %ID50 fills input registers 100 and 101, the low word in register 100 and the high word in 101. A bit: %IXb.n is point b × 8 + n %IX0.0 … %IX0.7 %IX1.0 … %IX1.7 0123456789101112131415 discrete inputs %IX1.3 = 1 × 8 + 3 = discrete input 11 A REAL: %IDn is registers 2n and 2n+1, low word first %ID50, one 32-bit REAL high word low word register 100sent first register 101sent second
AddressModbus pointThe PLCExample
%IXb.nDiscrete input b×8 + nreads it (FC2)%IX100.0 = discrete input 800
%QXb.nCoil b×8 + nwrites it (FC5 / FC15)%QX100.0 = coil 800
%IWnInput register nreads it (FC4)%IW100 = input register 100
%QWnHolding register nwrites it (FC6 / FC16)%QW100 = holding register 100
%IDnInput registers 2n, 2n+1reads them (FC4)%ID100 = input registers 200-201
%QDnHolding registers 2n, 2n+1writes them (FC16)%QD100 = holding registers 200-201

4. Set up the PLC

The program is one line of logic. It runs the belt until the sensor sees the box.

PROGRAM main
VAR
  Eye AT %IX0.0 : BOOL;
  Run AT %QX0.0 : BOOL;
END_VAR

Run := NOT Eye;
END_PROGRAM

These are the addresses the scene uses in step 1, and the ones OpenPLC gives the two Modbus points below in a new project, so the same program runs in the simulator and on the PLC. Strictly, the PLC's addresses are up to the PLC. What has to match is the Modbus point, discrete input 0 and coil 0. On another PLC, declare the variables wherever it puts those points.

Then point the PLC's Modbus TCP master at the simulator:

SettingValue
Server address127.0.0.1 on the same computer, otherwise that computer's IP address
Port5020, or the port you chose in step 2
Unit / slave ID1 (ignored)
PollRead discrete inputs, start 0, count 1, into the program's Eye
PollWrite coils, start 0, count 1, from the program's Run
Poll rateAnything under 1 s (see step 5). 20 to 100 ms is what this setup is tested at.

Example: OpenPLC

OpenPLC is free and installs on Windows without admin rights, so it is a quick way to try this without hardware. You need two downloads from Autonomy Logic's GitHub releases: the OpenPLC Editor, where you write and build the program, and the OpenPLC Runtime, the PLC that runs it. Start the runtime before you begin. These steps were followed with OpenPLC Editor 4.3.0 and Runtime 4.2.2.

  1. In the Editor, click New Project, choose PLC Project, and click Next. Give it a name, choose an empty folder, and click Next. Pick Structured Text as the language, leave the cyclic task at T#20ms, and click Create Project.
  2. The project opens on the program main. OpenPLC keeps a program's variables and its logic in two places. Click the </> button above the variables table to see them as text, and paste the VAR ... END_VAR block from the program above into it. Then paste Run := NOT Eye; into the code editor below it.
  3. Click the + button beside the project name, choose Remote Device, give it a name, choose Modbus as the protocol, and click Create. On the device's page, change Port from 502 to 5020. The IP Address (127.0.0.1) and Slave ID (1) are already right for a simulator on the same computer.
  4. Click the + beside IO Tag Mapping to add an IO group. Choose Read Discrete Inputs (FC 2), leave Offset at 0 and Length to 1, and click Create. Add a second group the same way with Write Multiple Coils (FC 15). The Address column shows where OpenPLC put them: %IX0.0 and %QX0.0 in a new project. If yours differ, use those in the program.
  5. Under Device in the project tree, open Configuration, set Device to OpenPLC Runtime v4, type 127.0.0.1 as the IP Address, click Connect, and log in to the runtime.
  6. Click the Build button in the toolbar on the far left (the arrow pointing down) and choose Build and upload. If the runtime is already running a program, confirm with Stop PLC and Continue. The console ends with PLC started. and Upload complete.
The OpenPLC Editor's Remote Device page for the simulator: IP Address 127.0.0.1, Port 5020, Slave ID 1, and two IO groups, a discrete input group at %IX0.0 and a coil group at %QX0.0.
OpenPLC Editor 4.3.0: the remote device on port 5020, with its two IO groups.

In the Editor, the play button in that toolbar starts and stops the PLC; it does not upload. The OpenPLC Simulator device the Editor selects by default builds for a simulated microcontroller, and in testing it never connected to this simulator, so choose OpenPLC Runtime v4.

5. Run it

Back in the simulator, press Play. The status line in Controller Properties (and the link status in the toolbar) shows each stage:

The box rides the belt, reaches the sensor, and the belt stops. That stop came from your PLC. The program in the simulator's PLC panel does not run while an external PLC is connected.

The box has ridden the belt conveyor and stopped in front of the photo eye.
Your PLC stopped the belt with the box at the photo eye.

To check, press Stop, which puts the box back where you placed it, and press Play again. Then stop the PLC while the box is still moving. The belt stops and the status says The PLC disconnected: its outputs are held off until it polls again. (A PLC that keeps its connection open when it stops shows The PLC stopped polling for 1.2 s: its outputs are held off until it polls again. instead, about a second later.) Start the PLC again and the belt carries on.

A PLC that stops polling sends no message saying so, and holding its last outputs would keep the line running on a value nobody is driving. So if no request arrives for 1 s, every output the PLC drives is set OFF (a coil false, a register 0) until it polls again. Real remote I/O does the same. The simulator keeps stepping, so the line coasts to a halt the way it would on the floor.

The PLC scans on its own clock, so Play with an external PLC starts at real time (1x). You can still change the speed during the run, to fill a buffer quickly or watch a handoff slowly, and the speed button turns amber: away from 1x the PLC's timers and reaction times no longer match the simulation, so the line can behave differently.

6. Analog values (optional)

A REAL travels in two registers. Add a Laser Sensor beside the belt, before the Photo Eye, and wire two more fields:

PartFieldType inModbus point
Laser SensorDistance Tag%ID50Input registers 100-101, REAL
Belt ConveyorSpeed Tag%QD50Holding registers 100-101, REAL

Then slow the belt down while the laser sees a box within a metre:

PROGRAM main
VAR
  Eye   AT %IX0.0 : BOOL;
  Run   AT %QX0.0 : BOOL;
  Range AT %ID50 : REAL;    (* metres to the box *)
  Speed AT %QD50 : REAL;    (* belt speed, m/s *)
END_VAR

Run := NOT Eye;
IF Range > 0.0 AND Range < 1.0 THEN
  Speed := 0.1;
ELSE
  Speed := 0.5;
END_IF;
END_PROGRAM

On the PLC, add a group that reads input registers from 100, two long, into %ID50, and one that writes holding registers from 100, two long, from %QD50. Set the master to low word first.

The OpenPLC Editor (4.3.0) cannot set this step up through its screens. An IO group maps each register to its own 16-bit word (%IW, %QW) and has no option for a REAL across two. The OpenPLC Runtime itself reads and writes REALs this way, low word first, which is how this step is tested, but that takes a hand-written device file rather than the Editor's Remote Device page.

The Range > 0.0 check is there because the PLC's first scan can run before its first poll, so an analog input reads 0 for a scan or two after start, and 0 looks like a real measurement. Inside the simulator this never happens, because inputs are loaded before the first scan. A real PLC needs the guard, and so does the program you will eventually put on one.

Troubleshooting

"An external PLC" is greyed out.
You are in a browser tab. Connecting needs the desktop app. You can still wire the scene and run Assign I/O in the browser, then open the scene in the desktop app.
"Could not serve: ..." when you press Play.
Something else is using the port. Pick another one in Controller Properties, and give the PLC the same number.
It stays on "no PLC connected yet".
Check the PLC is running and pointed at the right address and port. If the PLC is on another device, tick Reachable from the network, use this computer's IP address (not 127.0.0.1), and check the Windows firewall allows the app.
The PLC is connected, but the belt never runs or never stops.
The addresses on the two sides do not match. The usual causes are off-by-one (a 1-based tool shows discrete input 0 as 10001), mixing up bit and byte numbers (%IX1.0 is point 8, not 1), or a tag field left empty on a part. Controller Properties lists exactly which points it serves.
Analog values are nonsense, like a huge number or a tiny fraction.
Word order. The simulator sends the low word first; set your master to match ("swap words", "little-endian word order").
The belt stops for no reason, then starts again.
The PLC went more than 1 s without polling, so its outputs were held off. Poll faster, or check what is stalling the PLC.