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内注意多字节的缓存。