Buffer UART bytes before UDP send — fix MAVLink frame fragmentation
Accumulate UART bytes and flush to UDP only when buffer is full (512B) or after 3ms of UART inactivity (end-of-frame gap). Fixes mavp2p "packet too short" / "invalid magic byte" errors caused by sending partial MAVLink frames in separate UDP packets. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
31
src/main.cpp
31
src/main.cpp
@ -40,6 +40,13 @@ uint32_t bytesUartToUdp = 0;
|
||||
uint32_t bytesUdpToUart = 0;
|
||||
unsigned long lastStatsTime = 0;
|
||||
|
||||
// ── Buffer UART → UDP ────────────────────────────────────
|
||||
#define UART_BUF_SIZE 512
|
||||
#define UART_FLUSH_MS 3
|
||||
uint8_t uartBuf[UART_BUF_SIZE];
|
||||
int uartBufLen = 0;
|
||||
unsigned long uartLastByteTime = 0;
|
||||
|
||||
// ── LED non bloquante ─────────────────────────────────────
|
||||
void updateLED() {
|
||||
unsigned long elapsed = millis() - ledStateStart;
|
||||
@ -135,21 +142,23 @@ void loop() {
|
||||
|
||||
if (!wifiConnected) return;
|
||||
|
||||
// UART → UDP (H743 → Jetson)
|
||||
if (MavSerial.available()) {
|
||||
uint8_t buf[512];
|
||||
int len = 0;
|
||||
while (MavSerial.available() && len < (int)sizeof(buf)) {
|
||||
buf[len++] = MavSerial.read();
|
||||
// UART → UDP (H743 → Jetson) — accumulation puis flush
|
||||
while (MavSerial.available() && uartBufLen < UART_BUF_SIZE) {
|
||||
uartBuf[uartBufLen++] = MavSerial.read();
|
||||
uartLastByteTime = millis();
|
||||
}
|
||||
if (len > 0) {
|
||||
|
||||
bool flushNow = uartBufLen > 0 &&
|
||||
(uartBufLen >= UART_BUF_SIZE || millis() - uartLastByteTime >= UART_FLUSH_MS);
|
||||
|
||||
if (flushNow) {
|
||||
udp.beginPacket(TARGET_IP, UDP_PORT);
|
||||
udp.write(buf, len);
|
||||
udp.write(uartBuf, uartBufLen);
|
||||
udp.endPacket();
|
||||
bytesUartToUdp += len;
|
||||
bytesUartToUdp += uartBufLen;
|
||||
setLedState(LED_ACTIVITY);
|
||||
Serial.printf("[UART->UDP] %d bytes\n", len);
|
||||
}
|
||||
Serial.printf("[UART->UDP] %d bytes\n", uartBufLen);
|
||||
uartBufLen = 0;
|
||||
}
|
||||
|
||||
// UDP → UART (Jetson → H743)
|
||||
|
||||
Reference in New Issue
Block a user