|
|
|
@ -17,7 +17,9 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.net.InetAddress; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@ -47,7 +49,27 @@ public class ModbusCommunicateServiceImpl extends ServiceImpl<ModbusCommunicateM |
|
|
|
int registerNum = folderLocation.getRegisterNum(); |
|
|
|
int unitId = folderLocation.getUnitId(); |
|
|
|
|
|
|
|
|
|
|
|
int registerAddressInspection=folderLocation.getRegisterAddressInspection(); |
|
|
|
int registerNumInspection=folderLocation.getRegisterNumInspection(); |
|
|
|
//获取单号 |
|
|
|
String inspectionOrderNumber = getInspectionOrderNumber(modbusIp, modbusPort, registerAddressInspection, registerNumInspection, unitId); |
|
|
|
log.info("最终获取的检验单号是: {}" ,inspectionOrderNumber); |
|
|
|
|
|
|
|
List<Float> value = getValue(modbusIp, modbusPort, registerAddress, registerNum, unitId); |
|
|
|
log.info("最终获取的数据是: {}" ,value); |
|
|
|
} |
|
|
|
|
|
|
|
//获取具体值 |
|
|
|
public List<Float> getValue(String modbusIp, int modbusPort, int registerAddress, int registerNum, int unitId) { |
|
|
|
log.info("modbus请求开始解析"); |
|
|
|
// String modbusIp = "172.26.58.222"; |
|
|
|
// int modbusPort = 502; |
|
|
|
// int registerAddress = 499; |
|
|
|
// int registerNum = 20; |
|
|
|
// int unitId = 1; |
|
|
|
log.info("获取modbus设备ip-端口-地址值-数量-unitId:" + modbusIp + "-" + modbusPort + "-" + registerAddress + "-" + registerNum + "-" + unitId); |
|
|
|
ArrayList<Float> floats = new ArrayList<Float>(); |
|
|
|
|
|
|
|
try { |
|
|
|
InetAddress ipAddress = InetAddress.getByName(modbusIp); |
|
|
|
@ -85,6 +107,7 @@ public class ModbusCommunicateServiceImpl extends ServiceImpl<ModbusCommunicateM |
|
|
|
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); |
|
|
|
floats.add(floatValue); |
|
|
|
log.info("最终值是: {}" ,floatValue); |
|
|
|
} |
|
|
|
} else { |
|
|
|
@ -97,6 +120,77 @@ public class ModbusCommunicateServiceImpl extends ServiceImpl<ModbusCommunicateM |
|
|
|
log.error("连接Modbus设备时出现异常: {}", e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return floats; |
|
|
|
} |
|
|
|
|
|
|
|
//获取检验单号 |
|
|
|
public String getInspectionOrderNumber(String modbusIp, int modbusPort,int registerAddress,int registerNum,int unitId){ |
|
|
|
log.info("modbus请求开始解析"); |
|
|
|
// String modbusIp = "172.26.58.222"; |
|
|
|
// int modbusPort = 502; |
|
|
|
// int registerAddress = 299; |
|
|
|
// int registerNum = 11; |
|
|
|
// int unitId = 1; |
|
|
|
log.info("获取modbus设备ip-端口-地址值-数量-unitId:" + modbusIp + "-" + modbusPort + "-" + registerAddress + "-" + registerNum + "-" + unitId); |
|
|
|
String finalString=""; |
|
|
|
|
|
|
|
try { |
|
|
|
InetAddress ipAddress = InetAddress.getByName(modbusIp); |
|
|
|
// 连接到Modbus设备 |
|
|
|
TCPMasterConnection connection = new TCPMasterConnection(ipAddress); |
|
|
|
connection.setPort(modbusPort); |
|
|
|
connection.connect(); |
|
|
|
|
|
|
|
// 创建一个Modbus TCP请求 |
|
|
|
ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(registerAddress, registerNum); |
|
|
|
request.setUnitID(unitId); // 设置Modbus设备的Unit ID |
|
|
|
|
|
|
|
|
|
|
|
// 创建一个Modbus TCP事务 |
|
|
|
ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection); |
|
|
|
transaction.setRequest(request); |
|
|
|
|
|
|
|
// 执行Modbus TCP事务 |
|
|
|
transaction.execute(); |
|
|
|
log.info("开始执行modbus tcp事物"); |
|
|
|
|
|
|
|
// 获取响应 |
|
|
|
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse(); |
|
|
|
log.info("打印响应的数据: {}", response.getMessage()); |
|
|
|
if (response != null) { |
|
|
|
InputRegister[] registers = response.getRegisters(); |
|
|
|
ArrayList<byte[]> byteArrayList = new ArrayList<byte[]>(); |
|
|
|
for (InputRegister register : registers) { |
|
|
|
short value = register.toShort(); |
|
|
|
log.info("short类型: {}",value); |
|
|
|
// 创建一个长度为 2 的字节数组 |
|
|
|
byte[] byteArray = new byte[2]; |
|
|
|
// 将 short 值转换成字节数组 |
|
|
|
byteArray[1] = (byte) (value & 0xff); // 获取低位字节 |
|
|
|
byteArray[0] = (byte) ((value >> 8) & 0xff); // 获取高位字节 |
|
|
|
byteArrayList.add(byteArray); |
|
|
|
} |
|
|
|
|
|
|
|
// 将字节数组列表转换为字符串 |
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
for (byte[] byteArray : byteArrayList) { |
|
|
|
stringBuilder.append(new String(byteArray, StandardCharsets.UTF_8)); |
|
|
|
} |
|
|
|
//复制需要返回的string值 |
|
|
|
finalString = stringBuilder.toString(); |
|
|
|
log.info("转换后的字符串: {}", finalString); |
|
|
|
|
|
|
|
} else { |
|
|
|
log.info("处理数据出现异常"); |
|
|
|
} |
|
|
|
// 关闭连接 |
|
|
|
connection.close(); |
|
|
|
log.info("关闭连接"); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("连接Modbus设备时出现异常: {}", e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return finalString; |
|
|
|
} |
|
|
|
|
|
|
|
} |