**modbus在spring boot中的使用,使用modbus4j实现**
# pom.xml配置:
<!-- 辅助modbus4j:repository id:ias-snapshots id:ias-releases 两个 -->
<repositories>
<repository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>ias-snapshots</id>
<name>Infinite Automation Snapshot Repository</name>
<url>https://maven.mangoautomation.net/repository/ias-snapshot/</url>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>ias-releases</id>
<name>Infinite Automation Release Repository</name>
<url>https://maven.mangoautomation.net/repository/ias-release/</url>
</repository>
</repositories>
<!-- modbus -->
<dependency>
<groupId>com.infiniteautomation</groupId>
<artifactId>modbus4j</artifactId>
<version>3.0.3</version>
</dependency>
# modbus配置类:
package com.induo.common.modbus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.induo.domain.mapper.IpControlMapper;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.ip.IpParameters;
import java.util.HashMap;
@Configuration
/*
* 使用@Bean,就不用使用@Import来导入相应的类了,@Bean生成的bean的名字默认为方法名,由于hashMap使用很广泛,
* 所以使用@Bean的方式引入依赖,这样在注入的时候可以指定名称,以免注入错误的对象
* @Import({java.util.HashMap.class,com.serotonin.modbus4j.ModbusFactory.class})
*/
@Import(com.serotonin.modbus4j.ModbusFactory.class)
public class ModbusConfig {
@Bean
public HashMap<String, ModbusMaster> modbusMasterHashMap() {
return new HashMap<>();
}
@Autowired
IpControlMapper ipControlMapper;
@Autowired
private ModbusFactory modbusFactory;
@Autowired
@Qualifier("modbusMasterHashMap")
private HashMap<String,ModbusMaster> masterMap;
/**
* @Title getMaster
* @Description: 通过ip获取对应的modbus连接器
* @params: [ip]
* @return: com.serotonin.modbus4j.ModbusMaster
* @author: caiwei
* @date: 2019/5/1 13:58
*/
public ModbusMaster getMaster(String ip) {
ModbusMaster modbusMaster = masterMap.get(ip);
if(modbusMaster == null) {
setMaster(ip, ipControlMapper.queryControlByIp(ip).getPort());
modbusMaster = masterMap.get(ip);
}
return modbusMaster;
}
/**
* @Title setMaster
* @Description: 设置ip对应的modbus连接器
* @params: [ip, port]
* @return: void
* @author: caiwei
* @date: 2019/5/1 13:59
*/
private void setMaster(String ip, Integer port) {
ModbusMaster master;
IpParameters params = new IpParameters();
params.setHost(ip);
params.setPort(port);
//设置为true,会导致TimeoutException: request=com.serotonin.modbus4j.ip.encap.EncapMessageRequest@774dfba5",
//params.setEncapsulated(true);
master = modbusFactory.createTcpMaster(params, false);// TCP 协议
try {
//设置超时时间
master.setTimeout(3*1000);
//设置重连次数
master.setRetries(3);
//初始化
master.init();
} catch (ModbusInitException e) {
e.printStackTrace();
}
masterMap.put(ip, master);
}
}