-
Notifications
You must be signed in to change notification settings - Fork 1
/
cantool updated
181 lines (170 loc) · 3.12 KB
/
cantool updated
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<time.h>
String input = "";
boolean flag = false;
int state = 1; //表示open的状态int state=0;
//int isopen = 0;//表示之前没有打开,没有经历发送self数据,因为按逻辑写的话,发送数据时,当输入C,command这时是回车,会输出不必要的fail。
int cnt=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
//发送的正确的数据
String self_right[13] = {"t3588CD3CFC7F1818F000\r","t03D82A00000000000000\r","t03D82A00000000000000\r","t03D82A00000000000000\r""t3588CD3CFC7F1818F000\r",
"t3588CD3CFC7F1818F000\r","t3588CD3CFC7F1818F000\r","t3588CD3CFC7F1818F000\r","t3588CD3CFC7F1818F000\r","t3588CD3CFC7F1818F000\r","t3588CD3CFC7F1818F000\r",
"t3588CD3CFC7F1818F000\r","t3588CD3CFC7F1818F000\r"};
int right = 13;
void loop() {
// put your main code here, to run repeatedly:
if (flag)
{
// Serial.println(input);
dataProcess(input);
input="";
flag=false;
}
cnt++;
delay(10);
if(cnt>100)
{
srand((unsigned)time(NULL));
cnt-=100;
while(state == 0)
{
String temp = "";
int index = rand() % right;
Serial.print(self_right[index]);
}
}
}
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')
{
//sendData(1);
//Serial.println("SV2.5-HV2.0");
Serial.print("SV2.5-HV2.0\r");
}
else if(type=='O'&&command.charAt(1)=='1')
{
open();
}
else if(type=='C')
{
close();
}
else if(type=='S')
{
setSpeed(command.charAt(1));
}
else if(type=='T' || type=='t')
{
sendFrameData(command,type);
}
else
{
sendData(0);
}
}
void sendSelfData()
{
}
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==1)
{
int level=(int)(c-'0');
if(level<0||level >8)
sendData(0);
else
{
sendData(1);
}
}
else
{
sendData(0);
}
}
void close()
{
if(state==0)
{
state=1;
sendData(1);
}
else
{
sendData(0);
}
}
void open()
{
if(state==1)
{
state=0;
sendData(1);
}
else
{
sendData(0);
}
}
void sendData(int flag)
{
if(flag == 0){
//Serial.println("Fail!");
Serial.write((char)7);
}
else
{
//Serial.println("Suceed!");
Serial.write("\r");
}
}