-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
配合SerialDispatcher库 反馈内容String统一改为byte[],若需要解析或内容不为byte[],则解析为对应的数据
- Loading branch information
sunxudong
committed
Apr 23, 2020
1 parent
ae13fe5
commit e19a347
Showing
8 changed files
with
142 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,20 +10,26 @@ | |
* @Email: [email protected] | ||
* @Date: 2020/3/11 0:45 | ||
*/ | ||
public class CallbackRuleConverter implements DataConverter<String, String> { | ||
public class CallbackRuleConverter implements DataConverter<byte[], Long> { | ||
|
||
CallbackRuleConverter() { | ||
|
||
} | ||
|
||
// 数据格式 | ||
// 协议头 命令类型 命令 数据长度位 其他内容 CRC16校验 停止位 | ||
// BB 04 22 0005 00001032 B07A 7E | ||
// 协议头 数据长度位 子控设备地址 命令类型 消息体 CRC16校验 | ||
// 7A 0800 01 03 01 B07A | ||
@Override | ||
public String convert(String value) { | ||
if (value == null || value.isEmpty() || value.length() < 6) | ||
return ""; | ||
public Long convert(byte[] value) { | ||
if (value == null || value.length < 3) | ||
return -1L; | ||
|
||
return value.substring(2, 6).toUpperCase(); | ||
return (long) (byteToHex(value[1]) * 16 * 16 + byteToHex(value[2])); | ||
} | ||
|
||
private int byteToHex(byte b) { | ||
if (b < 0) | ||
return b & 0xff; | ||
return b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package org.sheedon.demo.converters; | ||
|
||
import android.annotation.SuppressLint; | ||
|
||
import org.sheedon.demo.CharsUtils; | ||
import org.sheedon.serial.DataCheckBean; | ||
import org.sheedon.serial.DataConverter; | ||
import org.sheedon.serial.ResponseBody; | ||
import org.sheedon.serial.SafetyByteBuffer; | ||
import org.sheedon.serial.internal.CharsUtils; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* 数据校验转化器 | ||
|
@@ -14,20 +15,20 @@ | |
* @Email: [email protected] | ||
* @Date: 2020/3/11 0:45 | ||
*/ | ||
public class CheckDataConverter implements DataConverter<StringBuffer, DataCheckBean> { | ||
public class CheckDataConverter implements DataConverter<SafetyByteBuffer, DataCheckBean> { | ||
|
||
private static final String STARTBIT = "BB"; | ||
private static final String ENDBIT = "7E"; | ||
private static final byte STARTBIT = (byte) 0xBB; | ||
private static final byte ENDBIT = 0x7E; | ||
|
||
CheckDataConverter() { | ||
|
||
} | ||
|
||
// 数据格式 | ||
// 协议头 命令类型 命令 数据长度位 其他内容 CRC16校验 停止位 | ||
// BB 04 22 0005 00001032 B07A 7E | ||
// 协议头 数据长度位 子控设备地址 命令类型 消息体 CRC16校验 | ||
// 7A 0800 01 03 01 B07A | ||
@Override | ||
public DataCheckBean convert(StringBuffer value) { | ||
public DataCheckBean convert(SafetyByteBuffer value) { | ||
if (value == null || value.length() == 0) { | ||
return DataCheckBean.build(null, 0); | ||
} | ||
|
@@ -37,28 +38,32 @@ public DataCheckBean convert(StringBuffer value) { | |
return DataCheckBean.build(null, 0); | ||
} | ||
|
||
if (index + 10 >= value.length()) { | ||
if (index + 5 >= value.length()) { | ||
return DataCheckBean.build(null, index); | ||
} | ||
|
||
// 一个内容到总长度 | ||
String lengthStr = value.substring(index + 6, index + 10); | ||
int length = calcLength(lengthStr) * 2; | ||
if (length < 0 || index + length + 14 > value.length()) { | ||
byte[] lengthStr = value.substring(index + 3, index + 5); | ||
int length = calcLength(lengthStr); | ||
if (length < 0 || index + length + 7 > value.length()) { | ||
return DataCheckBean.build(null, index); | ||
} | ||
|
||
String content = value.substring(index + 2, index + length + 12); | ||
byte[] content = value.substring(index + 1, index + length + 6); | ||
boolean check = checkContent(content); | ||
if (check) { | ||
ResponseBody body = ResponseBody.build(STARTBIT, | ||
content.substring(0, content.length() - 2), | ||
content.substring(content.length() - 2), | ||
ENDBIT, STARTBIT + content + ENDBIT); | ||
|
||
return DataCheckBean.build(body, index + length + 14); | ||
byte[] startBit = new byte[]{STARTBIT}; | ||
byte[] messageBit = Arrays.copyOf(content, content.length - 1); | ||
byte[] parityBit = Arrays.copyOfRange(content, content.length - 1, content.length); | ||
byte[] endBit = new byte[]{ENDBIT}; | ||
|
||
ResponseBody body = ResponseBody.build(startBit, messageBit, parityBit, endBit, | ||
value.substring(index, index + length + 7)); | ||
|
||
return DataCheckBean.build(body, index + length + 7); | ||
} else { | ||
return DataCheckBean.build(null, index + length + 14); | ||
return DataCheckBean.build(null, index + length + 7); | ||
} | ||
} | ||
|
||
|
@@ -67,12 +72,12 @@ public DataCheckBean convert(StringBuffer value) { | |
* | ||
* @param str 字符 | ||
*/ | ||
private int calcLength(String str) { | ||
String highPosition = str.substring(0, 2); | ||
String lowPosition = str.substring(2, 4); | ||
private int calcLength(byte[] str) { | ||
byte highPosition = str[0]; | ||
byte lowPosition = str[1]; | ||
|
||
int low = Integer.parseInt(lowPosition, 16); | ||
int high = Integer.parseInt(highPosition, 16); | ||
int high = highPosition & 0xFF; | ||
int low = lowPosition & 0xFF; | ||
|
||
return high * 16 * 16 + low; | ||
|
||
|
@@ -87,31 +92,15 @@ private int calcLength(String str) { | |
* @param content 内容 | ||
* @return 校验是否一致 | ||
*/ | ||
private boolean checkContent(String content) { | ||
if (content.length() <= 2) | ||
private boolean checkContent(byte[] content) { | ||
if (content.length <= 1) | ||
return false; | ||
|
||
String checkStr = content.substring(content.length() - 2); | ||
String contentStr = content.substring(0, content.length() - 2); | ||
StringBuilder checkResult = new StringBuilder(getCheckResult(contentStr)); | ||
for (int index = checkResult.length(); index < 2; index++) { | ||
checkResult.insert(0, "0"); | ||
} | ||
byte checkByte = content[content.length - 1]; | ||
byte[] contentBytes = Arrays.copyOf(content, content.length - 1); | ||
byte checkResult = CharsUtils.sumCheck(contentBytes); | ||
|
||
return checkResult.toString().equalsIgnoreCase(checkStr); | ||
return checkByte == checkResult; | ||
} | ||
|
||
@SuppressLint("DefaultLocale") | ||
private String getCheckResult(String contentStr) { | ||
if (contentStr == null) | ||
return ""; | ||
|
||
if (contentStr.length() % 2 == 1) { | ||
contentStr += "0"; | ||
} | ||
|
||
byte[] bytes = CharsUtils.hexStringToBytes(contentStr); | ||
|
||
return CharsUtils.sumCheckToHexStr(bytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.