What is Modbus Pal?
ModbusPal It is based on Java Open source Modbus From the station (Slave) Simulator, The project is hosted in SourceForge: https://modbuspal.sourceforge.net/. Its core feature is embedded Jython (Java Version Python) The script engine dynamically generates register values, Instead of just returning pre-set static data. This means that you can simulate various behaviors of real industrial equipment——Seasonal fluctuations of temperature sensors, Acceleration curve of frequency converter, Even analog signals with noise.
In development Modbus When using the main station program, Engineers often face the dilemma of 'no real hardware'. ModbusPal It is designed to solve this problem——It can simulate dozens of different types simultaneously on a regular computer Modbus Slave station equipment, support Modbus RTU (Serial port) And Modbus TCP (Ethernet) Two transmission modes.
ModbusPal Core functional characteristics
- Dynamic register generation: Through built-in mathematical functions (Sine wave, steep hill, Random numbers) Or user-defined Python The script calculates the value of each register in real-time
- Multiple concurrent slave stations: Simultaneously simulate multiple different scenarios Slave ID of Equipment Modbus RTU/TCP Automation mechanism
- Support automatic modification of register values for scheduled tasks: Simulate changes in equipment status, Like flipping the switch value (Analog gradient, Script engine)
- Jython Embedded: Allow writing complex business logic Jython 2.x, State machine (Communication simulation, etc, Cross platform operation)
- be based on: Can run Java Swing, Windows / Linux / macOS Serial port support
- Through: The library supports physical serial communication RxTx Can connect to real, Network RS-232/RS-485 Installation
prerequisite ModbusPal
Dependence
ModbusPal Operating environment Java Or above (JRE 8 If you need to use the physical serial port function) . If you need to use the physical serial port function, Installation is still required RxTx Serial communication library.
Linux Installation
# 安装 Java 运行时
sudo apt-get update
sudo apt-get install default-jre
# 下载 ModbusPal(SourceForge 最新版)
wget -O modbuspal.jar "https://sourceforge.net/projects/modbuspal/files/latest/download"
# 启动程序
java -jar modbuspal.jar
# 如果需要串口支持,安装 RxTx
sudo apt-get install librxtx-javaWindows Installation
Download modbuspal.jar Afterwards, If the system is already installed Java, Double click to run. If serial port function is required, Download RxTx of Documents Windows DLL Copy to (rxtxSerial.dll) , catalogue Create the first one C:Program FilesJavajrebin From the station.
Start up Modbus TCP Afterwards
The main interface is divided into a menu bar from top to bottom ModbusPal Engineering tree, Configure panel and log output area, Creating a basic slave only requires, Step. Click on the menu bar 5 In the pop-up dialog box:
- Select the protocol type as Add → Modbus Slave
- Set the listening port, Default TCP
- If multiple slave stations are needed, different ports can be assigned (Like 502) , Set the slave station address (For example 502, 503, 504)
- Right click on the newly created slave (Slave ID) , choice 1
- Set the starting address and quantity, Click on the toolbar Add → Holding Registers, Button to start the slave station
At this moment, the slave station is already in Run Port listening. Can be used TCP 502 Or, Wait for the main station tool to connect and read the register values Modbus Poll Use Modpoll Script generates dynamic data.
This is Python Killer level functionality
The following example simulates a temperature sensor ModbusPal Let the register value be in the form of a sine wave. Arrived, Fluctuations between them 20°C In 30°C Binding script in the middle:
# temperature_simulator.py
# 模拟温度传感器,值在 20.0 - 30.0°C 间以正弦波周期变化
import time
import math
def getValue(register):
"""
ModbusPal 每次读寄存器时调用此函数
register: 寄存器地址 (int)
返回值: 寄存器值 (int, 0-65535)
"""
t = time.time()
# 中心温度 25°C,振幅 5°C,周期 60 秒
temperature = 25.0 + 5.0 * math.sin(2.0 * math.pi * t / 60.0)
# 乘以 10 保留一位小数精度,如 25.3°C → 253
return int(temperature * 10)
def setValue(register, value):
"""可选:处理主站写入的命令"""
print("Register %d written with value %d" % (register, value))Right click on the target register ModbusPal Select script file: Specify the bound register address range → Bindings → Add Python Binding → After starting the slave station → Continuously read the register address using the main station tool. One can observe the sinusoidal variation of temperature values over time, Advanced scenarios, Simulate a complete multi device industrial network.
Advanced Scene: Simulate a complete multi device industrial network
In actual industrial scenarios, One RS-485 Multiple slave devices are usually mounted on the bus. We can use ModbusPal Simulate the following typical configurations:
| From the station ID | Equipment type | Register mapping |
|---|---|---|
| 1 | Temperature and humidity sensors | R0: Temperature(x10), R1: Humidity(x10) |
| 2 | Three phase electricity meter | R0: APhase voltage, R1: APhase current, R2-3: Active power(32-bit) |
| 3 | Frequency converter | R0: Operating frequency(x100), R1: Output current(x10), R2: Operating status(bitcode) |
| 4 | Liquid level meter | R0-1: Liquid level height(mm, 32-bit), R2: Temperature(x10) |
In ModbusPal Add in sequence 4 A slave station, Create corresponding register groups for each slave station, Bind registers that require dynamic values again Python script. The benefits of doing so are:: No need to purchase and connect real hardware, Multiple device communication coordination and protocol logic verification can be completed on the development machine.
Design complex simulation behaviors
Simulate the start stop and speed changes of the frequency converter
# vfd_simulator.py
# 模拟变频器:接收启停命令,转速按斜坡函数变化
import time
# 全局状态变量
target_freq = 0.0 # 目标频率 (Hz)
current_freq = 0.0 # 当前频率 (Hz)
running = False # 运行状态
RAMP_RATE = 2.0 # 斜坡速率:2 Hz/秒
last_time = time.time()
def getValue(register):
global current_freq, last_time
now = time.time()
dt = now - last_time
last_time = now
if register == 0:
# 频率寄存器:按斜坡速率逼近目标值
if abs(current_freq - target_freq) > 0.01:
if current_freq = 0:
status |= 0x02
return status
return 0
def setValue(register, value):
global target_freq, running
if register == 0:
# 主站写入控制命令
# value bit0: 启动/停止, bit1: 方向
running = bool(value & 0x01)
if not running:
target_freq = 0.0
elif register == 1:
# 主站写入目标频率(乘以 100)
target_freq = value / 100.0Simulate analog sensors with noise
# noisy_sensor.py
# 模拟带噪声的 4-20mA 压力变送器
import time
import math
import random
def getValue(register):
t = time.time()
# 基准压力:0.5 MPa,叠加小幅正弦波动
base_pressure = 500.0 # kPa
ripple = 10.0 * math.sin(2 * math.pi * t / 30.0)
# 添加随机噪声(±2 kPa)
noise = random.uniform(-2.0, 2.0)
pressure = base_pressure + ripple + noise
if register == 0:
# 压力值(kPa,整数)
return int(pressure)
elif register == 1:
# 压力值小数部分(Pa)
frac = pressure - int(pressure)
return int(frac * 1000)
return 0Combined with automated testing
ModbusPal Can be configured as a project file (.mbp Format) , Load through the command line:
# Load pre configured project files and start
java -jar modbuspal.jar --load my_project.mbp &This allows it to be integrated into automated testing processes. The typical process of testing scripts:
- Start up ModbusPal And load the testing project (Running in the background)
- Waiting for the slave station to be ready
- function Modbus Main station test cases (If used Modpoll Read and write registers)
- Verify whether the return value meets expectations
- Generate test reports
- stop it ModbusPal
ModbusPal Limitations of
use ModbusPal Attention should be paid to the following points::
- Project activity level: ModbusPal It is an early open-source project, In recent years, the frequency of updates has decreased. For some newer ones Modbus Function (Like FC43/14 Read device identification) , Support may be incomplete.
- Jython edition: Built in Jython The engine is based on Python 2.x Grammar, I won't support it f-string, type hints Wait Python 3 characteristic.
- High resolution display: be based on Java Swing of In GUI High 4K The screen may display poorly DPI Need adjustment, of Zoom settings Java Performance limit DPI Simulate a large number of slave stations.
- exceed: One (Time 20 Serial port processing may become a bottleneck) Comparison of alternative solutions, Java The highest flexibility.
support
- pymodbus (Python) : Suitable for building custom simulators, But complete code needs to be written TCP/RTU/TLS, Free platform, Slave station simulator
- ModRSsim2: Windows have But it does not support Modbus script, Commercial software GUI The most comprehensive commercial function Python Simulation tools
- Modbus Slave (But it's not free) : Newer free options Modbus The interface is more modern, Summary
- Unslave Modbus Simulator: With it, Script driven dynamic register generation capability
In open source
ModbusPal The simulator field has unique characteristics Python It is particularly suitable for the following application scenarios, Protocol debugging and verification during the development phase of the main station program without target hardware Modbus Need to simulate multiple different types of slave devices. meter: Modbus Frequency converter; Complex system integration testing (And dynamic waveforms are required, PLC, Random noise) Using slope functions and other techniques to reproduce real-life industrial field behavior scenarios; If you are developing, Upper computer software, System or IoT data collection gateway. It is a powerful tool for verifying communication logic Modbus upper computer software, SCADA System or IoT data collection gateway, ModbusPal It is a powerful tool for verifying communication logic.
Leave a Reply