Modbus Setting Up Test Environment

Setting Up Your First Modbus Test Environment

Imagine you’re learning to drive—you don’t start on the highway. You practice in a safe, controlled environment first. The same applies to learning Modbus: a test environment lets you experiment, make mistakes, and build confidence without risking production systems.

In this guide, we’ll walk you through building a practical Modbus test setup. We’ll cover both software simulation (no hardware needed) and physical hardware setup (using USB-to-RS485 adapters). We’ll also recommend free tools to get you started.

Why You Need a Modbus Test Environment

Before diving in, let’s understand why a test environment is essential:

  • Application Development: Test your Modbus software against real or simulated devices

Recommended Free Modbus Tools

You don’t need expensive software to test Modbus. Here are our top recommendations:

1. Modbus Poll (Demo Version)

  • Download: [Modbus Poll](https://www.modbustools.com/download.html)

2. Simply Modbus TCP/RTU

  • Download: [Simply Modbus](https://www.simplymodbus.ca/)

3. pymodbus (Python Library)

  • Installation: `pip install pymodbus`

4. Modbus Scope (Optional)

  • Download: [Modbus Scope](https://github.com/yujihoribe/modbus_scope)

Part 1: Software Simulation Setup (No Hardware Needed)

Let’s start with the simplest setup—simulating both master and slave devices on your computer.

Step 1: Install Modbus Tools

1. Download and install Simply Modbus TCP from the link above

2. No additional hardware required—just a computer with Windows

Step 2: Configure Modbus TCP Slave

1. Open Simply Modbus TCP Server

2. Set the following parameters:

TCP Port: 502 (standard Modbus TCP port)

Slave ID: 1

Register Values: Enter some sample values (e.g., 100, 200, 300 in the first three holding registers)

3. Click Start Server

4. You should see “Server Running” at the bottom

Step 3: Configure Modbus TCP Master

1. Open Simply Modbus TCP Client

2. Set the following parameters:

TCP/IP Address: 127.0.0.1 (localhost)

Port: 502

Slave ID: 1

3. Under “Read Holding Registers”:

Start Address: 0

Quantity: 3

Poll Rate: 1000 ms

4. Click Connect

5. Click Read/Write

Step 4: Observe Modbus Communication

  • Try writing new values from the client to the slave

Alternative: Python Scripting with pymodbus

For those comfortable with Python, here’s a simple master-slave simulation:

“`python

Save as modbus_simulation.py

from pymodbus.server import StartTcpServer

from pymodbus.device import ModbusDeviceIdentification

from pymodbus.datastore import ModbusSequentialDataBlock, ModbusSlaveContext, ModbusServerContext

from pymodbus.client import ModbusTcpClient

import time

Set up Modbus server

store = ModbusSlaveContext(

di=ModbusSequentialDataBlock(0, [17] * 100),

co=ModbusSequentialDataBlock(0, [17] * 100),

hr=ModbusSequentialDataBlock(0, [100, 200, 300, 400, 500]), # Sample holding register values

ir=ModbusSequentialDataBlock(0, [17] * 100)

)

context = ModbusServerContext(slaves=store, single=True)

identity = ModbusDeviceIdentification()

identity.VendorName = ‘pymodbus’

identity.ProductCode = ‘PM’

identity.VendorUrl = ‘http://github.com/riptideio/pymodbus/’

identity.ProductName = ‘pymodbus Server’

identity.ModelName = ‘pymodbus Server’

identity.MajorMinorRevision = ‘2.3.0’

Start server in a thread

import threading

server_thread = threading.Thread(target=StartTcpServer, args=(context, identity), kwargs={‘address’: (‘localhost’, 5020)})

server_thread.daemon = True

server_thread.start()

print(“Modbus server started on localhost:5020”)

Wait for server to start

time.sleep(1)

Create Modbus client

client = ModbusTcpClient(‘localhost’, port=5020)

client.connect()

print(“Connected to Modbus server”)

Read holding registers

result = client.read_holding_registers(address=0, count=5, slave=0)

print(f”Holding Registers: {result.registers}”)

Write to a register

client.write_register(address=2, value=999, slave=0)

print(“Wrote 999 to register 2”)

Read again to see the change

result = client.read_holding_registers(address=0, count=5, slave=0)

print(f”Updated Holding Registers: {result.registers}”)

client.close()

“`

Run with: `python modbus_simulation.py`

Part 2: Hardware Setup with USB-to-RS485 Adapter

Once you’re comfortable with simulation, you can move to physical hardware.

Required Hardware

  • Computer: Windows or Linux

Step 1: Hardware Connections

1. Identify Adapter Pins: Most USB-to-RS485 adapters have three terminals:

– `A+` (Positive)

– `B-` (Negative)

– `GND` (Ground)

2. Connect Devices in Daisy Chain:

– Connect `A+` from the adapter to `A+` on the Modbus device

– Connect `B-` from the adapter to `B-` on the Modbus device

– Connect `GND` if available (improves noise immunity)

3. Termination Resistors:

– Add 120Ω termination resistors at both ends of the RS485 bus

– Some adapters have built-in termination resistors (check your device manual)

Step 2: Install Driver Software

1. Plug the USB-to-RS485 adapter into your computer

2. Windows usually installs drivers automatically, but check the manufacturer’s website for the latest drivers

3. Check Device Manager to confirm the COM port assignment (e.g., COM3)

Step 3: Configure Software for RTU

1. Open Simply Modbus RTU Master

2. Set the following parameters:

COM Port: The port from Device Manager (e.g., COM3)

Baud Rate: 9600 (common default)

Data Bits: 8

Parity: None

Stop Bits: 1

Slave ID: 1 (match your device’s slave ID)

3. Click Connect

4. You should now be able to communicate with your physical Modbus device

Example Setup for Two USB-to-RS485 Adapters

If you don’t have a Modbus device, you can use two USB-to-RS485 adapters to create a master-slave setup:

1. Connect both adapters to your computer

2. Assign different COM ports to each

3. Use one as master (Simply Modbus RTU Master)

4. Use the other as slave (Simply Modbus RTU Slave)

5. Connect them with RS485 cables as described above

Safety Considerations

Working with industrial equipment requires caution. Follow these safety guidelines:

Electrical Safety

  • Ground Properly: Ensure all devices share a common ground to prevent ground loops

Equipment Safety

  • Keep Workspace Clean: Avoid clutter to prevent accidental disconnections

Software Safety

  • Document Changes: Keep a log of all changes you make during testing

Troubleshooting Common Issues

| Issue | Possible Causes | Solutions |

|———–|———————|—————|

| No communication | Wrong COM port | Check Device Manager for correct COM port |

| | Incorrect baud rate | Verify device baud rate matches your settings |

| | Wrong slave ID | Check the device’s slave ID configuration |

| | RS485 wiring reversed | Swap A+ and B- connections |

| | No termination resistors | Add 120Ω resistors at both ends |

| Unstable communication | Ground loop | Ensure all devices share a common ground |

| | Electromagnetic interference | Move away from motors, transformers, or power lines |

| | Poor cable quality | Use shielded twisted-pair cables |

| Registers show wrong values | Byte ordering issue | Check if device uses big-endian or little-endian |

| | Wrong register address | Verify register map with device manual |

Next Steps After Setup

Once your test environment is working, try these exercises to deepen your understanding:

1. Experiment with Different Function Codes: Test read coils (01), read holding registers (03), write single coil (05), write single register (06)

2. Simulate Errors: Disconnect cables to see how error handling works

3. Test Different Baud Rates: Try 9600, 19200, 38400 bps to see the effect on communication speed

4. Add Multiple Devices: Expand your setup with additional Modbus devices

5. Develop a Simple Application: Write a Python script to log data from your Modbus device

Conclusion

A Modbus test environment is your safe playground for learning and experimentation. Whether you start with software simulation or jump straight to hardware, the key is to practice regularly and document your findings.

Remember: every Modbus expert started where you are now. With patience, practice, and the right tools, you’ll soon be troubleshooting Modbus systems like a pro.

Happy testing!