digitalpetri/modbus


Modbus TCP, Modbus TCP Security, Modbus RTU on Serial, and Modbus RTU on TCP for Java 17+.

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.digitalpetri:modbus:1.2.2'
	}
	dependencies {
		implementation("com.github.digitalpetri:modbus:1.2.2")
	}
	<dependency>
	    <groupId>com.github.digitalpetri</groupId>
	    <artifactId>modbus</artifactId>
	    <version>1.2.2</version>
	</dependency>

                            
    libraryDependencies += "com.github.digitalpetri" % "modbus" % "1.2.2"
        
        

                            
    :dependencies [[com.github.digitalpetri/modbus "1.2.2"]]
        
        

Readme


Maven Central

A modern, performant, easy to use client and server implementation of Modbus, supporting:

  • Modbus TCP
  • Modbus TCP Security (Modbus TCP with TLS)
  • Modbus RTU on Serial
  • Modbus RTU on TCP

Quick Start Examples

Modbus TCP Client

var transport = NettyTcpClientTransport.create(cfg -> {
  cfg.hostname = "172.17.0.2";
  cfg.port = 502;
});

var client = ModbusTcpClient.create(transport);
client.connect();

ReadHoldingRegistersResponse response = client.readHoldingRegisters(
    1,
    new ReadHoldingRegistersRequest(0, 10)
);

System.out.println("Response: " + response);

Modbus RTU on Serial Client

var transport = SerialPortClientTransport.create(cfg -> {
  cfg.serialPort = "/dev/ttyUSB0";
  cfg.baudRate = 115200;
  cfg.dataBits = 8;
  cfg.parity = SerialPort.NO_PARITY;
  cfg.stopBits = SerialPort.ONE_STOP_BIT;
});

var client = ModbusRtuClient.create(transport);
client.connect();

client.readHoldingRegisters(
    1,
    new ReadHoldingRegistersRequest(0, 10)
);

System.out.println("Response: " + response);

Maven

Modbus TCP

<dependency>
    <groupId>com.digitalpetri.modbus</groupId>
    <artifactId>modbus-tcp</artifactId>
    <version>2.1.0</version>
</dependency>

Modbus Serial

<dependency>
    <groupId>com.digitalpetri.modbus</groupId>
    <artifactId>modbus-serial</artifactId>
    <version>2.1.0</version>
</dependency>

Features

Supported Function Codes

Code | Function | Client | Server -------- | -------- | ------ | ------ 0x01 | Read Coils | ✅ | ✅ 0x02 | Read Discrete Inputs | ✅ | ✅ 0x03 | Read Holding Registers | ✅ | ✅ 0x04 | Read Input Registers | ✅ | ✅ 0x05 | Write Single Coil | ✅ | ✅ 0x06 | Write Single Register | ✅ | ✅ 0x0F | Write Multiple Coils | ✅ | ✅ 0x10 | Write Multiple Registers | ✅ | ✅ 0x16 | Mask Write Register | ✅ | ✅ 0x17 | Read/Write Multiple Registers | ✅ | ✅

  • raw/custom PDUs on Modbus/TCP
  • broadcast messages on Modbus/RTU
  • pluggable codec implementations
  • pluggable transport implementations

License

Eclipse Public License - v 2.0