-
Notifications
You must be signed in to change notification settings - Fork 1
/
Receive data to return information
147 lines (129 loc) · 2.17 KB
/
Receive data to return information
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
136
137
138
139
140
141
142
143
String comdata = "";
int correct = 0; //表示数据正确与否
int on = 0; //表示open的状态
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available() > 0)
{
char sig = char(Serial.read());
comdata += sig;
delay(2);
if(sig == '\r')
{
correct = 1;
}
}
if(correct == 1)
{
dataProcess(comdata);
comdata = "";
correct = 0;
}
}
void dataProcess(String data)
{
if(data == NULL)
sendData(0);
char type = data.charAt(0);
data = data.substring(0,data.length()-1);
if(type == 'V' && data.length() == 1)
{
Serial.println("SV2.5-HV2");
return;
}
else if(type == 'O' && data.charAt(1)=='1'&&data.length()==2)
open();
else if(type=='C'&&data.length()==1)
close();
else if(type=='S'&&data.length()==2)
setSpeed(data.charAt(1));
if(type=='T' || type=='t')
sendFrameData(data,type);
else
sendData(0);
}
void setSpeed(char c)
{
if(on==0)
{
int level=(int)(c-'0');
if(level<0||level >8)
sendData(0);
else
{
sendData(1);
}
}
else
{
sendData(0);
}
}
void close()
{
if(on==1)
{
on=0;
sendData(1);
}
else
{
sendData(0);
}
}
void open()
{
if(on==0)
{
on=1;
sendData(1);
}
else
{
sendData(0);
}
}
void sendFrameData(String data,char type)
{
int normal;
if(on == 1)
{
sendData(0);
return;
}
if(type == 'T')
normal = 10;
else
normal = 5;
int len = data.length();
for(int i = 1; i < len; i++)
{
if(!((data[i] >= '0'&& data[i] <= '9') || ( data[i] >= 'A'&&data[i] <= 'F')))
{
sendData(0);
return;
}
}
int canLen = data.charAt(4)-'0';
if(len < normal || canLen <= 0 || canLen >8 || len != normal + canLen*2)
{
sendData(0);
return;
}
sendData(1);
}
void sendData(int sig)
{
if(sig == 0){
Serial.write((char)7);
Serial.println("no");
}
else{
Serial.println("ok");
Serial.write("\r");
}
}