-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleProtocol.java
More file actions
135 lines (116 loc) · 4.28 KB
/
ExampleProtocol.java
File metadata and controls
135 lines (116 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.ex.myapplication;
import com.ex.hiworld.server.tools.LogsUtils;
import org.junit.Test;
import java.util.Arrays;
import static junit.framework.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleProtocol {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
// tString2Byts();
tByteBreakCombine();
}
private void tByteBreakCombine() {
int[][] Arr = new int[10][];
for (int i = 0; i < Arr.length; i++) {
if (i == 1)
Arr[i] = new int[]{0x5A, 0xA5, 0x09, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6F};
else if (i == 2)
Arr[i] = new int[]{0x5A, 0xA5, 0x0E, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
else if (i == 3)
Arr[i] = new int[]{0xD4};
else if (i == 4)
Arr[i] = new int[]{0x5A, 0xA5, 0x04, 0x76, 0x08, 0x00, 0x00, 0x00, 0x81};
else if (i == 5)
Arr[i] = new int[]{0x5A, 0xA5, 0x04, 0x76, 0x08, 0x00, 0x00};
else if (i == 6)
Arr[i] = new int[]{0x00};
else if (i == 7)
Arr[i] = new int[]{0x81};
else if (i == 8)
Arr[i] = new int[]{0x5A, 0xA5, 0x04, 0x76, 0x08, 0x00, 0x00, 0x00, 0x81};
else
Arr[i] = new int[]{0x5A, 0xA5, 0x0C, 0x31, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x25, 0x25, 0x00, 0x00, 0x25, 0x83, 0x3E};
}
for (int[] aa : Arr) {
checkAndGetFullData(aa);
}
}
boolean isFullData = false;
int[] tempData = new int[1024];
int offset = 0;
private void checkAndGetFullData(int[] ints) {
if (ints.length > 4 && ints[0] == 0x5A && ints[1] == 0xA5) {
if (ints[2] != ints.length - 5) { // tou 2, len 1, cmd 1, checksum 1;
isFullData = false;
System.arraycopy(ints, 0, tempData, offset, ints.length);
offset = ints.length;
return;
}
System.arraycopy(ints, 0, tempData, offset, ints.length);
} else {
// not standard data , may be a remaining data of last data.
if (!isFullData) {
System.arraycopy(ints, 0, tempData, offset, ints.length);
}
boolean fullData = isFullData(tempData);
isFullData = fullData;
if (!isFullData) {
offset += ints.length;
return;
}
}
offset = 0;
isFullData = true;
int reallen = tempData[2] + 5;
int data[] = new int[reallen];
System.arraycopy(tempData, 0, data, 0, reallen);
Arrays.fill(tempData, 0);
System.out.println("complete data: " + LogsUtils.toHexString(data));
}
private boolean isFullData(int[] dd) {
if (dd.length > 4 && dd[0] == 0x5A && dd[1] == 0xA5) {
return checkOk(dd, dd[2]);
}
return false;
}
private boolean checkOk(int[] dd, int len) {
int chck = 0;
for (int i = 0; i < len; i++) {
chck += dd[2 + i];
}
chck = (chck - 1) & 0xFF;
return chck == dd[len + 4];
}
private void tString2Byts() {
String ss = "87.5 MHz";
char[] chars = ss.toCharArray();
StringBuffer sou = new StringBuffer();
sou.append("{");
for (char c : chars) {
byte b = (byte) c;
sou.append(" " + Integer.toHexString(b));
}
System.out.println(sou.toString());
byte[] bytes = ss.getBytes();
sou = new StringBuffer();
sou.append("{");
for (byte b : bytes) {
sou.append(" " + Integer.toHexString(b));
}
System.out.println(sou.toString());
for (byte b : bytes) {
sou.append(b);
}
System.out.println("... " + sou.length());
for (int i = 0; i < 5; i++)
sou.append(0);
byte[] newB = sou.toString().getBytes();
System.out.println(".... new : " + newB.length + ", " + sou.toString());
}
}