parent
0d46b18c5c
commit
5d6dd5dbef
@ -0,0 +1,68 @@
|
|||||||
|
package org.cjyx.chankosc.config;
|
||||||
|
|
||||||
|
|
||||||
|
import org.cjyx.chankosc.entity.ChConfig;
|
||||||
|
import org.cjyx.chankosc.service.chconfig.IChConfigService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.*;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class BeanConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 传感器配置列表
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public Map<String, ChConfig> deviceConfigs(IChConfigService chConfigService) {
|
||||||
|
List<ChConfig> configs = chConfigService.list();
|
||||||
|
Map<String, ChConfig> configMap = configs.stream().collect(
|
||||||
|
Collectors.toMap(c -> (c.getAddrNo() + "-" + c.getChannel()), v -> v, (k1, k2) -> k1));
|
||||||
|
return configMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redis配置
|
||||||
|
*/
|
||||||
|
@Autowired
|
||||||
|
private RedisConnectionFactory factory;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, Object> redisTemplate() {
|
||||||
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||||
|
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.setValueSerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.setConnectionFactory(factory);
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||||
|
return redisTemplate.opsForHash();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
|
||||||
|
return redisTemplate.opsForValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||||
|
return redisTemplate.opsForList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||||
|
return redisTemplate.opsForZSet();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
package org.cjyx.chankosc.config;
|
|
||||||
|
|
||||||
|
|
||||||
import org.cjyx.chankosc.utils.Rs485DataListener;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class createBeanConfiguration {
|
|
||||||
|
|
||||||
// @Bean
|
|
||||||
// public Rs485DataListener dataListener() {
|
|
||||||
// return new Rs485DataListener();
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,30 @@
|
|||||||
|
package org.cjyx.chankosc.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("config")
|
||||||
|
public class ChConfig {
|
||||||
|
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@TableField("addrno")
|
||||||
|
private String addrNo; // 地址编号
|
||||||
|
|
||||||
|
private String channel; // 通道编号
|
||||||
|
|
||||||
|
@TableField("defdata")
|
||||||
|
private String defData; // 初始默认值
|
||||||
|
|
||||||
|
private int status; // 状态
|
||||||
|
|
||||||
|
@TableField("pointno")
|
||||||
|
private int pointNo; // 展台点位编号
|
||||||
|
|
||||||
|
private String comment;
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package org.cjyx.chankosc.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.cjyx.chankosc.entity.ChConfig;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ChConfigMapper extends BaseMapper<ChConfig> {
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package org.cjyx.chankosc.service.chconfig;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.cjyx.chankosc.entity.ChConfig;
|
||||||
|
import org.cjyx.chankosc.mapper.ChConfigMapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ChConfigServiceImpl extends ServiceImpl<ChConfigMapper, ChConfig> implements IChConfigService {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package org.cjyx.chankosc.service.chconfig;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import org.cjyx.chankosc.entity.ChConfig;
|
||||||
|
|
||||||
|
public interface IChConfigService extends IService<ChConfig> {
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.cjyx.chankosc.service;
|
package org.cjyx.chankosc.service.chdevice;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.cjyx.chankosc.entity.ChDevice;
|
import org.cjyx.chankosc.entity.ChDevice;
|
@ -1,4 +1,4 @@
|
|||||||
package org.cjyx.chankosc.service;
|
package org.cjyx.chankosc.service.chdevice;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import org.cjyx.chankosc.entity.ChDevice;
|
import org.cjyx.chankosc.entity.ChDevice;
|
@ -0,0 +1,217 @@
|
|||||||
|
package org.cjyx.chankosc.utils;
|
||||||
|
|
||||||
|
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通讯工具类,采用modbus4j实现
|
||||||
|
*
|
||||||
|
* @author lxq
|
||||||
|
* @dependencies modbus4j-3.0.3.jar
|
||||||
|
* @website https://github.com/infiniteautomation/modbus4j
|
||||||
|
*/
|
||||||
|
public class Modbus4jUtils {
|
||||||
|
/**
|
||||||
|
* 工厂。
|
||||||
|
*/
|
||||||
|
static ModbusFactory modbusFactory;
|
||||||
|
static {
|
||||||
|
if (modbusFactory == null) {
|
||||||
|
modbusFactory = new ModbusFactory();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取master
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws ModbusInitException
|
||||||
|
*/
|
||||||
|
public static ModbusMaster getMaster() throws ModbusInitException {
|
||||||
|
IpParameters params = new IpParameters();
|
||||||
|
params.setHost("localhost");
|
||||||
|
params.setPort(502);
|
||||||
|
|
||||||
|
// modbusFactory.createRtuMaster(wapper); //RTU 协议
|
||||||
|
// modbusFactory.createUdpMaster(params);//UDP 协议
|
||||||
|
// modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
|
||||||
|
ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
|
||||||
|
master.init();
|
||||||
|
|
||||||
|
return master;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取[01 Coil Status 0x]类型 开关数据
|
||||||
|
*
|
||||||
|
* @param slaveId
|
||||||
|
* slaveId
|
||||||
|
* @param offset
|
||||||
|
* 位置
|
||||||
|
* @return 读取值
|
||||||
|
* @throws ModbusTransportException
|
||||||
|
* 异常
|
||||||
|
* @throws ErrorResponseException
|
||||||
|
* 异常
|
||||||
|
* @throws ModbusInitException
|
||||||
|
* 异常
|
||||||
|
*/
|
||||||
|
public static Boolean readCoilStatus(int slaveId, int offset)
|
||||||
|
throws ModbusTransportException, ErrorResponseException, ModbusInitException {
|
||||||
|
// 01 Coil Status
|
||||||
|
BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId, offset);
|
||||||
|
Boolean value = getMaster().getValue(loc);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取[02 Input Status 1x]类型 开关数据
|
||||||
|
*
|
||||||
|
* @param slaveId
|
||||||
|
* @param offset
|
||||||
|
* @return
|
||||||
|
* @throws ModbusTransportException
|
||||||
|
* @throws ErrorResponseException
|
||||||
|
* @throws ModbusInitException
|
||||||
|
*/
|
||||||
|
public static Boolean readInputStatus(int slaveId, int offset)
|
||||||
|
throws ModbusTransportException, ErrorResponseException, ModbusInitException {
|
||||||
|
// 02 Input Status
|
||||||
|
BaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);
|
||||||
|
Boolean value = getMaster().getValue(loc);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取[03 Holding Register类型 2x]模拟量数据
|
||||||
|
*
|
||||||
|
* @param slaveId
|
||||||
|
* slave Id
|
||||||
|
* @param offset
|
||||||
|
* 位置
|
||||||
|
* @param dataType
|
||||||
|
* 数据类型,来自com.serotonin.modbus4j.code.DataType
|
||||||
|
* @return
|
||||||
|
* @throws ModbusTransportException
|
||||||
|
* 异常
|
||||||
|
* @throws ErrorResponseException
|
||||||
|
* 异常
|
||||||
|
* @throws ModbusInitException
|
||||||
|
* 异常
|
||||||
|
*/
|
||||||
|
public static Number readHoldingRegister(int slaveId, int offset, int dataType)
|
||||||
|
throws ModbusTransportException, ErrorResponseException, ModbusInitException {
|
||||||
|
// 03 Holding Register类型数据读取
|
||||||
|
BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
|
||||||
|
Number value = getMaster().getValue(loc);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取[04 Input Registers 3x]类型 模拟量数据
|
||||||
|
*
|
||||||
|
* @param slaveId
|
||||||
|
* slaveId
|
||||||
|
* @param offset
|
||||||
|
* 位置
|
||||||
|
* @param dataType
|
||||||
|
* 数据类型,来自com.serotonin.modbus4j.code.DataType
|
||||||
|
* @return 返回结果
|
||||||
|
* @throws ModbusTransportException
|
||||||
|
* 异常
|
||||||
|
* @throws ErrorResponseException
|
||||||
|
* 异常
|
||||||
|
* @throws ModbusInitException
|
||||||
|
* 异常
|
||||||
|
*/
|
||||||
|
public static Number readInputRegisters(int slaveId, int offset, int dataType)
|
||||||
|
throws ModbusTransportException, ErrorResponseException, ModbusInitException {
|
||||||
|
// 04 Input Registers类型数据读取
|
||||||
|
BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);
|
||||||
|
Number value = getMaster().getValue(loc);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量读取使用方法
|
||||||
|
*
|
||||||
|
* @throws ModbusTransportException
|
||||||
|
* @throws ErrorResponseException
|
||||||
|
* @throws ModbusInitException
|
||||||
|
*/
|
||||||
|
public static void batchRead() throws ModbusTransportException, ErrorResponseException, ModbusInitException {
|
||||||
|
|
||||||
|
BatchRead<Integer> batch = new BatchRead<Integer>();
|
||||||
|
|
||||||
|
batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.FOUR_BYTE_FLOAT));
|
||||||
|
batch.addLocator(1, BaseLocator.inputStatus(1, 0));
|
||||||
|
|
||||||
|
ModbusMaster master = getMaster();
|
||||||
|
|
||||||
|
batch.setContiguousRequests(false);
|
||||||
|
BatchResults<Integer> results = master.send(batch);
|
||||||
|
System.out.println(results.getValue(0));
|
||||||
|
System.out.println(results.getValue(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
// 01 读输出线圈
|
||||||
|
// Boolean x00001 = readCoilStatus(1, 0);
|
||||||
|
// Boolean x00003 = readCoilStatus(1, 2);
|
||||||
|
// Boolean x00007 = readCoilStatus(1, 6);
|
||||||
|
// System.out.println("0x0001:" + x00001);
|
||||||
|
// System.out.println("0x0003:" + x00003);
|
||||||
|
// System.out.println("0x0007:" + x00007);
|
||||||
|
//
|
||||||
|
// // 02 读输入线圈
|
||||||
|
// Boolean x10001 = readInputStatus(1, 0);
|
||||||
|
// Boolean x10003 = readInputStatus(1, 2);
|
||||||
|
// Boolean x10007 = readInputStatus(1, 6);
|
||||||
|
// System.out.println("1x0001:" + x10001);
|
||||||
|
// System.out.println("1x0003:" + x10003);
|
||||||
|
// System.out.println("1x0007:" + x10007);
|
||||||
|
//
|
||||||
|
// // 03 读保持寄存器
|
||||||
|
// Number x40002 = readHoldingRegister(1, 1, DataType.FOUR_BYTE_INT_SIGNED);
|
||||||
|
// Number x40005 = readHoldingRegister(1, 4, DataType.EIGHT_BYTE_INT_SIGNED);
|
||||||
|
// Number x40010 = readHoldingRegister(1, 9, DataType.TWO_BYTE_INT_SIGNED);
|
||||||
|
// System.out.println("4x0002:" + x40002);
|
||||||
|
// System.out.println("4x0005:" + x40005);
|
||||||
|
// System.out.println("4x0010:" + x40010);
|
||||||
|
//
|
||||||
|
// // 04 读输入寄存器
|
||||||
|
// Number x30001 = readInputRegisters(1, 0, DataType.EIGHT_BYTE_FLOAT);
|
||||||
|
// Number x30005 = readInputRegisters(1, 4, DataType.TWO_BYTE_INT_UNSIGNED);
|
||||||
|
// Number x30006 = readInputRegisters(1, 5, DataType.TWO_BYTE_INT_SIGNED);
|
||||||
|
// Number x30008 = readInputRegisters(1, 7, DataType.FOUR_BYTE_FLOAT);
|
||||||
|
// System.out.println("x30001:" + x30001);
|
||||||
|
// System.out.println("x30005:" + x30005);
|
||||||
|
// System.out.println("x30006:" + x30006);
|
||||||
|
// System.out.println("x30008:" + x30008);
|
||||||
|
|
||||||
|
// 读二进制位
|
||||||
|
Number xl30001 = readInputRegisters(1, 0, DataType.EIGHT_BYTE_FLOAT);
|
||||||
|
System.out.println("xl30001: " + xl30001);
|
||||||
|
|
||||||
|
// // 批量读取
|
||||||
|
// batchRead();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue