Wokwi@通过RFC2217 TCP 服务器连接到模拟微控制器串口

wokwi 的VS Code Extension配置

wokwi.toml

js 复制代码
[wokwi]
version = 1
elf = ".pio/build/esp32/firmware.elf"
firmware = ".pio/build/esp32/firmware.bin"
rfc2217ServerPort = 4000

platformio.ini

bash 复制代码
[env:esp32]
platform = espressif32
framework = arduino
board = esp32dev

diagram.json

json 复制代码
{
  "version": 1,
  "author": "Uri Shaked",
  "editor": "wokwi",
  "parts": [
    { "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": -4.76, "attrs": {} },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 109.55,
      "left": -76.8,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 119.15,
      "left": -76.8,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 34.8,
      "left": -111.4,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 63.6,
      "left": -140.2,
      "attrs": { "color": "green" }
    }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "r1:1", "led1:A", "green", [ "v0.07", "h-10" ] ],
    [ "r2:1", "led2:A", "green", [ "h-38.4", "v-19.2" ] ],
    [ "esp:GND.1", "led2:C", "black", [ "h0" ] ],
    [ "esp:GND.1", "led1:C", "black", [ "h0" ] ],
    [ "esp:26", "r1:2", "green", [ "h0" ] ],
    [ "r2:2", "esp:27", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

main.cpp

c 复制代码
#include "Arduino.h"

void setup(void) {
    Serial.begin(115200);
    // 修正原注释的小问题,Arduino的Serial不是localhost,这里改为更符合实际的提示
    Serial.println("Serial communication started (Baud rate: 115200)");
}

void loop(void) {
    static int a = 0;
    delay(1000);
    // 原有功能:定时输出AAA和计数
    Serial.printf("AAA %d \r\n", a++);
    // 读取并打印串口接收的数据
    if (Serial.available() > 0) { // 检查是否有串口数据可读取
        // 读取接收到的字符串(直到换行符,适配Serial Monitor的默认发送格式)
        String receivedData = Serial.readStringUntil('\n');
        // 去除字符串中的回车、换行等空白字符,避免输出多余符号
        receivedData.trim();
        // 打印接收到的数据
        Serial.print("Received data: ");
        Serial.println(receivedData);
    }
}

PC 环境准备

bash 复制代码
# 控制台打开 Anaconda Prompt
(base) C:\Users\PC>conda env list
# conda environments:
#
base                  *  D:\soft\Anaconda3
mingzynq                 D:\soft\Anaconda3\envs\mingzynq
platformio               D:\soft\Anaconda3\envs\platformio
untitled1                D:\soft\Anaconda3\envs\untitled1


(base) C:\Users\PC>conda activate platformio

(platformio) C:\Users\PC>d:

(platformio) D:\>cd D:\workspace\gitee\esp32-http-server-main

(platformio) D:\workspace\gitee\esp32-http-server-main>pip3 install pyserial
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simpl
Requirement already satisfied: pyserial in d:\soft\anaconda3\envs\platformio\lib\site-packages (3.5)

(platformio) D:\workspace\gitee\esp32-http-server-main>python fc2217_client.py
Connected. Listening...
AAA 610
AAA 611
Received data: write from pc
AAA 612
Received data: write from pc
AAA 613
Received data: write from pc
AAA 614
Received data: write from pc

不想装python可以直接用clion自带的

bash 复制代码
# 64位python
&"D:\Program Files\JetBrains\CLion 2023.2\bin\lldb\win\x64\bin\python.exe" fc2217_client.py

# 32位python [测试32位动态库用]
 & "D:\Program Files\JetBrains\CLion 2023.2\bin\lldb\win\x86\bin\python.exe" fc2217_client.py

rfc2217_client.py

pc上运行

py 复制代码
import serial

# 连接 Wokwi 提供的 rfc2217 虚拟串口
ser = serial.serial_for_url("rfc2217://localhost:4000?ign_set_control=1",
                            baudrate=115200,
                            timeout=0.05)

print("Connected. Listening...")

try:
    while True:
        data = ser.read(1024)
        if data:
            print(data.decode(errors="ignore"), end="")
            ser.write(b"write from pc\r\n")
except KeyboardInterrupt:
    print("\nStopped.")
finally:
    ser.close()

或用带tcpClient的串口助手

相关推荐
多看多敲多思考19 小时前
华润微CS32ME10 MCU使用教程(2)---CS32ME10之UART串口模块使用
stm32·单片机·嵌入式硬件·mcu
lzj_pxxw1 天前
W25Q64存储芯片 软件设计刚需常识
stm32·单片机·嵌入式硬件·mcu·学习
电子科技圈2 天前
芯科科技在蓝牙亚洲大会展示汽车与边缘AI前沿蓝牙创新技术, 解锁车用、家居、健康及工商业等应用场景
人工智能·科技·嵌入式硬件·mcu·物联网·网络安全·汽车
来生硬件工程师2 天前
【程序库】 MutiButton 按键库
c语言·笔记·stm32·单片机·mcu·嵌入式实时数据库
多看多敲多思考2 天前
华润微CS32ME10 MCU使用教程(1)---CS32ME10之GPIO使用
c语言·stm32·单片机·嵌入式硬件·mcu
国产芯片设计3 天前
小家电驱动开发实战:远乐YL1628在电饭煲显示面板的应用与调试
单片机·嵌入式硬件·mcu·51单片机·硬件工程
国产芯片设计3 天前
DIY实战|0.8寸WiFi自动授时电子钟,国产数码管驱动芯片方案分享
stm32·单片机·mcu·51单片机·硬件工程
嵌入式的飞鱼5 天前
SD NAND vs eMMC:嵌入式存储方案怎么选?
嵌入式硬件·mcu·sd nand
[J] 一坚6 天前
嵌入式高手C
c语言·开发语言·stm32·单片机·mcu·51单片机·iot
EVERSPIN9 天前
基于MCU CH32X035 Type-C PD显示器方案
单片机·mcu·计算机外设