SuperAPI AI 工具集

esp32-serial-logging

编程与开发

查看项目

更新于 10-15 12:05

技能介绍

用途

本技能用于捕获 ESP32(或任意微控制器)的串口输出并写入文件,实现实时监控与分析,主要面向嵌入式开发调试场景。

设备端日志输出

ESP-IDF 框架

使用 esp_log 头文件与日志宏输出不同级别日志:

#include "esp_log.h"

static const char* TAG = "MyComponent";

void my_function() {
    ESP_LOGI(TAG, "Info message");
    ESP_LOGW(TAG, "Warning: value=%d", some_value);
    ESP_LOGE(TAG, "Error occurred");
    ESP_LOGD(TAG, "Debug details");
}

Arduino 框架

void setup() {
    Serial.begin(115200);
}

void loop() {
    Serial.println("Status: running");
    Serial.printf("Sensor: %d\n", analogRead(A0));
    delay(1000);
}

主机端捕获日志

查找串口并开始捕获:

# Find serial port
PORT=$(ls /dev/cu.usbmodem* /dev/ttyUSB* /dev/ttyACM* 2>/dev/null | head -1)
echo "Found port: $PORT"

# Configure and start capture
stty -f "$PORT" 115200 raw -echo 2>/dev/null || stty -F "$PORT" 115200 raw -echo
cat "$PORT" >> /tmp/device.log &
echo "Logging to /tmp/device.log (PID: $!)"

监控日志

# Real-time monitoring
tail -f /tmp/device.log

# Filter specific patterns
tail -f /tmp/device.log | grep -E "ERROR|WiFi|Button"

搜索错误

# Find crashes and errors
grep -E "ERROR|crash|overflow|panic|assert|Backtrace" /tmp/device.log

# Find reboots (look for boot messages or uptime resets)
grep -E "boot:|rst:|Uptime: [0-9] sec" /tmp/device.log

调试工作流

  1. 复现问题前清空日志:> /tmp/device.log
  2. 复现问题
  3. 分析捕获的日志:cat /tmp/device.log

常见波特率

设备 波特率
ESP32(默认) 115200
ESP32(快速) 460800
Arduino 9600
STM32 115200

停止日志

pkill -f "cat /dev/cu.usbmodem"
pkill -f "cat /dev/ttyUSB"

故障排查

  • 找不到端口:检查 USB 连接,尝试 ls /dev/cu.* /dev/tty.*
  • 权限被拒绝:在 Linux 上将用户加入 dialout 组
  • 输出乱码:波特率设置错误

适用场景与限制

适用于 ESP32 及类似微控制器的串口调试、崩溃与重启分析、运行状态持续观察。资料未提供图形界面、跨平台封装工具或自动化分析能力,相关功能暂未提供。