Skip to content

Commit

Permalink
🎉 Release 1.3,通讯期间中文编码问题再次修复(BETA,关键),修复底层处理字节毫秒级影响(BETA)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hny0305Lin committed Jul 13, 2024
1 parent 8479fe1 commit 8144af4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public final class Ch34ReadThread extends Thread {
/* 字节监听提取接口 */
private BytesExtract bytesExtract;

/* 全局String 用于保存所有字节数据 */
private StringBuilder allBytes = new StringBuilder();
private StringBuilder currentLine = new StringBuilder();

/* 全局String 用于保存处理过的最长字节数组 */
private byte[] longestProcessedBytes = null;

/* 标志位 辅助保存处理最长字节数组,因为最长字节数组一定就是最终的唯一一次,这个标志位随时true false更替 */
private boolean hasExtracted = false;

/* 构造方法 */
public Ch34ReadThread(CH34xUARTDriver cH34xUARTDriver, UsbEndpoint usbEndpoint, UsbDeviceConnection usbDeviceConnection) {
super();
Expand Down Expand Up @@ -89,36 +99,82 @@ public void run() {
} while (this.usbEndpoint == null && !Thread.interrupted());

for (int i = 0; i < this.ch34xUARTDriver.REQUEST_COUNT; ++i) {
//等待USB
UsbRequest usbRequest = this.usbDeviceConnection.requestWait();
UsbRequest usbRequestI = this.ch34xUARTDriver.getUsbRequests()[i];

//如果USB已关闭且计数器超过20、如果请求为空或不匹配,则跳出循环,重置计数器
if (!this.ch34xUARTDriver.isNullUsb() && count++ > 20) {
Log.v(TAG, "USB已经关闭");
break root;
break root; //跳出循环
}
if (usbRequest == null) break;
if (usbRequest != usbRequestI) {
continue;
}
count = 0;

//获取当前请求的字节缓冲区和长度
byte[] temp = this.ch34xUARTDriver.getByteBuffers()[i].array();
int tempLength = this.ch34xUARTDriver.getByteBuffers()[i].position();
if (tempLength > 0) {

try {
//获取
this.ch34xUARTDriver.getSemaphore().acquire();

byte[] bytes = Arrays.copyOf(temp, tempLength);

// 记录接收到的字节用于调试
Log.d(TAG, "Received bytes: " + Arrays.toString(bytes));

// 使用 UTF-8 编码将字节数组转换为字符串
String receivedData = new String(bytes, 0, tempLength, StandardCharsets.UTF_8);
Log.d(TAG, "Received data: " + receivedData);

// 如果有字节提取监听器,则调用其 value 方法
if (bytesExtract != null) bytesExtract.value(bytes);

//处理字节数组
for (int j = 0; j < tempLength; j++) {
allBytes.append(String.format("%02x", temp[j] & 0xFF)); //转换为小写十六进制
if (temp[j] == 0x0a) { // 检查是否为换行符
if (currentLine.length() > 0) {
//使用UTF-8编码转换
byte[] processedBytes = hexStringToByteArray(currentLine.toString());
Log.d(TAG, "0000000 长度:processedBytes.length="+ processedBytes.length + "\t内容:" + Arrays.toString(processedBytes));
if (longestProcessedBytes == null || processedBytes.length > longestProcessedBytes.length) {
longestProcessedBytes = processedBytes; // 更新为最长的字节数组
hasExtracted = true;
}
String outputLine = new String(hexStringToByteArray(currentLine.toString()), StandardCharsets.UTF_8);
Log.d(TAG, "Processed line 0000000: " + outputLine);
//清空StringBuilder为下一行
currentLine.setLength(0);
}
} else {
if (allBytes.length() > 0) {
currentLine.append(String.format("%02x", temp[j] & 0xFF));
}
}
}

//检查并输出最后一行(如果存在)
if (currentLine.length() > 0) {
byte[] processedBytes = hexStringToByteArray(currentLine.toString());
if (longestProcessedBytes == null || processedBytes.length > longestProcessedBytes.length) {
longestProcessedBytes = processedBytes; // 更新为最长的字节数组
hasExtracted = false;
}
String outputLine = new String(processedBytes, StandardCharsets.UTF_8);
Log.d(TAG, "1111111 长度:processedBytes.length="+ processedBytes.length + "\t内容:" + Arrays.toString(processedBytes));
Log.d(TAG, "Processed line 1111111: " + outputLine);
}

//清空allBytes以避免累积
allBytes.setLength(0);

//传入最长的字节数组
if (bytesExtract != null && longestProcessedBytes != null && hasExtracted) {
bytesExtract.value(longestProcessedBytes);
longestProcessedBytes = null;
hasExtracted = false;
}

//第一版代码,现在已更换
// byte[] bytes = Arrays.copyOf(temp, tempLength);
// //Log.d(TAG, "Received bytes: " + Arrays.toString(bytes));
// String receivedData = new String(bytes, 0, tempLength, StandardCharsets.UTF_8);
// //Log.d(TAG, "Received data: " + receivedData);
// if (bytesExtract != null) bytesExtract.value(bytes);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
Expand Down Expand Up @@ -146,4 +202,16 @@ public void run() {
public void setListener(BytesExtract bytesExtract) {
this.bytesExtract = bytesExtract;
}


// 辅助方法:将十六进制字符串转换为字节数组
private byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ private void NearLinkChatReadData() {

//监听
MainAPP.CH34X.setReadListener(bytes -> {
Log.v(TAG, "setReadListener已进入");
//字节转文本
//String string = StringUtils.needProcess().bytesToString(bytes);
String string = new String(bytes, StandardCharsets.UTF_8);
Expand All @@ -621,7 +622,10 @@ private String CH34xProcessingForReadData(String string) {
String completeSecondData = "";
Log.v(TAG, "长度:completeFirstData.length="+ completeFirstData.length() + "\t内容:" + completeFirstData);
buffer.delete(0, endIndex + 1);

/* 这里可以添加末尾有换行符判断的代码 */
} else {
String completeFirstData = string;
String completeSecondData = "";
//去掉特定的前缀字符串,然后返回(聊天内容),只有当消息包含特定的前缀时才处理
if (completeFirstData.contains(ChatUtils.getPrefixServer()) || completeFirstData.contains(ChatUtils.getPrefixClient())) {
if (completeFirstData.startsWith(ChatUtils.getPrefixServer())) {
Expand Down Expand Up @@ -796,6 +800,7 @@ private String CH34xProcessingForReadData(String string) {
}
}
}
/* 这里可以添加末尾无换行符判断的代码 */
}
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>
<string name="app_name">NLChat</string>
<string name="app_package">com.haohanyh.linmengjia.nearlink.nlchat.fun</string>
<string name="app_version">1.3.210.2024.0713</string>
<string name="app_version">1.3.236.2024.0713</string>

<string name="appwarn">NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。</string>
<string name="thanks3q">友情感谢</string>
Expand Down

0 comments on commit 8144af4

Please sign in to comment.