修复功能码验证问题
This commit is contained in:
parent
feab0f55ed
commit
8fefdf5204
|
@ -46,7 +46,7 @@ public class ModbusSender {
|
|||
if (modbusCommandBo.getIdentifier() == null) {
|
||||
throw new NullPointerException("Invalid gateway identifier");
|
||||
}
|
||||
if (!ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring(modbusCommandBo.getCommand(), 2, 2))) {
|
||||
if (!ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring(modbusCommandBo.getCommand(), 2, 4))) {
|
||||
throw new RuntimeException("Not support command: " + modbusCommandBo.getCommand());
|
||||
}
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ public class SyncHandler extends ChannelInboundHandlerAdapter {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring(msg.toString(), 2, 2))) {
|
||||
if (!ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring(msg.toString(), 2, 4))) {
|
||||
log.warn("Gateway {} fails to send command {} to return data: {}",
|
||||
channel.getIdentifier(), message.getCommand(), msg);
|
||||
// TODO 可在此处添加设备告警
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.iot.modbus_rtcp.utils;
|
|||
|
||||
import com.iot.modbus_rtcp.dto.CommandTypeComparable;
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
@ -40,24 +41,24 @@ public enum ModbusFunctionCodeEnums {
|
|||
this.description = description;
|
||||
}
|
||||
|
||||
public String getHexCode() {
|
||||
return Integer.toHexString(this.code);
|
||||
public String getFunctionCode() {
|
||||
return StringUtils.leftPad(Integer.toHexString(this.code), 2, "0");
|
||||
}
|
||||
|
||||
public static Collection<ModbusFunctionCodeEnums> getAllControlCode() {
|
||||
public static Collection<ModbusFunctionCodeEnums> getAllControlFunctionCode() {
|
||||
return Arrays.stream(values())
|
||||
.filter(item -> Objects.equals(CommandTypeComparable.CommandType.CONTROL, item.getCommandType()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static Collection<ModbusFunctionCodeEnums> getAllCollectionCode() {
|
||||
public static Collection<ModbusFunctionCodeEnums> getAllCollectionFunctionCode() {
|
||||
return Arrays.stream(values())
|
||||
.filter(item -> Objects.equals(CommandTypeComparable.CommandType.COLLECTION, item.getCommandType()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static boolean isHandlerHexCode(String hexCode) {
|
||||
return Arrays.stream(values()).anyMatch(item -> Objects.equals(item.getHexCode(), hexCode));
|
||||
return Arrays.stream(values()).anyMatch(item -> StringUtils.equalsIgnoreCase(item.getFunctionCode(), hexCode));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.iot.modbus_rtcp;
|
||||
|
||||
import com.iot.modbus_rtcp.utils.ModbusFunctionCodeEnums;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
|
@ -8,6 +10,21 @@ import org.junit.jupiter.api.Test;
|
|||
*/
|
||||
public class BinaryToDecimalTest {
|
||||
|
||||
@Test
|
||||
public void testIsHandlerHexCode() throws Exception {
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("01"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("02"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("03"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("04"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("05"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("06"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("0F"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode("10"));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring("010400000016", 2, 4)));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring("010300640052", 2, 4)));
|
||||
System.out.println(ModbusFunctionCodeEnums.isHandlerHexCode(StringUtils.substring("010200140004 ", 2, 4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void binaryNegative() {
|
||||
int number = 225;
|
||||
|
|
Loading…
Reference in New Issue