From d22349714c8d199c02f336f90fba841ef8f5cd39 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 17:46:23 +0800
Subject: [PATCH] 优化内存后最终版202511211746
---
src/chuankou/SerialDataReceiver.java | 144 ++++++++++++++++++++++++++++++-----------------
1 files changed, 91 insertions(+), 53 deletions(-)
diff --git a/src/chuankou/SerialDataReceiver.java b/src/chuankou/SerialDataReceiver.java
index 6c5c56c..f55892d 100644
--- a/src/chuankou/SerialDataReceiver.java
+++ b/src/chuankou/SerialDataReceiver.java
@@ -6,21 +6,26 @@
public class SerialDataReceiver {
private static final int BUFFER_SIZE = 1024;
- private static byte[] staticDataBuffer = new byte[BUFFER_SIZE];
- private static int staticBufferPosition = 0;
+ private static final int MIN_PACKET_LENGTH = 9;
+ private static final byte[] START_MARKER = {(byte) 0xDD, (byte) 0xCC};
+
+ // 浣跨敤闈為潤鎬佹垚鍛橀伩鍏嶅绾跨▼鐜涓嬬殑绔炰簤鏉′欢
+ private byte[] dataBuffer = new byte[BUFFER_SIZE];
+ private int bufferPosition = 0;
+ private final List<byte[]> reusablePackets = new ArrayList<>();
/**
- * 闈欐�佹柟娉曪細鎺ユ敹涓插彛鍘熷鏁版嵁骞惰В鏋愬畬鏁存暟鎹寘
+ * 瀹炰緥鏂规硶锛氭帴鏀朵覆鍙e師濮嬫暟鎹苟瑙f瀽瀹屾暣鏁版嵁鍖�
* @param rawData 鍘熷鏁版嵁
* @param debugEnabled 鏄惁鍚敤璋冭瘯
* @param maxRawDataPrintLength 鏈�澶ф墦鍗伴暱搴�
* @return 瑙f瀽鍑虹殑瀹屾暣鏁版嵁鍖呭垪琛紝濡傛灉娌℃湁瀹屾暣鍖呭垯杩斿洖绌哄垪琛�
*/
- public static List<byte[]> receiveData(byte[] rawData, boolean debugEnabled, int maxRawDataPrintLength) {
- List<byte[]> completePackets = new ArrayList<>();
+ public List<byte[]> receiveData(byte[] rawData, boolean debugEnabled, int maxRawDataPrintLength) {
+ reusablePackets.clear();
if (rawData == null || rawData.length == 0) {
- return completePackets;
+ return reusablePackets;
}
// 鎵撳嵃鍘熷鎺ユ敹鏁版嵁锛堣皟璇曠敤锛�
@@ -28,88 +33,114 @@
printRawData("鏀跺埌涓插彛鍘熷鏁版嵁", rawData, maxRawDataPrintLength);
}
- // 灏嗘暟鎹坊鍔犲埌缂撳啿鍖�
- if (staticBufferPosition + rawData.length > staticDataBuffer.length) {
+ // 妫�鏌ョ紦鍐插尯瀹归噺锛屽姩鎬佸鐞�
+ if (!ensureBufferCapacity(rawData.length)) {
// 缂撳啿鍖轰笉瓒虫椂锛屾竻鐞嗗苟閲嶆柊寮�濮�
- System.arraycopy(staticDataBuffer, staticBufferPosition - rawData.length,
- staticDataBuffer, 0, rawData.length);
- staticBufferPosition = rawData.length;
- } else {
- System.arraycopy(rawData, 0, staticDataBuffer, staticBufferPosition, rawData.length);
- staticBufferPosition += rawData.length;
+ if (debugEnabled) {
+ System.out.println("缂撳啿鍖轰笉瓒筹紝娓呯┖缂撳啿鍖洪噸鏂板紑濮�");
+ }
+ bufferPosition = 0;
}
- // 澶勭悊缂撳啿鍖轰腑鐨勬暟鎹苟鏀堕泦瀹屾暣鍖�
- processBuffer(completePackets, debugEnabled);
+ // 灏嗘暟鎹坊鍔犲埌缂撳啿鍖�
+ System.arraycopy(rawData, 0, dataBuffer, bufferPosition, rawData.length);
+ bufferPosition += rawData.length;
- return completePackets;
+ // 澶勭悊缂撳啿鍖轰腑鐨勬暟鎹苟鏀堕泦瀹屾暣鍖�
+ processBuffer(reusablePackets, debugEnabled);
+
+ return new ArrayList<>(reusablePackets);
+ }
+
+ /**
+ * 纭繚缂撳啿鍖烘湁瓒冲瀹归噺锛屽涓嶅鍒欏皾璇曞帇缂�
+ */
+ private boolean ensureBufferCapacity(int required) {
+ if (bufferPosition + required <= dataBuffer.length) {
+ return true;
+ }
+
+ // 灏濊瘯閫氳繃鍘嬬缉缂撳啿鍖烘潵鑵惧嚭绌洪棿
+ int startIndex = findStartMarker();
+ if (startIndex > 0) {
+ compactBuffer(startIndex);
+ return bufferPosition + required <= dataBuffer.length;
+ }
+
+ return false;
}
/**
* 澶勭悊缂撳啿鍖轰腑鐨勬暟鎹紝瑙f瀽瀹屾暣鏁版嵁鍖�
*/
- private static void processBuffer(List<byte[]> completePackets, boolean debugEnabled) {
- final int MIN_PACKET_LENGTH = 9;
- final byte[] START_MARKER = {(byte) 0xDD, (byte) 0xCC};
-
- while (staticBufferPosition >= MIN_PACKET_LENGTH) {
+ private void processBuffer(List<byte[]> completePackets, boolean debugEnabled) {
+ while (bufferPosition >= MIN_PACKET_LENGTH) {
// 鏌ユ壘璧峰鏍囪
- int startIndex = findStartMarker(START_MARKER);
+ int startIndex = findStartMarker();
if (startIndex == -1) {
// 娌℃湁鎵惧埌璧峰鏍囪锛屾竻绌烘棤鏁堟暟鎹�
if (debugEnabled) {
- //System.out.println("鏈壘鍒拌捣濮嬫爣璁帮紝娓呯┖缂撳啿鍖�");
+ System.out.println("鏈壘鍒拌捣濮嬫爣璁帮紝娓呯┖缂撳啿鍖�");
}
- staticBufferPosition = 0;
+ bufferPosition = 0;
return;
}
// 妫�鏌ユ槸鍚︽湁瓒冲鐨勬暟鎹鍙栨暟鎹暱搴�
- if (startIndex + 4 > staticBufferPosition) {
+ if (startIndex + 4 > bufferPosition) {
// 鏁版嵁涓嶈冻锛岀瓑寰呮洿澶氭暟鎹�
compactBuffer(startIndex);
return;
}
// 璇诲彇鏁版嵁闀垮害 (澶х搴�)
- int dataLength = ((staticDataBuffer[startIndex + 2] & 0xFF) << 8) |
- (staticDataBuffer[startIndex + 3] & 0xFF);
+ int dataLength = ((dataBuffer[startIndex + 2] & 0xFF) << 8) |
+ (dataBuffer[startIndex + 3] & 0xFF);
int totalPacketLength = 2 + 2 + dataLength + 2; // 璧峰鏍囪2 + 鏁版嵁闀垮害2 + 鏁版嵁鍐呭 + CRC2
+ // 妫�鏌ユ暟鎹暱搴︽湁鏁堟��
+ if (dataLength < 0 || totalPacketLength > BUFFER_SIZE) {
+ if (debugEnabled) {
+ System.out.println("鏃犳晥鏁版嵁闀垮害: " + dataLength + ", 璺宠繃璧峰瀛楄妭");
+ }
+ // 璺宠繃閿欒鐨勮捣濮嬫爣璁帮紝缁х画鏌ユ壘
+ compactBuffer(startIndex + 1);
+ continue;
+ }
+
// 妫�鏌ユ槸鍚︽敹鍒板畬鏁存暟鎹寘
- if (startIndex + totalPacketLength > staticBufferPosition) {
+ if (startIndex + totalPacketLength > bufferPosition) {
// 鏁版嵁鍖呬笉瀹屾暣锛岀瓑寰呮洿澶氭暟鎹�
compactBuffer(startIndex);
return;
}
// 鎻愬彇瀹屾暣鏁版嵁鍖�
- byte[] packet = new byte[totalPacketLength];
- System.arraycopy(staticDataBuffer, startIndex, packet, 0, totalPacketLength);
+ byte[] packet = Arrays.copyOfRange(dataBuffer, startIndex, startIndex + totalPacketLength);
if (debugEnabled) {
- //System.out.println("瑙f瀽鍒板畬鏁存暟鎹寘: " + bytesToHex(packet));
+ System.out.println("瑙f瀽鍒板畬鏁存暟鎹寘: " + bytesToHex(packet));
}
// 娣诲姞鍒拌繑鍥炲垪琛�
completePackets.add(packet);
// 绉诲姩缂撳啿鍖轰綅缃�
- int remaining = staticBufferPosition - (startIndex + totalPacketLength);
+ int remaining = bufferPosition - (startIndex + totalPacketLength);
if (remaining > 0) {
- System.arraycopy(staticDataBuffer, startIndex + totalPacketLength,
- staticDataBuffer, 0, remaining);
+ System.arraycopy(dataBuffer, startIndex + totalPacketLength,
+ dataBuffer, 0, remaining);
}
- staticBufferPosition = remaining;
+ bufferPosition = remaining;
}
}
/**
* 鏌ユ壘璧峰鏍囪浣嶇疆
*/
- private static int findStartMarker(byte[] startMarker) {
- for (int i = 0; i <= staticBufferPosition - startMarker.length; i++) {
- if (staticDataBuffer[i] == startMarker[0] && staticDataBuffer[i + 1] == startMarker[1]) {
+ private int findStartMarker() {
+ for (int i = 0; i <= bufferPosition - START_MARKER.length; i++) {
+ if (dataBuffer[i] == START_MARKER[0] && dataBuffer[i + 1] == START_MARKER[1]) {
return i;
}
}
@@ -119,20 +150,20 @@
/**
* 鍘嬬缉缂撳啿鍖猴紝灏嗘湁鏁堟暟鎹Щ鍒板紑澶�
*/
- private static void compactBuffer(int startIndex) {
- if (startIndex > 0) {
- System.arraycopy(staticDataBuffer, startIndex, staticDataBuffer, 0,
- staticBufferPosition - startIndex);
- staticBufferPosition -= startIndex;
+ private void compactBuffer(int startIndex) {
+ if (startIndex > 0 && startIndex < bufferPosition) {
+ System.arraycopy(dataBuffer, startIndex, dataBuffer, 0,
+ bufferPosition - startIndex);
+ bufferPosition -= startIndex;
}
}
/**
* 鎵撳嵃鍘熷鏁版嵁锛堣皟璇曠敤锛�
*/
- private static void printRawData(String prefix, byte[] data, int maxPrintLength) {
+ private void printRawData(String prefix, byte[] data, int maxPrintLength) {
if (data == null || data.length == 0) {
- //System.out.println(prefix + ": 绌烘暟鎹�");
+ System.out.println(prefix + ": 绌烘暟鎹�");
return;
}
@@ -148,13 +179,13 @@
sb.append("... [鎴柇锛屾�婚暱搴�: ").append(data.length).append("]");
}
- //System.out.println(sb.toString());
+ System.out.println(sb.toString());
}
/**
* 宸ュ叿鏂规硶锛氬瓧鑺傛暟缁勮浆鍗佸叚杩涘埗瀛楃涓�
*/
- private static String bytesToHex(byte[] bytes) {
+ private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X ", b));
@@ -165,16 +196,23 @@
/**
* 娓呯┖缂撳啿鍖猴紙閬垮厤鍐呭瓨娉勬紡锛�
*/
- public static void clearBuffer() {
- staticBufferPosition = 0;
+ public void clearBuffer() {
+ bufferPosition = 0;
// 鍙�夛細娓呯┖缂撳啿鍖哄唴瀹�
- Arrays.fill(staticDataBuffer, (byte) 0);
+ Arrays.fill(dataBuffer, (byte) 0);
}
/**
* 鑾峰彇褰撳墠缂撳啿鍖虹姸鎬�
*/
- public static int getBufferStatus() {
- return staticBufferPosition;
+ public int getBufferStatus() {
+ return bufferPosition;
+ }
+
+ /**
+ * 鑾峰彇缂撳啿鍖哄閲�
+ */
+ public int getBufferCapacity() {
+ return dataBuffer.length;
}
}
\ No newline at end of file
--
Gitblit v1.9.3