springboot中modbus使用
pom.xml配置:
false
true
ias-snapshots
Infinite Automation Snapshot Repository
true
false
ias-releases
Infinite Automation Release Repository
https://maven.mangoautomation.net/repository/ias-release/
com.infiniteautomation
modbus4j
3.0.3
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 modbusMasterHashMap() {
return new HashMap<>();
}
@Autowired
IpControlMapper ipControlMapper;
@Autowired
private ModbusFactory modbusFactory;
@Autowired
@Qualifier("modbusMasterHashMap")
private HashMap 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);
}
}
modbus工具类:
package com.induo.common.modbus;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @ClassName: ModbusUtil
* @Description: modbus读写工具类
* @auther: caiwei
* @date: 2019/5/1 15:44
*/
@Component
public class ModbusUtil {
//从机默认值
private Integer slaveId = 1;
@Autowired
private ModbusConfig modbusConfig;
/**
* @Title readCoilStatus
* @Description: 读(线圈)开关量数据,相当于功能码:01H-读线圈状态
* @params: [ip, slaveId, offset, numberOfRegister]
* @return: boolean[]
* @throws:
* @author: caiwei
* @date: 2019/5/1 14:32
*/
public boolean[] readCoilStatus(String ip, int offset, int numberOfRegister) throws ModbusTransportException {
ModbusMaster master = modbusConfig.getMaster(ip);
ReadCoilsRequest request = new ReadCoilsRequest(slaveId, offset, numberOfRegister);
ReadCoilsResponse response = (ReadCoilsResponse) master.send(request);
boolean[] booleans = response.getBooleanData();
return valueRegroup(numberOfRegister, booleans);
}
/**
* @Title readInputStatus
* @Description: 读取外围设备输入的开关量,相当于功能码:02H-读离散输入状态
* @params: [ip, offset, numberOfRegister]
* @return: boolean[]
* @throws:
* @author: caiwei
* @date: 2019/5/1 14:49
*/
public boolean[] readInputStatus(String ip, int offset, int numberOfRegister) throws ModbusTransportException {
ModbusMaster master = modbusConfig.getMaster(ip);
ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId,offset, numberOfRegister);
ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse) master.send(request);
boolean[] booleans = response.getBooleanData();
return valueRegroup(numberOfRegister, booleans);
}
/**
* @Title readHoldingRegister
* @Description: 读取保持寄存器数据,相当于功能码:03H-读保持寄存器
* @params: [ip, offset, numberOfRegister]
* @return: short[]
* @throws:
* @author: caiwei
* @date: 2019/5/1 15:53
*/
public short[] readHoldingRegister(String ip, int offset, int numberOfRegister) throws ModbusTransportException {
ModbusMaster master = modbusConfig.getMaster(ip);
ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, offset, numberOfRegister);
ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master.send(request);
return response.getShortData();
}
/**
* @Title readInputRegisters
* @Description: 读取外围设备输入的数据,相当于功能码:04H-读输入寄存器
* @params: [ip, offset, numberOfRegister]
* @return: short[]
* @throws:
* @author: caiwei
* @date: 2019/5/1 15:56
*/
public short[] readInputRegisters(String ip, int offset, int numberOfRegister) throws ModbusTransportException {
ModbusMaster master = modbusConfig.getMaster(ip);
ReadInputRegistersRequest request = new ReadInputRegistersRequest(slaveId, offset, numberOfRegister);
ReadInputRegistersResponse response = (ReadInputRegistersResponse) master.send(request);
return response.getShortData();
}
/**
* @Title writeCoil
* @Description: 写单个(线圈)开关量数据,相当于功能码:05H-写单个线圈
* @params: [ip, writeOffset, writeValue]
* @return: boolean
* @throws:
* @author: caiwei
* @date: 2019/5/1 16:00
*/
public boolean writeCoil(String ip, int writeOffset, boolean writeValue) throws ModbusTransportException {
ModbusMaster tcpMaster = modbusConfig.getMaster(ip);
WriteCoilRequest request = new WriteCoilRequest(slaveId, writeOffset, writeValue);
WriteCoilResponse response = (WriteCoilResponse) tcpMaster.send(request);
return !response.isException();
}
/**
* @Title writeCoils
* @Description: 写多个开关量数据(线圈),相当于功能码:0FH-写多个线圈
* @params: [ip, startOffset, data]
* @return: boolean
* @throws:
* @author: caiwei
* @date: 2019/5/1 16:00
*/
public boolean writeCoils(String ip, int startOffset, boolean[] data) throws ModbusTransportException {
ModbusMaster tcpMaster = modbusConfig.getMaster(ip);
WriteCoilsRequest request = new WriteCoilsRequest(slaveId, startOffset, data);
WriteCoilsResponse response = (WriteCoilsResponse) tcpMaster.send(request);
return !response.isException();
}
/**
* @Title writeHoldingRegister
* @Description: 写单个保持寄存器,相当于功能码:06H-写单个保持寄存器
* @params: [ip, writeOffset, writeValue]
* @return: boolean
* @throws:
* @author: caiwei
* @date: 2019/5/1 16:02
*/
public boolean writeHoldingRegister(String ip, int writeOffset, short writeValue) throws ModbusTransportException, ModbusInitException {
ModbusMaster tcpMaster = modbusConfig.getMaster(ip);
WriteRegisterRequest request = new WriteRegisterRequest(slaveId, writeOffset, writeValue);
WriteRegisterResponse response = (WriteRegisterResponse) tcpMaster.send(request);
return !response.isException();
}
/**
* @Title writeHoldingRegisters
* @Description: 写多个保持寄存器,相当于功能码:10H-写多个保持寄存器
* @params: [ip, slaveId, startOffset, data]
* @return: boolean
* @throws:
* @author: caiwei
* @date: 2019/5/1 16:03
*/
public boolean writeHoldingRegisters(String ip, int startOffset, short[] data) throws ModbusTransportException, ModbusInitException {
ModbusMaster tcpMaster = modbusConfig.getMaster(ip);
WriteRegistersRequest request = new WriteRegistersRequest(slaveId, startOffset, data);
WriteRegistersResponse response = (WriteRegistersResponse) tcpMaster.send(request);
return !response.isException();
}
/**
* @Title valueRegroup
* @Description: 转换工具,将Boolean转换成0,1
* @params: [numberOfBits, values]
* @return: boolean[]
* @throws:
* @author: caiwei
* @date: 2019/5/1 15:55
*/
private boolean[] valueRegroup(int numberOfBits, boolean[] values) {
boolean[] bs = new boolean[numberOfBits];
int temp = 1;
for (boolean b : values) {
bs[temp - 1] = b;
temp++;
if (temp > numberOfBits) {
break;
}
}
return bs;
}
}
ipControlMapper:
package com.induo.area.domain.mapper;
import java.util.List;
import com.induo.area.domain.entity.IpControl;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface IpControlMapper {
@Results(id = "ipControlMap", value = {
@Result(column = "ip", property = "ip", javaType = String.class),
@Result(column = "port", property = "port", javaType = Integer.class),
@Result(column = "ip_type", property = "ipType", javaType = Integer.class),
@Result(column = "device_data_gather_cycle", property = "deviceDataGatherCycle", javaType = Integer.class),
@Result(column = "device_data_gather_interval", property = "deviceDataGatherInterval", javaType = Integer.class),
@Result(column = "note_alarm_data_gather_cycle", property = "noteAlarmDataGatherCycle", javaType = Integer.class),
@Result(column = "environment_data_gather_cycle", property = "environmentDataRecordCycle", javaType = Integer.class)
}) 无锡人流医院哪家好 http://mobile.wxbhnkyy120.com/
//通过ip查询控制器
@Select("select ip, port, ip_type, device_data_gather_cycle, device_data_gather_interval, note_alarm_data_gather_cycle, environment_data_gather_cycle " +
"from control " +
"where ip = #{ip}")
IpControl queryControlByIp(String ip);
//通过ip类型(0:区域控制器, 1:八防控制器)查询控制器
@Select("select ip " +
"from control " +
"where ip_type = #{ipType}")
List queryAllIpControlIp(String ipType);
//插入控制器
@Insert("insert into control(ip, port, ip_type, device_data_gather_cycle, device_data_gather_interval, note_alarm_data_gather_cycle, environment_data_gather_cycle) " +
"values (#{ip}, #{port}, #{ipType}, #{deviceDataGatherCycle}, #{deviceDataGatherInterval}, #{noteAlarmDataGatherCycle}, #{environmentDataRecordCycle})")
void addIpControl(IpControl ipControl);
//更新区域控制器
@Update("")
void updateIpControl(IpControl ipControl);
//通过ip删除相应的控制器
@Delete("delete " +
"from control " +
"where ip = #{ip}")
void deleteIpControl(String ip);
}
springboot中modbus使用的更多相关文章
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...
- 由浅入深学习springboot中使用redis
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- SpringBoot 中常用注解
本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...
- SpringBoot中关于Mybatis使用的三个问题
SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
随机推荐
- Mapbox矢量瓦片标准(mapbox vector-tile-spec)
目录 1. 目标 2. 文件格式 2.1. 文件后缀 2.2 MIME类型 3. 投影和范围 4. 内部结构 4.1. 图层 4.2. 要素 4.3. 几何图形编码 4.4. 要素属性 4.5. 示例 ...
- 【4opencv】为基于OpenCV的图像处理程序编写界面—关于QT\MFC\CSharp的选择以及GOCW的介绍
基于OpenCV编写图像处理项目,除了算法以外,比较重要一个问题就是界面设计问题.对于c++语系的程序员来说,一般来说有QT/MFC两种考虑.QT的确功能强大,特别是QML编写andr ...
- Java基础 awt Frame 设置窗体的背景颜色
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- Android之WebRTC介绍(二)
WebRTC提供了点对点之间的通信,但并不意味着WebRTC不需要服务器.暂且不说基于服务器的一些扩展业务,WebRTC至少有两件事必须要用到服务器: 1. 浏览器之间交换建立通信的元数据(信令)必须 ...
- SpringBoot入门-MongoDB(五)
安装MongoDB 安装MongoDB MongoDB用户.数据库相关命令操作 https://www.jianshu.com/p/237a0c5ad9fa # 创建用户以及角色 use spring ...
- Qt组件(例如按钮、树形控件)上响应鼠标右键
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->textEdit,SIGNAL(cust ...
- ABP .NETCore更新数据库时一直连接的之前数据库
使用Update-Database -Verbose更新数据库时,在appsettings.json配置文件中已修改为新的连接字符串,但是使用命令更新数据库时仍然连接的是之前的数据库. 后来把代码移至 ...
- Centos7——Firefox浏览器个性化配置调教
因为谷歌浏览器无法正常登陆帐号,只能切换到火狐浏览器 默认浏览器我使用的是bing搜索 1.隐藏顶部标题栏 顶部标题栏真的占地方,所以直接选择隐藏 点击设置->自定义customize-> ...
- SecureCRT-登录unix/linux服务器主机的软件
百度百科说辞: SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. SecureCRT支持SSH,同时支持Te ...
- (CSDN迁移)JAVA多线程实现-继承Thread
继承Thread方法: extends Thread 重写覆盖run()方法: @Override public void run() 通过start()方法启动线程. threadDemo01.st ...