forked from hidaba/PylontechMonitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_uart.cpp
More file actions
292 lines (225 loc) · 7.01 KB
/
Copy pathpy_uart.cpp
File metadata and controls
292 lines (225 loc) · 7.01 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "py_uart.h"
#include "py_log.h"
#include "config.h"
#include <string.h>
// Pins
#define BAT_RX_PIN 16
#define BAT_TX_PIN 17
// Static RX buffer for lossless reception
static char rxBuf[8192];
static size_t rxPos = 0;
static int g_invalidCount = 0;
static int enterCount = 0;
// Auto wakeup counter
static int wakeupCounter = 2;
// Frame queue comes from the .ino
extern QueueHandle_t frameQueue;
volatile int consoleTicket = 0;
volatile int consoleTicketFrameReady = 0;
String consolePendingCommand = "";
// ---------------------------------------------------------
static bool isValidFrame(const String& f) {
if (f.indexOf("@") < 0) {
Log(LOG_DEBUG, "UART: invalid frame → missing '@'");
return false;
}
if (f.indexOf("$$") < 0) {
Log(LOG_DEBUG, "UART: invalid frame → missing '$$'");
return false;
}
if (f.length() < 40) {
Log(LOG_DEBUG, "UART: invalid frame → too short (" + String(f.length()) + ")");
return false;
}
int lines = 0;
for (int i = 0; i < f.length(); i++)
if (f[i] == '\n') lines++;
if (lines < 3) return false;
if (f.indexOf("Press [Enter]") >= 0 &&
f.indexOf("Remote command:") < 0)
return false;
return true;
}
// ---------------------------------------------------------
void PyUart::begin(int rx, int tx) {
rxPin = rx;
txPin = tx;
Serial2.setRxBufferSize(4096);
Serial2.begin(115200, SERIAL_8N1, rxPin, txPin);
delay(50);
Log(LOG_INFO, "UART: begin() RX=" + String(rxPin) + " TX=" + String(txPin));
commReady = true;
busy = false;
frameValid = false;
lastCommand = "";
lastRawFrame = "";
lastCommandId = 0;
g_invalidCount = 0;
wakeupCounter = 2;
state = UART_IDLE;
cmdStart = 0;
rxPos = 0;
rxBuf[0] = '\0';
}
// ---------------------------------------------------------
void PyUart::switchBaud(int newRate) {
Log(LOG_DEBUG, "UART: switchBaud(" + String(newRate) + ")");
Serial2.flush();
delay(20);
Serial2.end();
delay(20);
Serial2.begin(newRate, SERIAL_8N1, rxPin, txPin);
delay(20);
}
// ---------------------------------------------------------
void PyUart::wakeUpConsole() {
Log(LOG_WARN, "UART: wakeUpConsole() triggered");
commReady = false;
switchBaud(1200);
Serial2.write("~20014682C0048520FCC3\r");
delay(500);
byte nl[] = {0x0E, 0x0A};
switchBaud(115200);
for (int i = 0; i < 3; i++) {
Serial2.write(nl, 2);
delay(200);
if (Serial2.available()) {
while (Serial2.available()) Serial2.read();
break;
}
}
commReady = true;
g_invalidCount = 0;
Log(LOG_INFO, "UART: wakeUpConsole complete → commReady=true");
Serial2.write("pwr\n");
Log(LOG_INFO, "UART: auto-PWR after wakeUp");
}
// ---------------------------------------------------------
bool PyUart::sendCommand(const char* cmd, int cmdId) {
enterCount = 0;
if (!commReady) {
Log(LOG_WARN, "UART: commReady=false → skip command");
return false;
}
if (busy || state != UART_IDLE) {
Log(LOG_WARN, "UART: busy");
return false;
}
while (Serial2.available()) Serial2.read();
delay(10);
lastCommandId = cmdId;
lastCommand = String(cmd);
frameValid = false;
rxPos = 0;
rxBuf[0] = '\0';
Log(LOG_DEBUG, "UART TX: '" + lastCommand + "' (id=" + String(lastCommandId) + ")");
Serial2.write(cmd);
Serial2.write("\n");
Serial2.flush();
busy = true;
state = UART_WAITING;
cmdStart = millis();
return true;
}
// ---------------------------------------------------------
void PyUart::loop() {
static unsigned long lastByteTime = 0;
static bool sawEnterPrompt = false;
// 1. Collect bytes
while (Serial2.available()) {
char c = (char)Serial2.read();
if (rxPos < sizeof(rxBuf) - 1) {
rxBuf[rxPos++] = c;
rxBuf[rxPos] = '\0';
}
lastByteTime = millis();
if (state == UART_WAITING)
state = UART_COLLECTING;
if (!sawEnterPrompt &&
strstr(rxBuf, "Press [Enter] to be continued") != nullptr)
{
sawEnterPrompt = true;
}
}
if (state == UART_IDLE)
return;
// 2. Timeout
if (millis() - cmdStart > 1500) {
Log(LOG_WARN, "UART: timeout");
busy = false;
state = UART_IDLE;
rxPos = 0;
rxBuf[0] = '\0';
sawEnterPrompt = false;
UartFrame f;
f.type = FRAME_UNKNOWN;
f.module = 0;
f.commandId = lastCommandId;
xQueueSend(frameQueue, &f, 0);
return;
}
// 3. Wait for quiet time
if (millis() - lastByteTime < 150)
return;
// 4. ENTER handling for multi-part frames
if (sawEnterPrompt &&
millis() - lastByteTime > 200 &&
strstr(rxBuf, "$$") == nullptr)
{
if (enterCount < 2) {
Serial2.write("\r");
Log(LOG_DEBUG, "UART: sending ENTER (safe)");
sawEnterPrompt = false;
enterCount++;
return;
}
Log(LOG_WARN, "UART: ENTER limit reached → sending 'q'");
lastRawFrame = String(rxBuf, rxPos);
frameValid = false;
busy = false;
state = UART_IDLE;
rxPos = 0;
rxBuf[0] = '\0';
sawEnterPrompt = false;
UartFrame f;
f.type = FRAME_UNKNOWN;
f.module = 0;
f.commandId = lastCommandId;
xQueueSend(frameQueue, &f, 0);
return;
}
// 5. Check for frame end
bool hasEndToken = (strstr(rxBuf, "$$") != nullptr);
if (!hasEndToken)
return;
// 6. Frame complete
Log(LOG_DEBUG, "UART: frame complete (" + String(rxPos) + " bytes)");
lastRawFrame = String(rxBuf, rxPos);
frameValid = isValidFrame(lastRawFrame);
busy = false;
state = UART_IDLE;
rxPos = 0;
rxBuf[0] = '\0';
sawEnterPrompt = false;
UartFrame f;
f.type = FRAME_UNKNOWN;
f.module = 0;
f.commandId = lastCommandId;
if (lastCommand == "pwr") {
f.type = FRAME_PWR;
}
else if (lastCommand.startsWith("bat")) {
f.type = FRAME_BAT;
f.module = lastCommand.substring(3).toInt();
}
else if (lastCommand.startsWith("stat")) {
f.type = FRAME_STAT;
f.module = lastCommand.substring(4).toInt();
}
// Mark console frame ready if this command came from console
if (lastCommand == consolePendingCommand) {
consoleTicketFrameReady = consoleTicket;
consolePendingCommand = "";
}
xQueueSend(frameQueue, &f, 0);
}