调整落库逻辑
This commit is contained in:
parent
539907bd0c
commit
eee0d0b810
|
@ -43,7 +43,7 @@ public class ModbusTCPController implements ApplicationRunner {
|
|||
|
||||
try {
|
||||
modbusCommandBoList.stream().forEach(modbusCommandBo -> {
|
||||
modbusCommandBo.setCommand(modbusCommandBo.getCommand() + CRCUtil.getCRC(HexUtil.HexStringToBytes(modbusCommandBo.getCommand())));
|
||||
modbusCommandBo.setCommand(modbusCommandBo.getCommand() + CRCUtil.getCRC(HexUtil.hexStringToBytes(modbusCommandBo.getCommand())));
|
||||
|
||||
modbusCommandBo.setType(CommandTypeComparable.CommandType.COLLECTION);
|
||||
modbusCommandBo.setTimestamp(System.nanoTime());
|
||||
|
@ -71,7 +71,7 @@ public class ModbusTCPController implements ApplicationRunner {
|
|||
|
||||
try {
|
||||
modbusCommandBoList.stream().forEach(modbusCommandBo -> {
|
||||
modbusCommandBo.setCommand(modbusCommandBo.getCommand() + CRCUtil.getCRC(HexUtil.HexStringToBytes(modbusCommandBo.getCommand())));
|
||||
modbusCommandBo.setCommand(modbusCommandBo.getCommand() + CRCUtil.getCRC(HexUtil.hexStringToBytes(modbusCommandBo.getCommand())));
|
||||
|
||||
modbusCommandBo.setType(CommandTypeComparable.CommandType.CONTROL);
|
||||
modbusCommandBo.setTimestamp(System.nanoTime());
|
||||
|
|
|
@ -9,7 +9,7 @@ public class ModbusEncoder extends MessageToByteEncoder<String> {
|
|||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext channelHandlerContext, String s, ByteBuf byteBuf) {
|
||||
byteBuf.writeBytes(HexUtil.HexStringToBytes(s));
|
||||
byteBuf.writeBytes(HexUtil.hexStringToBytes(s));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ public class CRCUtil {
|
|||
public static void main(String[] args) {
|
||||
String[] array = new String[]{"0A0300000019", "0A0303930023", "0A0301A4002D", "0A0301D6002D", "0A0300320064", "0A0300960064", "0A0300FA0064", "0A03015E0064", "0A0200000050"};
|
||||
for (String str : array) {
|
||||
System.out.println(str + CRCUtil.getCRC(HexUtil.HexStringToBytes(str)));
|
||||
System.out.println(str + CRCUtil.getCRC(HexUtil.hexStringToBytes(str)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.iot.modbus_rtcp.utils;
|
|||
|
||||
public class HexUtil {
|
||||
|
||||
public static byte[] HexStringToBytes(String src) {
|
||||
public static byte[] hexStringToBytes(String src) {
|
||||
int l = src.length() / 2;
|
||||
byte[] ret = new byte[l];
|
||||
for (int i = 0; i < l; i++) {
|
||||
|
@ -23,7 +23,7 @@ public class HexUtil {
|
|||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(HexStringToBytes("0D"));
|
||||
System.out.println(hexStringToBytes("0D"));
|
||||
System.out.println(new byte[]{(byte) Integer.parseInt("0D", 16)});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
package com.iot.modbus_rtcp;
|
||||
|
||||
import com.iot.modbus_rtcp.utils.HexUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:shilong.wang@alpha-ess.com">王仕龙</a>
|
||||
* 2024/11/25 14:51
|
||||
*/
|
||||
public class BinaryToDecimalTest {
|
||||
|
||||
@Test
|
||||
public void binaryNegative() {
|
||||
int number = 225;
|
||||
String binaryString = Integer.toBinaryString(number);
|
||||
System.out.println("二进制表示: " + binaryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void binaryToNumber() {
|
||||
String binaryString = "11111011";
|
||||
int decimalNumber = Integer.parseInt(binaryString, 2);
|
||||
System.out.println("十进制表示: " + decimalNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hexStringToNegativeNumber() {
|
||||
String hexString = "E1";
|
||||
int number = 225;
|
||||
String binaryString = Integer.toBinaryString(number);
|
||||
byte[] byteValues = HexUtil.hexStringToBytes(hexString);
|
||||
System.out.println(Integer.parseInt(binaryString, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hexStringToNegativeNumber1() {
|
||||
// String hexNumber = "F7"; // 16进制负数表示,等同于-9
|
||||
String hexNumber = "E1"; // 16进制负数表示,等同于-10
|
||||
int decimalNumber = Integer.parseInt(hexNumber, 16);
|
||||
String binaryNumber = Integer.toBinaryString(decimalNumber);
|
||||
binaryNumber = binOriginalToBack(binaryNumber);
|
||||
binaryNumber = binBackToRepair(binaryNumber, "1");
|
||||
|
||||
System.out.println("有符号16进制数 " + hexNumber + " 对应的10进制数为 " + this.binRepairToDec(binaryNumber));
|
||||
}
|
||||
|
||||
/**
|
||||
* 原码转反码
|
||||
*
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
public static String binOriginalToBack(String number) {
|
||||
if (number.startsWith("0")) {
|
||||
return number;
|
||||
} else {
|
||||
StringBuffer sbf = new StringBuffer();
|
||||
sbf.append("1");
|
||||
String f_str = number.substring(1);
|
||||
for (int i = 0; i < f_str.length(); i++) {
|
||||
String s_str = String.valueOf(f_str.charAt(i));
|
||||
if (s_str.equals("0")) {
|
||||
sbf.append("1");
|
||||
} else if (s_str.equals("1")) {
|
||||
sbf.append("0");
|
||||
}
|
||||
}
|
||||
return sbf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 反码转补码
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
public static String binBackToRepair(String a, String b) {
|
||||
if (a.startsWith("0")) {
|
||||
return a;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int pre = 0;//进位
|
||||
int sum = 0;//存储进位和另两个位的和
|
||||
while (a.length() != b.length()) {//将两个二进制的数位数补齐,在短的前面添0
|
||||
if (a.length() > b.length()) {
|
||||
b = "0" + b;
|
||||
} else {
|
||||
a = "0" + a;
|
||||
}
|
||||
}
|
||||
for (int i = a.length() - 1; i >= 0; i--) {
|
||||
x = a.charAt(i) - '0';
|
||||
y = b.charAt(i) - '0';
|
||||
sum = x + y + pre;//从低位做加法
|
||||
if (sum >= 2) {
|
||||
pre = 1;//进位
|
||||
sb.append(sum - 2);
|
||||
} else {
|
||||
pre = 0;
|
||||
sb.append(sum);
|
||||
}
|
||||
}
|
||||
if (pre == 1) {
|
||||
sb.append("1");
|
||||
}
|
||||
// 翻转返回
|
||||
return sb.reverse().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 补码转十进制
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
|
||||
public String binRepairToDec(String str) {
|
||||
String h_number = str.substring(1);
|
||||
if (str.startsWith("0")) {
|
||||
return "" + Long.valueOf(h_number, 2);
|
||||
} else if (str.startsWith("1")) {
|
||||
return "-" + Long.valueOf(h_number, 2);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue