Modbus library in Java ecosystem
Java has a wide range of applications in industrial automation server-side development, such as SCADA backend, MES data acquisition, industrial data processing services, etc. Modbus.org recommends two Java Modbus libraries - Jamod (covered) and Modbus4J (covered), in addition to JLibModbus and ModbusPal. This article focuses on JLibModbus that has not yet been covered and provides recommendations for selecting between various Java libraries.
JLibModbus - Actively Developed Java Modbus Protocol Stack
JLibModbus is a pure Java implemented Modbus protocol library, hosted on SourceForge:sourceforge.net/projects/jlibmodbus. Compared to Jamod, JLibModbus is still being continuously updated and tested, and is an "actively tested and improved" project.
Features
- Supports Modbus RTU and Modbus TCP
- Serial communication using jSSC (Java Simple Serial Connector) or RXTX library implementation
- Supports commonly used function codes (FC01-FC06, FC15, FC16, FC23)
- Master and Slave modes
- Pure Java implementation with no local library dependencies (except for serial libraries)
Maven dependencies
<!-- JLibModbus 的 Maven 集成示例 -->
<dependency>
<groupId>com.intelligt.modbus</groupId>
<artifactId>jlibmodbus</artifactId>
<version>1.2.9.7</version>
</dependency>TCP Client Example
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
public class ModbusReader {
public static void main(String[] args) throws Exception {
// 配置 TCP 参数
TcpParameters tcpParams = new TcpParameters();
tcpParams.setHost("192.168.1.100");
tcpParams.setPort(502);
// 创建 TCP 主站
ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParams);
master.connect();
// 读取保持寄存器(从站 1,地址 0,10 个寄存器)
int[] registers = master.readHoldingRegisters(1, 0, 10);
System.out.println("读取结果:");
for (int i = 0; i < registers.length; i++) {
System.out.printf(" 地址 %d: %d (0x%04X)%n", i, registers[i], registers[i]);
}
// 写入单寄存器
master.writeSingleRegister(1, 0, 1234);
master.disconnect();
}
}Java Modbus Library Comparison
| Library Name | Project Status | Transmission Mode | License | Recommended Scenarios |
|---|---|---|---|---|
| Jamod | Stop Update (SourceForge) | TCP, RTU, ASCII | BSD ish | Learning Purpose, Not Recommended for New Projects |
| Modbus4J | Stop Update (SourceForge) | TCP, UDP, RTU, ASCII | GPL | When full protocol support is required |
| JLibModbus | Active (SourceForge) | TCP, RTU | Apache 2.0 | New project preferred |
| ModbusPal | Stop updating | TCP, RTU | GPL | Simulate the scene from the station |
Advanced scenario: Building Modbus data collection service with Java
In large-scale industrial projects, a backend service is typically required to continuously poll dozens of Modbus slave devices, store the data in a database, and provide it for upper level applications to query. The following is a simplified data acquisition service framework based on JLibModbus:
public class ModbusDataCollector {
// 设备配置列表
static class DeviceConfig {
String name;
String ip;
int slaveId;
int[][] registerRanges; // {start, count}
}
public void startCollecting(List<DeviceConfig> devices) {
ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(devices.size());
for (DeviceConfig dev : devices) {
scheduler.scheduleAtFixedRate(() -> {
try {
collectDeviceData(dev);
} catch (Exception e) {
System.err.println("采集失败: " + dev.name + " - " + e.getMessage());
}
}, 0, 5, TimeUnit.SECONDS); // 每 5 秒采集一次
}
}
private void collectDeviceData(DeviceConfig dev) throws Exception {
TcpParameters params = new TcpParameters();
params.setHost(dev.ip);
params.setPort(502);
ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(params);
master.connect();
for (int[] range : dev.registerRanges) {
int[] data = master.readHoldingRegisters(dev.slaveId, range[0], range[1]);
saveToDatabase(dev.name, range[0], data);
}
master.disconnect();
}
private void saveToDatabase(String device, int startAddr, int[] data) {
// 写入时序数据库(如 InfluxDB、TDengine)或关系数据库
}
}Summary
In the Java ecosystem, JLibModbus is currently the most recommended open-source Modbus library - Apache 2.0 license (friendly to commercial projects), still actively developed and tested, with clear API design. If you are developing Modbus related data acquisition, device management, or SCADA backend services on the Java technology stack, JLibModbus is the preferred underlying protocol communication library.
Leave a Reply