# modbus协议tcp客户端 **Repository Path**: daxin-school/tcp-modbus-master ## Basic Information - **Project Name**: modbus协议tcp客户端 - **Description**: modbus协议tcp客户端,基于netty构建,支持同步与异步api,性能非常好,使用简单方便 - **Primary Language**: Java - **License**: LGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 7 - **Created**: 2024-01-22 - **Last Updated**: 2025-06-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # tcp-modbus-master * 基于netty与reactor构建的高性能modbus-tcp客户端项目 * 所有功能均支持同步与异步调用 >> 同步读线圈|异步读线圈
>> 同步读离散量输入|异步读离散量输入
>> 同步读保持寄存器|异步读保持寄存器
>> 同步读输入寄存器|异步读输入寄存器
>> 同步写多个线圈|异步写多个线圈
>> 同步写多个寄存器|异步写多个寄存器
>> 同步写单个线圈|异步写单个线圈
>> 同步写单个寄存器|异步写单个寄存器
## 同步与异步的API ```java @Slf4j public class ModbusClientTest { /** * 获取客户端连接 */ public ModbusClient getModbusClient() throws ConnectionException, InterruptedException { ModbusClient modbusClient = ModbusClientBuilder.newBuilder() .setHost("127.0.0.1") .setPort(502) .setEnableLog(true) .build(); modbusClient.connect(false); return modbusClient; } public void logResponse(ModbusRsp response) { if(response.getFuncCode().isError()) { log.info(response.getExceptionMessage()); } else { log.info(response.toString()); } } public void logException(Throwable t) { log.warn(t.getMessage()); } /** * 异步读线圈 */ @Test public void readCoilsAsyncTest() throws ConnectionException, InterruptedException { ModbusClient client = getModbusClient(); for (int i = 0; i < 10; i++) { client.readCoilsAsync(0, 10) .subscribe(this::logResponse, this::logException); } Thread.sleep(30000); } /** * 同步读线圈 */ @Test public void readCoilsTest() throws ConnectionException, InterruptedException, ExecutionException { ModbusClient client = getModbusClient(); CompletableFuture future = client.readCoils(0, 10); logResponse(future.get()); } /** * 异步读离散输入 */ @Test public void readDiscreteInputsAsyncTest() throws ConnectionException, InterruptedException { ModbusClient client = getModbusClient(); for (int i = 0; i < 100; i++) { client.readDiscreteInputsAsync(0, 10).subscribe(this::logResponse, this::logException); } Thread.sleep(20000); } /** * 同步读离散输入 */ @Test public void readDiscreteInputsTest() throws ConnectionException, InterruptedException, ExecutionException { ModbusClient client = getModbusClient(); for (int i = 0; i < 10; i++) { CompletableFuture future = client.readDiscreteInputs(0, 10); logResponse(future.get()); } } /** * 异步读保持寄存器 */ @Test public void readHoldingRegistersRequestAsyncTest() throws ConnectionException, InterruptedException { ModbusClient client = getModbusClient(); for (int i = 0; i < 10; i++) { client.readHoldingRegistersRequestAsync(0, 10).subscribe(this::logResponse, this::logException); } Thread.sleep(20000); } /** * 同步读保持寄存器 */ @Test public void readHoldingRegistersRequestTest() throws ConnectionException, InterruptedException, ExecutionException { ModbusClient client = getModbusClient(); for (int i = 0; i < 10; i++) { CompletableFuture future = client.readHoldingRegistersRequest(0, 10); logResponse(future.get()); } } /** * 异步读输入寄存器 */ @Test public void readInputRegistersRequestAsyncTest() throws InterruptedException, ConnectionException { ModbusClient client = getModbusClient(); for (int i = 0; i < 10; i++) { client.readInputRegistersRequestAsync(0, 10).subscribe(this::logResponse, this::logException); } Thread.sleep(20000); } /** * 同步读输入寄存器 */ @Test public void readInputRegistersRequestTest() throws InterruptedException, ConnectionException, ExecutionException { ModbusClient client = getModbusClient(); CompletableFuture future = client.readInputRegistersRequest(0, 10); logResponse(future.get()); } /** * 异步写单个线圈 */ @Test public void writeSingleCoilRequestAsyncTest() throws InterruptedException, ConnectionException { ModbusClient client = getModbusClient(); client.writeSingleCoilRequestAsync(3, true).subscribe(this::logResponse, this::logException); Thread.sleep(2000); } /** * 同步写单个线圈 */ @Test public void writeSingleCoilRequestTest() throws InterruptedException, ConnectionException, ExecutionException { ModbusClient client = getModbusClient(); CompletableFuture future = client.writeSingleCoilRequest(3, false); logResponse(future.get()); } /** * 异步写单个寄存器 */ @Test public void writeSingleRegisterRequestAsyncTest() throws InterruptedException, ConnectionException { ModbusClient client = getModbusClient(); client.writeSingleRegisterRequestAsync(2, 255).subscribe(this::logResponse, this::logException); Thread.sleep(2000); } /** * 同步写单个寄存器 */ @Test public void writeSingleRegisterRequestTest() throws InterruptedException, ConnectionException, ExecutionException { ModbusClient client = getModbusClient(); CompletableFuture future = client.writeSingleRegisterRequest(3, 255); logResponse(future.get()); } /** * 异步写多个线圈 */ @Test public void writeMultipleCoilsRequestAsyncTest() throws InterruptedException, ConnectionException { ModbusClient client = getModbusClient(); BitSet bitSet = new BitSet(); bitSet.set(0, 5, true); client.writeMultipleCoilsRequestAsync(1, 5, bitSet).subscribe(this::logResponse, this::logException); Thread.sleep(2000); } /** * 同步写多个线圈 */ @Test public void writeMultipleCoilsRequestTest() throws InterruptedException, ConnectionException, ExecutionException { ModbusClient client = getModbusClient(); BitSet bitSet = new BitSet(); bitSet.set(0, 5, false); CompletableFuture future = client.writeMultipleCoilsRequest(1, 3, bitSet); logResponse(future.get()); } /** * 异步写多个寄存器 */ @Test public void writeMultipleRegistersRequestAsyncTest() throws InterruptedException, ConnectionException { ModbusClient client = getModbusClient(); int[] registers = new int[2]; registers[0] = 255; registers[1] = 128; client.writeMultipleRegistersRequestAsync(1, registers).subscribe(this::logResponse, this::logException); Thread.sleep(2000); } /** * 同步写多个寄存器 */ @Test public void writeMultipleRegistersRequestTest() throws InterruptedException, ConnectionException, ExecutionException { ModbusClient client = getModbusClient(); int[] registers = new int[2]; registers[0] = 128; registers[1] = 255; CompletableFuture future = client.writeMultipleRegistersRequest(1, registers); logResponse(future.get()); } } ``` ## 技术交流 qq群: 517896305 email: 183615215@qq.com