INOVANCE
pom文件
<!-- Modbus数据读写 -->
<dependency>
<groupId>com.intelligt.modbus</groupId>
<artifactId>jlibmodbus</artifactId>
<version>1.2.9.7</version>
</dependency>
获取master
public static ModbusMaster getMaster(String host) {
if (!masters.containsKey(host)) {
try {
IpParameters params = new IpParameters();
params.setHost(host);
params.setPort(502);
ModbusMaster master = modbusFactory.createTcpMaster(params, true);// TCP 协议
master.init();
masters.put(host, master);
} catch (ModbusInitException ex) {
if (!ex.getMessage().equals("java.net.SocketTimeoutException: connect timed out")) {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return masters.get(host);
}
读数据
//ModbusMaster
ModbusMaster[] master = {getMaster(target.getNodeIp())};
//获取数据
String value = master[0].getValue(new NumericLocator(1, 3, address, type)).toString();
写数据
//ModbusMaster
ModbusMaster master = getMaster(target.getNodeIp());
//设置数据
master.setValue(getNumberFormat(target), value);
标签:masters,汇川,host,master,ex,params,PLC,ModbusMaster,INOVANCE
From: https://www.cnblogs.com/jspider/p/18201970