-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_send.cpp
178 lines (163 loc) · 4.36 KB
/
cmd_send.cpp
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
#include "arduino_cli.h"
#ifdef HAVE_HWSERIAL1
/**
* Send data to another Serial Port
*/
void cmd_send(String *cmd, String *args) {
// Make sure cmd parameter isn't null
if(cmd == NULL) {
Serial.println("ERROR: 1st param of cmd_tail must not NULL");
return;
}
// Show usage if there is no argument
if(args == NULL) {
help(cmd);
return;
}
// Prepare variables
String messageStr = "";
String baudrateStr = "";
// Tokenize arguments
byte argIndex = 0;
bool isInQuote = false;
bool isEscaping = false;
for(byte i = 0; i < args->length(); i++) {
char each = args->charAt(i);
if(each == '"' && !isEscaping) {
isInQuote = !isInQuote;
} else if(each == '\\' && isInQuote && !isEscaping) {
isEscaping = true;
continue;
} else if(each == ' ' && !isInQuote) {
if(argIndex >= 1) {
Serial.println("Excessive arguments");
Serial.println("");
help(cmd);
return;
} else {
argIndex++;
}
} else {
if(isEscaping) {
if(each == 'n') {
each = '\n';
} else if(each == 'r') {
each = '\r';
} else if(each == 't') {
each = '\t';
}
}
switch(argIndex) {
case 0:
messageStr += each;
break;
case 1:
baudrateStr += each;
break;
}
}
isEscaping = false;
}
// Abort if not enough argument
if(args->length() <= 0) {
help(cmd);
return;
}
// Show error on unclosed quote
if(isInQuote) {
Serial.println("ERROR: Unclosed quote (\")");
Serial.println("");
return;
}
// Parse baudrate
int baudrate = baudrateStr.toInt();
if(baudrate <= 0) {
if(baudrateStr.length() > 0) {
// CONDITION: Baudrate is invalid
Serial.println("Baudrate must number and more than 0");
Serial.println("");
help(cmd);
return;
} else {
// CONDITION: Baudrate is not specified, fallback to default
baudrate = 115200;
}
}
// Initialize second serial port
Serial1.begin(baudrate);
byte countdown = 10;
while(!Serial1) {
delay(1000);
// Cleanup first serial, just in case accidental input
int n = Serial.available();
for(int i = 0; i < n; i++) {
Serial.read();
}
if(countdown > 0) {
countdown--;
} else {
Serial.println("Cannot connect to 2nd Serial Port");
Serial.println("");
return;
}
}
// Send message based on data type
if(args->charAt(0) == '"') {
// CONDITION: Message enforced as string
Serial1.print(messageStr);
} else if(messageStr.length() > 1 && messageStr.charAt(0) == '0') {
// CONDITION: Message is raw bytes
String prefix = messageStr.substring(0,2);
byte radix = 0;
if(prefix == "0b") {
radix = 2;
} else if(prefix == "0o") {
radix = 8;
} else if(prefix == "0d") {
radix = 10;
} else if(prefix == "0x") {
radix = 16;
}
long long messageBytes;
if(radix > 0) {
messageBytes = strtol(&messageStr[2], NULL, radix);
} else {
messageBytes = strtol(&messageStr[0], NULL, 0);
}
Serial1.write((byte)(messageBytes & 0xff));
if(messageBytes < 0 || messageBytes > 0xff) {
Serial1.write((byte)((messageBytes >> 8) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffff) {
Serial1.write((byte)((messageBytes >> 16) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffffff) {
Serial1.write((byte)((messageBytes >> 24) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffffffff) {
Serial1.write((byte)((messageBytes >> 32) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffffffffff) {
Serial1.write((byte)((messageBytes >> 40) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffffffffffff) {
Serial1.write((byte)((messageBytes >> 48) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffffffffffffff) {
Serial1.write((byte)((messageBytes >> 56) & 0xff));
}
if(messageBytes < 0 || messageBytes > 0xffffffffffffffff) {
Serial1.write((byte)((messageBytes >> 64) & 0xff));
}
Serial1.flush();
} else {
// CONDITION: Message is either number or plain string
Serial1.print(messageStr);
}
// Disconnect second serial port
Serial1.end();
// Print response
Serial.println("ok");
Serial.println("");
}
#endif