-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_and_send.ino
95 lines (81 loc) · 1.71 KB
/
test_and_send.ino
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
String comdata = "";
int correct = 0; //表示数据正确与否
int on = 1; //表示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.print("SV2.5-HV2.0");
else if(type == 'O' && data.charAt(1)=='1'&&command.length()==2)
open();
else if(type=='C'&&data.length()==1)
close();
else if(type=='S'&&data.length()==2)
setSpeed(data.charAt(1));
else if(type=='T' || type=='t')
sendFrameData(data,type);
else
sendData(0);
}
void sendFrameData(String data,char type)
{
int normal;
if(on == 0)
{
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);
else
Serial.write("\r");
}