Skip to content

Commit

Permalink
更新内容
Browse files Browse the repository at this point in the history
配合SerialDispatcher库
反馈内容String统一改为byte[],若需要解析或内容不为byte[],则解析为对应的数据
  • Loading branch information
sunxudong committed Apr 23, 2020
1 parent ae13fe5 commit e19a347
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 101 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation project(':seriallibrary')
implementation 'com.github.Sheedon:SerialDispatcher:1.1.1'
implementation 'com.github.Sheedon:SerialDispatcher:1.2.1'
}
59 changes: 31 additions & 28 deletions app/src/main/java/org/sheedon/demo/RFIDModel.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.sheedon.demo;

import org.sheedon.serial.internal.CharsUtils;
import org.sheedon.serial.retrofit.serialport.RULES;

import java.util.Arrays;

/**
* RFIDModel
*
Expand All @@ -11,44 +14,48 @@
*/
public class RFIDModel {

@RULES(end = 2)
private String commandType;// 命令类型
@RULES(end = 1)
private byte[] commandType;// 命令类型
@RULES(begin = 1, end = 2)
private byte[] command;// 命令
@RULES(begin = 2, end = 4)
private String command;// 命令
@RULES(begin = 4, end = 8)
private String length;// 长度
@RULES(begin = 8, end = 10)
private String signalStrength;// 信号强度
@RULES(begin = 10, end = 14)
private String pc;// PC值
@RULES(begin = 14, end = 38)
private String labelNumber;// 标签编号
private byte[] length;// 长度
@RULES(begin = 4, end = 5)
private byte[] signalStrength;// 信号强度
@RULES(begin = 5, end = 7)
private byte[] pc;// PC值
@RULES(begin = 7, end = 19)
private byte[] labelNumber;// 标签编号

public String getCommandType() {
public byte[] getCommandType() {
return commandType;
}

public String getCommand() {
public byte[] getCommand() {
return command;
}

public String getLength() {
public byte[] getLength() {
return length;
}

public String getSignalStrength() {
public byte[] getSignalStrength() {
return signalStrength;
}

public String getPc() {
public byte[] getPc() {
return pc;
}

public byte[] getLabelNumber() {
return labelNumber;
}

/**
* 获取标签编号
*/
public String getDecodeLabelNumber() {
return labelNumber;
return CharsUtils.byte2HexStrNotEmpty(labelNumber);
}

/**
Expand All @@ -63,22 +70,18 @@ public long getDecodeSignalStrength() {
* 16进制转10进制
*/
private long convertNum() {
try {
return Integer.parseInt(signalStrength, 16);
} catch (NumberFormatException e) {
return -1;
}
return signalStrength.length > 0 ? (signalStrength[0] & 0xff) : 0;
}

@Override
public String toString() {
return "RFIDModel{" +
"commandType='" + commandType + '\'' +
", command='" + command + '\'' +
", length='" + length + '\'' +
", signalStrength='" + signalStrength + '\'' +
", pc='" + pc + '\'' +
", labelNumber='" + labelNumber + '\'' +
"commandType=" + CharsUtils.byte2HexStrNotEmpty(commandType) +
", command=" + CharsUtils.byte2HexStrNotEmpty(command) +
", length=" + CharsUtils.byte2HexStrNotEmpty(length) +
", signalStrength=" + CharsUtils.byte2HexStrNotEmpty(signalStrength) +
", pc=" + CharsUtils.byte2HexStrNotEmpty(pc) +
", labelNumber=" + CharsUtils.byte2HexStrNotEmpty(labelNumber) +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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;

/**
* 数据校验转化器
Expand All @@ -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);
}
Expand All @@ -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);
}
}

Expand All @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.sheedon.serial.DataCheckBean;
import org.sheedon.serial.DataConverter;
import org.sheedon.serial.SafetyByteBuffer;

/**
* 数据转化工厂
Expand All @@ -29,13 +30,13 @@ private DataConverterFactory() {

@Nullable
@Override
public DataConverter<String, String> callbackNameConverter(String data) {
public DataConverter<byte[], Long> callbackNameCodeConverter(byte[] data) {
return ruleConverter == null ? ruleConverter = new CallbackRuleConverter() : ruleConverter;
}

@Nullable
@Override
public DataConverter<StringBuffer, DataCheckBean> checkDataConverter() {
public DataConverter<SafetyByteBuffer, DataCheckBean> checkDataConverter() {
return checkConverter == null ? checkConverter = new CheckDataConverter() : checkConverter;
}
}
6 changes: 3 additions & 3 deletions seriallibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 4
versionName "1.1.2"
versionCode 5
versionName "1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand All @@ -31,5 +31,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'com.github.Sheedon:SerialDispatcher:1.1.1'
implementation 'com.github.Sheedon:SerialDispatcher:1.2.1'
}
Loading

0 comments on commit e19a347

Please sign in to comment.