Android|Android 串口开发——定时查询心跳数据,解析心跳数据。——持续更新中

/** * 字节数组转换成对应的16进制表示的字符串 * * @param src * @return */ public static String bytes2HexStr(byte[] src) { StringBuilder builder = new StringBuilder(); if (src =https://www.it610.com/article/= null || src.length <= 0) { return""; } char[] buffer = new char[2]; for (int i = 0; i < src.length; i++) { buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16); buffer[1] = Character.forDigit(src[i] & 0x0F, 16); builder.append(buffer); } return builder.toString().toUpperCase(); }

1、心跳数据强转为 byte[] 数据类型
@Override public RecvCommand adaptReceive(byte[] allPack, Object... other) { int cmd = (int) other[0]; LogPlus.e( "收到的命令:" +cmd); byte[] data = https://www.it610.com/article/(byte[]) other[1];


2、开始解析——必须根据解析协议才能解析数据
public class RecvA3Status extends BaseRecvCommand {private final int mResult; private final float mPowerFactorState; public List auxiliaryPlates; public RecvA3Status(byte[] allPack, byte[] data) { super(allPack, data); mResult = 0xff & data[0]; mPowerFactorState = ByteUtil.getFloat(data, 25); auxiliaryPlates = new ArrayList<>(); for (int i = 0; i < (data.length - 29) / 107; i++) { byte[] bytes = new byte[107]; System.arraycopy(data, 29 + i * 107, bytes, 0, bytes.length); if (i==0){ LogPlus.e("几号门==="+i, "门状态11接收数据2仓门=" + ByteUtil.bytes2HexStr(bytes)); }else { LogPlus.i("几号门==="+i, "门状态11接收数据2仓门=" + ByteUtil.bytes2HexStr(bytes)); }auxiliaryPlates.add(new AuxiliaryPlate(bytes)); } }/** * 仓控板状态表 */ public static class AuxiliaryPlate { private int onlineState; private int doorState; private int batteryState; private int fireState; private int urgentState; private int temperature; private int batteryVoltage; private int chargerOnlineState; private ChargerState chargerState; private OnOffAmount onOffAmount; private AnalogQuantity analogQuantity; private int BMSOnlineState; private String BMSID; /**type 0 = 正常电池样式1 = 显示屏样式 */ public int type = 0; public AuxiliaryPlate(byte[] bytes) { onlineState = 0xff & bytes[0]; doorState = 0xff & bytes[1]; batteryState = 0xff & bytes[2]; fireState = 0xff & bytes[3]; urgentState = 0xff & bytes[4]; temperature = 0xff & bytes[5]; batteryVoltage = ByteUtil.bytes2long(bytes, 6, 2) / 100; chargerOnlineState = 0xff & bytes[8]; byte[] bytes1 = new byte[6]; System.arraycopy(bytes, 9, bytes1, 0, bytes1.length); chargerState = new ChargerState(bytes1); BMSOnlineState = 0xff & bytes[15]; byte[] bytes2 = new byte[26]; System.arraycopy(bytes, 16, bytes2, 0, bytes2.length); BMSID = new String(bytes2, StandardCharsets.US_ASCII).trim(); byte[] bytes3 = new byte[7]; System.arraycopy(bytes, 42, bytes3, 0, bytes3.length); onOffAmount = new OnOffAmount(bytes3); byte[] bytes4 = new byte[58]; System.arraycopy(bytes, 49, bytes4, 0, bytes4.length); analogQuantity = new AnalogQuantity(bytes4); }


付解析协议:
Android|Android 串口开发——定时查询心跳数据,解析心跳数据。——持续更新中
文章图片

【Android|Android 串口开发——定时查询心跳数据,解析心跳数据。——持续更新中】
实现demo:https://download.csdn.net/download/meixi_android/12707609bug在线交流: QQ1085220040

    推荐阅读