From 4d38e23be1f1724be1c933084aa2517b3e21f1f4 Mon Sep 17 00:00:00 2001 From: wangshilong Date: Sun, 1 Dec 2024 14:36:51 +0800 Subject: [PATCH] max value --- .../java/com/iot/modbus_rtcp/GatewayTest.java | 20 +++++++++++++ .../modbus_rtcp/NonBlockingSocketTest.java | 30 +++++++++---------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/iot/modbus_rtcp/GatewayTest.java b/src/test/java/com/iot/modbus_rtcp/GatewayTest.java index 4cd1449..d02d12c 100644 --- a/src/test/java/com/iot/modbus_rtcp/GatewayTest.java +++ b/src/test/java/com/iot/modbus_rtcp/GatewayTest.java @@ -15,6 +15,26 @@ public class GatewayTest { String s2 = "KENENG1400000358"; System.out.println(new String(HexUtil.hexStringToBytes(s1))); System.out.println(HexUtil.bytesToHexString(s2.getBytes())); + + + String hex = "333538"; + int decimal = 0; + int power = 0; + for (int i = hex.length() - 1; i >= 0; i--) { + char digit = hex.charAt(i); + if (digit >= '0' && digit <= '9') { + decimal += (digit - '0') * Math.pow(16, power); + } else if (digit >= 'A' && digit <= 'F') { + decimal += (digit - 'A' + 10) * Math.pow(16, power); + } else if (digit >= 'a' && digit <= 'f') { + decimal += (digit - 'a' + 10) * Math.pow(16, power); + } else { + throw new IllegalArgumentException("Invalid hex digit: " + digit); + } + power++; + } + System.out.println(decimal); } + } diff --git a/src/test/java/com/iot/modbus_rtcp/NonBlockingSocketTest.java b/src/test/java/com/iot/modbus_rtcp/NonBlockingSocketTest.java index a1b8dc0..39982c8 100644 --- a/src/test/java/com/iot/modbus_rtcp/NonBlockingSocketTest.java +++ b/src/test/java/com/iot/modbus_rtcp/NonBlockingSocketTest.java @@ -1,5 +1,7 @@ package com.iot.modbus_rtcp; +import com.iot.modbus_rtcp.utils.HexUtil; + import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; @@ -48,7 +50,7 @@ public class NonBlockingSocketTest { lastSentHeartBeatTime = nowTime; socketChannel.write(heartBeatBuffer); } - LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(500)); + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); continue; } String line = byteBufferToHexString(readBuffer).trim(); @@ -57,24 +59,20 @@ public class NonBlockingSocketTest { System.out.println(LocalDateTime.now() + "<==:收到服务器端请求:" + line); // 接收到请求 - switch (line.toUpperCase()) { - case "01040000001671C4" -> - // 发送响应字符串 - socketChannel.write(ByteBuffer.wrap("01 04 2A 07 E8 00 0B 00 13 00 17 00 11 00 23 00 03 00 01 00 00 00 00 00 02 00 3B 03 E7 00 39 00 00 0D AC 0D AC 00 00 00 00 00 00 0A 14 4A 98".replaceAll(" ", "").getBytes())); + switch (line.toUpperCase().substring(0, line.length() - 4)) { + case "010200140004" -> + socketChannel.write(ByteBuffer.wrap(HexUtil.hexStringToBytes("01042C07D000010003000C002B0029000300010000001A0018003303CD002300080DAC0DAC0000000000000959091DF066"))); + case "0103006A002A" -> + socketChannel.write(ByteBuffer.wrap(HexUtil.hexStringToBytes("0103A400010000000000000000000000000000000000000001000100000000000A0005001E000B001E00000011002D00000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001E00000017001E00000000000100230000000003E800640000000A0000000000000000000003E7003B003B00000001000103E7003B003B0000000100019014"))); + case "0104000A001A" -> + socketChannel.write(ByteBuffer.wrap(HexUtil.hexStringToBytes("01043400020016002B0034000100070001000F001E0016000A00000000000000000000000000000000000003080000153D000000001365B5E3"))); // 接收到开井请求 - case "01050001FF00DDFA" -> - // 发送响应字符串 - socketChannel.write(ByteBuffer.wrap("01050001FF00DDFA".getBytes())); - + case "01050000FF00" -> + socketChannel.write(ByteBuffer.wrap(HexUtil.hexStringToBytes("01050000FF00DDFA"))); // 接收到关井请求 - case "0105000100009C0A" -> - // 发送响应字符串 - socketChannel.write(ByteBuffer.wrap("0105000100009C0A".getBytes())); + case "01050001FF00" -> + socketChannel.write(ByteBuffer.wrap(HexUtil.hexStringToBytes("01050001FF00DDFA"))); - // 接收到读取运行模式请求 - case "010300640001C5D5" -> - // 发送响应字符串 - socketChannel.write(ByteBuffer.wrap("01 03 02 00 03 F8 45".replaceAll(" ", "").getBytes())); // 接收到退出请求 case "exit" -> socketChannel.close(); }