-
Notifications
You must be signed in to change notification settings - Fork 1
/
Receive data and instructions
154 lines (141 loc) · 2.25 KB
/
Receive data and instructions
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
144
145
146
147
String input = "";
boolean flag = false;
int state = 0; //表示open的状态int state=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if (flag)
{
// Serial.println(input);
dataProcess(input);
input="";
flag=false;
}
}
void serialEvent()
{
while (Serial.available())
{
char inChar=(char)Serial.read();
input+=inChar;
if (inChar=='\r')
{
flag=true;
}
}
}
void dataProcess(String command)
{
if(command==NULL||command.length()==0)
sendData(0);
char type=command.charAt(0);
command = command.substring(0,command.length()-1);
if(type=='V'&&command.length()==1)
{
sendData(1);
}
else if(type=='O'&&command.charAt(1)=='1'&&command.length()==2)
{
open();
}
else if(type=='C'&&command.length()==1)
{
close();
}
else if(type=='S'&&command.length()==2)
{
setSpeed(command.charAt(1));
}
else if(type=='T' || type=='t')
{
sendFrameData(command,type);
}
else
{
sendData(0);
}
}
void sendFrameData(String data,char type)
{
int normal;
if(state == 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 setSpeed(char c)
{
if(state==0)
{
int level=(int)(c-'0');
if(level<0||level >8)
sendData(0);
else
{
sendData(1);
}
}
else
{
sendData(0);
}
}
void close()
{
if(state==1)
{
state=0;
sendData(1);
}
else
{
sendData(0);
}
}
void open()
{
if(state==0)
{
state=1;
sendData(1);
}
else
{
sendData(0);
}
}
void sendData(int flag)
{
if(flag == 0){
Serial.write((char)7);
Serial.println("bhjdscvsdjc");}
else
{
Serial.print("SV2.5-HV2.0");
Serial.write("\r");
}
}