easymodbus4j开发实例系列教程之1客户端master模式
easymodbus4j 开发实例系列教程之1----客户端master模式
easymodbus4j是一个高性能和易用的 Modbus 协议的 Java 实现,基于 Netty 开发,可用于 Modbus协议的Java客户端和服务器开发. easymodbus4j A high-performance and ease-of-use implementation of the Modbus protocol written in Java netty support for modbus 8 mode client/server and master/slave.
easymodbus4j 特点:1、Netty NIO high performance高性能.
2、Modbus Function sync/aync 同步/异步非阻塞。
3、Modbus IoT Data Connector Supports工业物联网平台IoT支持。
4、支持Modbus TCPModbus RTU protocol两种通信协议.
5、灵活架构,支持8种生产部署模式,自由组合,满足不同生产要求.
6、通用组件包,支持高度自定义接口.
7、完全支持Modbus TCP 4种部署模式: TCP服务器master,TCP客户端slave,TCP服务器slave,TCP客户端master。
8、完全支持Modbus RTU 4种部署模式: RTU服务器master,RTU客户端slave,RTU服务器slave,RTU客户端master。
9、友好的调试以及日志支持bititsetyteshortintfloatdouble。
10、Supports Function Codes:
Read Coils (FC1)
Read Discrete Inputs (FC2)
Read Holding Registers (FC3)
Read Input Registers (FC4)
Write Single Coil (FC5)
Write Single Register (FC6)
Write Multiple Coils (FC15)
Write Multiple Registers (FC16)
Read/Write Multiple Registers (FC23)
#Example Project Code in https://github.com/zengfr/easymodbus4j
Repositories Central Sonatype Mvnrepository easymodbus4j
artifactId/jar:easymodbus4j-core.jar Modbus protocol协议
easymodbus4j-codec.jar Modbus 通用编码器解码器
easymodbus4j.jar Modbus General/Common公共通用包
easymodbus4j-client.jar Modbus client客户端
easymodbus4j-server.jar Modbus server服务器端
easymodbus4j-extension.jar Modbus extension扩展包 ModbusMasterResponseProcessor/ModbusSlaveRequestProcessor interface
快速开发Quick Start:第一步step1 ,import jar:
maven:
<dependency>
<groupId>com.github.zengfr</groupId>
<artifactId>easymodbus4j-client</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>com.github.zengfr</groupId>
<artifactId>easymodbus4j-server</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>com.github.zengfr</groupId>
<artifactId>easymodbus4j-extension</artifactId>
<version>0.0.5</version>
</dependency>
easymodbus4j 开发实例系列教程之1----客户端master模式 实例如下:
https://gitee.com/zengfr/easymodbus4j/blob/master/easymodbus4j-example/src/main/java/com/github/zengfr/easymodbus4j/example3/Example3.java
package com.github.zengfr.easymodbus4j.example3;import com.github.zengfr.easymodbus4j.ModbusConfs;
import com.github.zengfr.easymodbus4j.client.ModbusClient4TcpMaster;
import com.github.zengfr.easymodbus4j.client.ModbusClientTcpFactory;
import com.github.zengfr.easymodbus4j.common.util.ConsoleUtil;
import com.github.zengfr.easymodbus4j.common.util.ScheduledUtil;
import com.github.zengfr.easymodbus4j.example.processor.ExampleModbusMasterResponseProcessor;
import com.github.zengfr.easymodbus4j.handle.impl.ModbusMasterResponseHandler;
import com.github.zengfr.easymodbus4j.handler.ModbusResponseHandler;
import com.github.zengfr.easymodbus4j.processor.ModbusMasterResponseProcessor;
import com.github.zengfr.easymodbus4j.sender.ChannelSender;
import com.github.zengfr.easymodbus4j.sender.ChannelSenderFactory;
import io.netty.channel.Channel;
public class Example3 {
private static ModbusClient4TcpMaster modbusClient;
public static void main(String[] args) throws Exception {
initClient();
scheduleToSendData();
//1 ChannelSender to send data to machine
//2 ExampleModbusMasterResponseProcessor to receive resp data.
}
private static void initClient() throws Exception {
String host="123.45.67.89";
int port=502;
long sleep=3000;
short transactionIdentifierOffset=0;
ModbusConfs.MASTER_SHOW_DEBUG_LOG = true;
ModbusMasterResponseProcessor masterProcessor=new ExampleModbusMasterResponseProcessor(transactionIdentifierOffset);
ModbusResponseHandler responseHandler = new ModbusMasterResponseHandler(masterProcessor);;
modbusClient = ModbusClientTcpFactory.getInstance().createClient4Master(host, port, responseHandler);
Thread.sleep(sleep);
}
private static void scheduleToSendData() {
Runnable runnable = () ->{
ConsoleUtil.clearConsole(true);
Channel channel = modbusClient.getChannel();
if(channel==null||(!channel.isActive())||!channel.isOpen()||!channel.isWritable())
return;
ChannelSender sender = ChannelSenderFactory.getInstance().get(channel);
//short unitIdentifier=1;
//ChannelSender sender2 =new ChannelSender(channel, unitIdentifier);
int startAddress=0;
int quantityOfCoils=4;
try {
sender.readCoilsAsync(startAddress, quantityOfCoils);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
int sleep=1000;
ScheduledUtil.getInstance().scheduleAtFixedRate(runnable, sleep * 5);
}
}
https://gitee.com/zengfr/easymodbus4j/blob/master/easymodbus4j-example/src/main/java/com/github/zengfr/easymodbus4j/example3/Example3.java
以上是 easymodbus4j开发实例系列教程之1客户端master模式 的全部内容, 来源链接: utcz.com/z/517238.html