1. General agreement
General agreements are as follows:
1, The received data is an unfixed length. The protocol starts with AA and ends with DE D0
The received data may be AA aa 08 56 82 44 DE DO
2, The received data is an unfixed length. The start A0 55 of the protocol and the end A0 55 of the protocol are exclusive or check values of the transmitted data,
For example: A0 55 04 51 00 00 55 A0 55 04 53 00 01 56
2. The data received in sections is not easy to analyze
An instruction is indeed divided into several sections, which are not easy to handle. It is found that there is a delay acceptance processing on the Internet, and the effect is good:
/** * 4.Thread receiving serial data */ private class ReceiveThread extends Thread { @Override public void run() { //Condition judgment: as long as the condition is true, the thread will be executed all the time while (isStart) { if (inputStream == null) { return; } byte[] readData = new byte[1024*4]; try { String receiveCmd = ""; String HEAD = "A055"; StringBuilder sb = new StringBuilder(); // In order to finish reading at one time, delayed reading is done if (inputStream.available() > 0 ) { SystemClock.sleep(200); int size = inputStream.read(readData); if (size > 0) { String readString = DataUtils.ByteArrToHex(readData, 0, size); LogUtils.d(readString); if(!TextUtils.isEmpty(readString)){ String[] split = readString.split(HEAD); for (int i = 1; i < split.length; i++) { receiveCmd = HEAD+split[i]; sb.append(receiveCmd+"\n"); LogUtils.e(receiveCmd); } } EventBus.getDefault().post(sb.toString()); sb.setLength(0); } } } catch (IOException e) { e.printStackTrace(); } } } }