Modbus TCP读取寄存器数据
modbus4j读取寄存器数据 【Modbus TCP读取寄存器数据】ModbusReadUtil读取工具类
package cn.sync.modbus;
import org.apache.log4j.Logger;
import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.locator.BaseLocator;
/**
*
* Modbus TCP协议读取数据
* @author zyl
*
*/
public class ModbusReadUtil { private static Logger logger = Logger.getLogger(ModbusReadUtil.class);
/**
* 单列工厂。
*/
static ModbusFactory modbusFactory;
static {
if (modbusFactory == null) {
modbusFactory = new ModbusFactory();
}
}
/**
* 获取master
* @param hostIP地址
* @param port端口号
* @return
* @throws ModbusInitException
*/
public static ModbusMaster getMaster(String host,int port) throws ModbusInitException {
try {
IpParameters params = new IpParameters();
params.setHost(host);
params.setPort(port);
ModbusMaster master = modbusFactory.createTcpMaster(params, false);
// TCP 协议
master.init();
//master初始化
return master;
} catch (Exception e) {
logger.error("获取master出现异常"+e);
e.printStackTrace();
}
return null;
}
/**
* 读取[03 Holding Register类型 2x]模拟量数据
* @param master ModbusMaster对象
* @param slaveId
*slave Id设备ID
* @param offset
*偏移截止位置
* @param dataType
*数据类型,来自com.serotonin.modbus4j.code.DataType
* @return
* @throws ModbusTransportException
*异常
* @throws ErrorResponseException
*异常
* @throws ModbusInitException
*异常
*/
public static Number readHoldingRegister(ModbusMaster master,int slaveId, int offset, int dataType)
throws ModbusTransportException, ErrorResponseException, ModbusInitException {
// 03 Holding Register类型数据读取
BaseLocator loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
System.out.println("loc:"+loc);
Number value =https://www.it610.com/article/master.getValue(loc);
System.out.println();
return value;
}
/**
* 批量读取使用寄存器数据
* @parammaster ModbusMaster对象
* @param batchRead批量读取集合
* @throws ModbusTransportException
* @throws ErrorResponseException
* @throws ModbusInitException
*/
public static BatchResults batchRead(ModbusMaster master,BatchRead batchRead)throws Exception {
try {
batchRead.setContiguousRequests(false);
BatchResults results = master.send(batchRead);
return results;
} catch (Exception e) {
logger.error("批量读取使用寄存器数据出现异常"+e);
e.printStackTrace();
}
return null;
}
//测试
public static void main(String[] args) {
try {
ModbusMaster master=getMaster("127.0.0.1",502);
BatchRead batch = new BatchRead();
batch.addLocator(0, BaseLocator.holdingRegister(1,1002, DataType.FOUR_BYTE_FLOAT_SWAPPED));
batch.addLocator(1, BaseLocator.holdingRegister(1,1004, DataType.FOUR_BYTE_FLOAT_SWAPPED));
batch.addLocator(2, BaseLocator.holdingRegister(1,1006, DataType.FOUR_BYTE_FLOAT_SWAPPED));
batch.addLocator(3, BaseLocator.holdingRegister(1,1008, DataType.FOUR_BYTE_FLOAT_SWAPPED));
BatchResults batchRead=batchRead(master,batch);
System.out.println(batchRead.getValue(0));
System.out.println(batchRead.getValue(1));
System.out.println(batchRead.getValue(2));
System.out.println(batchRead.getValue(3));
//DataType.FOUR_BYTE_FLOAT_SWAPPEDfloat高低位数据处理
Number v031 = readHoldingRegister(master,1,1010, DataType.FOUR_BYTE_FLOAT_SWAPPED);
// 注意,float
System.out.println("v031:"+v031);
} catch (Exception e) {
e.printStackTrace();
} }
}
代码以及依赖JAR下载
推荐阅读
- CVE-2020-16898|CVE-2020-16898 TCP/IP远程代码执行漏洞
- ExcelPackage读取写入
- springboot中.yml文件的值无法读取的问题及解决
- 计算机网络基础TCP\HTTP\HTTPS
- TCP长连接与段链接
- TCP组件设计篇(详细设计)
- 工具|Wireshark抓包对其字TCP段理解
- 网络|网络编程释疑(TCP连接拔掉网线后会发生什么)
- 服务器未能释放tcp连接,TCP连接的建立和释放
- C#(App.config读取自定义节点,未能加载文件或程序集)