mcpField
E
esp32-serial-commands

esp32-serial-commands

github.com/h1d测试与质量11279 次安装40 周前更新
代码仓库

关于 esp32-serial-commands

用于通过 USB 串口向 ESP32 发送命令,以模拟按键和用户操作,适合设备测试。支持带参数的命令,可用于压力测试、稳定性测试及日志监控。

主要功能

  1. 01通过 USB 串口发送命令
  2. 02模拟按键和用户操作
  3. 03支持带参数的命令
  4. 04运行压力与稳定性测试
  5. 05结合日志检查设备反馈

适用场景

  1. 01自动重复发送按键命令
  2. 02长时间运行设备稳定性测试
  3. 03检查命令处理结果和日志

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/h1d/agent-skills-esp32 --skill esp32-serial-commands

安装后在 Claude Code 中输入/esp32-serial-commands确认已加载。

详细介绍

ESP32 串口命令模拟

概述

直接向串口发送命令,以模拟按键操作和用户操作。支持通过 USB 串口连接。

ESP32 设置(设备端)

ESP-IDF 框架
#include "esp_log.h"
#include <string.h>

static const char* TAG = "SerialCmd";

void serial_command_task(void* arg) {
    char buf[64];
    int idx = 0;

    while (1) {
        int c = getchar();
        if (c == EOF) {
            vTaskDelay(pdMS_TO_TICKS(10));
            continue;
        }

        if (c == '\n' || c == '\r') {
            buf[idx] = '\0';
            if (idx > 0) {
                ESP_LOGI(TAG, "Command: %s", buf);

                if (strcmp(buf, "PRESS") == 0) {
                    button_callback();
                } else if (strncmp(buf, "SCREEN:", 7) == 0) {
                    int screen = atoi(buf + 7);
                    go_to_screen(screen);
                }
            }
            idx = 0;
        } else if (idx < sizeof(buf) - 1) {
            buf[idx++] = c;
        }
    }
}

// In app_main():
// xTaskCreate(serial_command_task, "serial_cmd", 2048, NULL, 5, NULL);
Arduino 框架
void setup() {
    Serial.begin(115200);
}

void loop() {
    if (Serial.available()) {
        String cmd = Serial.readStringUntil('\n');
        cmd.trim();

        Serial.printf("Command: %s\n", cmd.c_str());

        if (cmd == "PRESS") {
            button_callback();
        } else if (cmd.startsWith("SCREEN:")) {
            int screen = cmd.substring(7).toInt();
            go_to_screen(screen);
        } else if (cmd == "STATUS") {
            Serial.println("OK");
        }
    }
}

主机端 - 发送命令

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

# Send a command
echo "PRESS" > "$PORT"

常见命令模式

# Simple command
echo "PRESS" > "$PORT"

# Command with parameter
echo "PRESS:3" > "$PORT"      # Press 3 times
echo "SCREEN:2" > "$PORT"     # Go to screen 2
echo "MODE:debug" > "$PORT"   # Set debug mode

自动化测试

压力测试
PORT=$(ls /dev/cu.usbmodem* /dev/ttyUSB* 2>/dev/null | head -1)
for i in {1..100}; do
  echo "PRESS" > "$PORT"
  echo "Sent press $i"
  sleep 2
done
长时间稳定性测试
PORT=$(ls /dev/cu.usbmodem* /dev/ttyUSB* 2>/dev/null | head -1)
END=$(($(date +%s) + 3600))  # 1 hour
count=0

while [ $(date +%s) -lt $END ]; do
  count=$((count + 1))
  echo "PRESS" > "$PORT"
  echo "[$(date +%H:%M:%S)] Press $count"
  sleep 30
done

echo "Total: $count presses"
通过日志监控进行测试

终端 1 - 监控日志:

tail -f /tmp/device.log

终端 2 - 发送命令:

PORT=$(ls /dev/cu.usbmodem* /dev/ttyUSB* 2>/dev/null | head -1)
echo "PRESS" > "$PORT"

提示

  • 时序:在命令之间添加延迟,让设备有时间处理
  • 反馈:检查串口日志,确认命令已收到
  • 错误处理:设备应回显状态(OK/ERROR)
  • 结合日志记录:测试时务必监控日志

评论

支持 Markdown