ESP32-C3热点WIFI与STM32通讯程序

文章描述了如何使用STM32F103VCT6与ESP32建立串口连接,通过ESP32创建热点,实现在PC和STM32之间的无线WiFiTCP传输,重点关注多字节缓存的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STM32F103VCT6的串口与ESP32的串口0 通讯,波特率256000;

ESP32建立热点,PC通过访问固定IP建立客户端;

最终实现PC与STM32之间无线WIFI.TCP传输;

4014个字节通讯速度:0.1s内完成.

 串口缓存,大家自行改为环形缓存。勿喷!

//===================上电初始化执行一次=================================

void setup() {

  Serial.begin(256000);

  Serial.onReceive(Serial_InterruptCallback);

   // You can remove the password parameter if you want the AP to be open.

  // a valid password must have more than 7 characters

  if (!WiFi.softAP(ssid, password)) {

    log_e("Soft AP creation failed.");

    while (1);

  }  //----------------------------------------

  server.begin();

  bit_RxFlag=false;

  Buff=0;

  SendLength=0;

  memset(SendBuff,0,sizeof(SendBuff));

  memset(SerialBuff,0,sizeof(SerialBuff));

}

//=============================循环执行函数=============================

void loop() {

  WiFiClient client;

  client = server.available();  // listen for incoming clients

  if (client) {  // if you get a client, 

    Buff =0;

    bit_McutoPCFlag =false;

    bit_SerialDataFlag=false;

    // Serial1.println("New Client.");// print a message out the Serial1 port

    // String currentLine = "";  // make a String to hold incoming data from the client

    while (client.connected())

    {  // loop while the client's connected

       //--------------------------------------------------------

       if(bit_SerialDataFlag)

       {
         i=0;

         while(true){

          if(     (SerialBuff[i-5]==0xbb)
                &&(SerialBuff[i-4]==0xcc)
                &&(SerialBuff[i-3]==0xdd)
                &&(SerialBuff[i-2]==0xee))

            {

                SendLength= SerialBuff[i-1]<<8 | SerialBuff[i];

                //memset(SendlBuff,0,sizeof(SendlBuff));

                for(int j=0;j<(SendLength+6);j++){

                  SendBuff[j]= SerialBuff[i-5+j];

                }

                SendLength+=6;

                bit_McutoPCFlag =true;

                break;

            }

          i++;

        }

        bit_SerialDataFlag=false;

        Buff=0;

       }

      //------------------------MCU-> PC---------------------------

      if (bit_McutoPCFlag)

      {

        client.write(SendBuff,SendLength);

         SendLength=0;

         bit_McutoPCFlag = false;

      }

      //----------------------PC-> MCU-----------------------

      if (client.available()) {  // if there's bytes to read from the client,

       
       //int WiFiClient::read(uint8_t *buf, size_t size)

        byte c = client.read();  // read  byte, then

        Serial.write(c);         // print  out the Serial monitor

      }

    }

    // close the connection:

    client.stop();

    //Serial1.println("Client Disconnected.");

  }

}

Serial_InterruptCallback内注意多字节的缓存。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值