|
|
@ -3,17 +3,21 @@ package com.xujie.sys.modules.reader.service.impl; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction; |
|
|
import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction; |
|
|
import com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest; |
|
|
|
|
|
import com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse; |
|
|
|
|
|
|
|
|
import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest; |
|
|
|
|
|
import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse; |
|
|
import com.ghgande.j2mod.modbus.net.TCPMasterConnection; |
|
|
import com.ghgande.j2mod.modbus.net.TCPMasterConnection; |
|
|
|
|
|
import com.ghgande.j2mod.modbus.procimg.InputRegister; |
|
|
import com.xujie.sys.modules.reader.dao.ModbusCommunicateMapper; |
|
|
import com.xujie.sys.modules.reader.dao.ModbusCommunicateMapper; |
|
|
|
|
|
import com.xujie.sys.modules.reader.entity.EquipmentFolderLocation; |
|
|
import com.xujie.sys.modules.reader.entity.ModbusCommunicateEntity; |
|
|
import com.xujie.sys.modules.reader.entity.ModbusCommunicateEntity; |
|
|
|
|
|
import com.xujie.sys.modules.reader.service.EquipmentFolderLocationService; |
|
|
import com.xujie.sys.modules.reader.service.ModbusCommunicateService; |
|
|
import com.xujie.sys.modules.reader.service.ModbusCommunicateService; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.net.InetAddress; |
|
|
import java.net.InetAddress; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@ -21,22 +25,29 @@ public class ModbusCommunicateServiceImpl extends ServiceImpl<ModbusCommunicateM |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private ModbusCommunicateMapper modbusCommunicateMapper; |
|
|
private ModbusCommunicateMapper modbusCommunicateMapper; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private EquipmentFolderLocationService equipmentFolderLocationService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void getInfoByModbus() { |
|
|
public void getInfoByModbus() { |
|
|
log.info("modbus请求开始解析"); |
|
|
log.info("modbus请求开始解析"); |
|
|
|
|
|
LambdaQueryWrapper<EquipmentFolderLocation> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
|
queryWrapper.eq(EquipmentFolderLocation::getSuffix, "PLC"); |
|
|
|
|
|
EquipmentFolderLocation folderLocation = equipmentFolderLocationService.getOne(queryWrapper); |
|
|
|
|
|
|
|
|
ModbusCommunicateEntity one = this.getOne(new LambdaQueryWrapper<>()); |
|
|
ModbusCommunicateEntity one = this.getOne(new LambdaQueryWrapper<>()); |
|
|
if (one==null){ |
|
|
|
|
|
|
|
|
if (one == null) { |
|
|
log.info("设备信息未填"); |
|
|
log.info("设备信息未填"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
String modbusIp = one.getModbusIp(); |
|
|
|
|
|
int modbusPort = one.getModbusPort(); |
|
|
|
|
|
int registerAddress = one.getRegisterAddress(); |
|
|
|
|
|
int registerNum = one.getRegisterNum(); |
|
|
|
|
|
int unitId = one.getUnitId(); |
|
|
|
|
|
log.info("获取modbus设备ip-端口-地址值-数量-unitId:"+modbusIp+"-"+modbusPort+"-"+registerAddress+"-"+registerNum+"-"+unitId); |
|
|
|
|
|
|
|
|
String modbusIp = folderLocation.getIp(); |
|
|
|
|
|
int modbusPort = folderLocation.getPort(); |
|
|
|
|
|
int registerAddress = folderLocation.getRegisterAddress(); |
|
|
|
|
|
int registerNum = folderLocation.getRegisterNum(); |
|
|
|
|
|
int unitId = folderLocation.getUnitId(); |
|
|
|
|
|
|
|
|
|
|
|
log.info("获取modbus设备ip-端口-地址值-数量-unitId:" + modbusIp + "-" + modbusPort + "-" + registerAddress + "-" + registerNum + "-" + unitId); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
InetAddress ipAddress = InetAddress.getByName(modbusIp); |
|
|
InetAddress ipAddress = InetAddress.getByName(modbusIp); |
|
|
@ -46,29 +57,35 @@ public class ModbusCommunicateServiceImpl extends ServiceImpl<ModbusCommunicateM |
|
|
connection.connect(); |
|
|
connection.connect(); |
|
|
|
|
|
|
|
|
// 创建一个Modbus TCP请求 |
|
|
// 创建一个Modbus TCP请求 |
|
|
//ReadInputRegistersRequest(寄存器起始地址值,寄存器个数) |
|
|
|
|
|
ReadInputRegistersRequest request = new ReadInputRegistersRequest(registerAddress, registerNum); |
|
|
|
|
|
|
|
|
ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(registerAddress, registerNum); |
|
|
request.setUnitID(unitId); // 设置Modbus设备的Unit ID |
|
|
request.setUnitID(unitId); // 设置Modbus设备的Unit ID |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个Modbus TCP事务 |
|
|
// 创建一个Modbus TCP事务 |
|
|
ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection); |
|
|
ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection); |
|
|
transaction.setRequest(request); |
|
|
transaction.setRequest(request); |
|
|
|
|
|
|
|
|
// 执行Modbus TCP事务 |
|
|
// 执行Modbus TCP事务 |
|
|
transaction.execute(); |
|
|
transaction.execute(); |
|
|
|
|
|
log.info("开始执行modbus tcp事物"); |
|
|
|
|
|
|
|
|
// 获取响应 |
|
|
// 获取响应 |
|
|
ReadInputRegistersResponse response = (ReadInputRegistersResponse) transaction.getResponse(); |
|
|
|
|
|
|
|
|
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse(); |
|
|
|
|
|
log.info("打印响应的数据: {}", response.getMessage()); |
|
|
if (response != null) { |
|
|
if (response != null) { |
|
|
// // 处理响应数据 |
|
|
|
|
|
// InputRegister[] registers1 = response.getRegisters(); |
|
|
|
|
|
// for (InputRegister inputRegister : registers1) { |
|
|
|
|
|
// int value = inputRegister.getValue(); |
|
|
|
|
|
// log.info("获取到的modbus数据为: {}",value); |
|
|
|
|
|
// } |
|
|
|
|
|
byte[] message = response.getMessage(); |
|
|
|
|
|
for (byte b : message) { |
|
|
|
|
|
log.info("获取到的modbus数据为: {}", b); |
|
|
|
|
|
|
|
|
// 处理响应数据 |
|
|
|
|
|
ArrayList<Integer> hex = new ArrayList<>(); |
|
|
|
|
|
InputRegister[] registers = response.getRegisters(); |
|
|
|
|
|
for (InputRegister register : registers) { |
|
|
|
|
|
short value = register.toShort(); |
|
|
|
|
|
int hexValue = value & 0xFFFF; |
|
|
|
|
|
log.info("获取的值是: {},value: {}",hexValue,value); |
|
|
|
|
|
hex.add(hexValue); |
|
|
|
|
|
} |
|
|
|
|
|
for (int i = 0; i < hex.size(); i+=2) { |
|
|
|
|
|
int hexI= (hex.get(i) << 16 ) | ( hex.get(i+1)& 0xFFFF); |
|
|
|
|
|
float floatValue = Float.intBitsToFloat(hexI); |
|
|
|
|
|
log.info("最终值是: {}" ,floatValue); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
log.info("处理数据出现异常"); |
|
|
log.info("处理数据出现异常"); |
|
|
@ -81,4 +98,5 @@ public class ModbusCommunicateServiceImpl extends ServiceImpl<ModbusCommunicateM |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |