You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wemos D1 Mini西里尔字母转摩尔斯电码代码修复请求

问题排查与代码修复方案

核心问题根源

  1. 原代码的摩尔斯码映射表仅覆盖英文字母,未包含西里尔字母的键值对
  2. 网页提交西里尔字符时,若未指定UTF-8编码,会导致Wemos D1 Mini端解析为乱码
  3. 字符大小写未统一,导致西里尔小写字母无法匹配映射表

具体修复步骤

1. 完善西里尔字母的摩尔斯码映射表

替换原有的映射表,加入标准西里尔字母对应的摩尔斯码(确保大小写统一为大写):

// 英文字母+西里尔字母+数字的摩尔斯码映射
const String morseCode[] = {
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", // A-J
  "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", // K-T
  "..-", "...-", ".--", "-..-", "-.--", "--..", // U-Z
  ".-", "-...", ".--", "--.", "-..", ".", "..-.", "--..", "..", ".---", // А-Я(西里尔标准编码)
  "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
  "..-", "..-.", "....", "-.-.", "---.", "----", "--.-", "--.--", "-.--", "-..-",
  "..-..", "..--", ".-.-",
  "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." // 0-9
};

// 对应字符集(大写英文字母+大写西里尔字母+数字)
const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ0123456789";

2. 统一字符处理逻辑

在接收网页数据后,先转为大写并验证字符有效性,避免乱码或不匹配:

void handlePostRequest() {
  String inputText = server.arg("text");
  inputText.toUpperCase(); // 统一转为大写,匹配映射表

  for (int i = 0; i < inputText.length(); i++) {
    char currentChar = inputText.charAt(i);
    int charIndex = -1;

    // 在字符集中查找对应索引
    for (int j = 0; j < sizeof(chars)-1; j++) {
      if (chars[j] == currentChar) {
        charIndex = j;
        break;
      }
    }

    if (charIndex != -1) {
      blinkMorseCode(morseCode[charIndex]); // 调用LED闪烁函数
    } else if (currentChar == ' ') {
      delay(2000); // 空格对应2秒停顿
    }
    // 未知字符直接跳过
  }
  server.send(200, "text/html", "已开始输出摩尔斯码");
}

// LED闪烁实现示例
void blinkMorseCode(String code) {
  for (int i = 0; i < code.length(); i++) {
    digitalWrite(LED_BUILTIN, HIGH);
    if (code[i] == '.') {
      delay(200); // 点:200ms亮
    } else if (code[i] == '-') {
      delay(600); // 划:600ms亮
    }
    digitalWrite(LED_BUILTIN, LOW);
    delay(200); // 字符内间隔200ms
  }
  delay(600); // 字符间间隔600ms
}

3. 确保网页UTF-8编码

修改网页HTML,添加UTF-8声明,避免提交时字符乱码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>西里尔摩尔斯码转换器</title>
</head>
<body>
  <form method="POST" action="/">
    <input type="text" name="text" placeholder="输入英文或西里尔字母">
    <button type="submit">触发LED闪烁</button>
  </form>
</body>
</html>

4. 调试验证

添加串口打印,确认字符是否被正确识别:

void setup() {
  Serial.begin(115200);
  // 其他初始化代码...
}

// 在handlePostRequest中添加打印
Serial.print("接收字符: ");
Serial.println(currentChar);
Serial.print("匹配索引: ");
Serial.println(charIndex);

上传代码后打开串口监视器,提交西里尔字母,查看打印信息是否正常。

内容的提问来源于stack exchange,提问作者Kirsten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:13:15