Serial communication program of STM32 board based on Ardunio

One, environment settings

Software: Arduino IDE
Hardware: USB to serial port, STM32 development board
Environment configuration: Reference: https://blog.csdn.net/hot_ant/article/details/108122317

Second, the code and specific implementation

Code:

void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(115200);//设置串口的波特率
  pinMode(PC2,OUTPUT);//PC2是蓝色的,PC2表示LED的接口,还有一个是PC3

}
int flag=1;//串口发送标志位,1表示发送,0表示停止发送
void loop() {
    
    
  // put your main code here, to run repeatedly:
  if (flag == 1)
  {
    
    
    Serial.println("Hello World!");
    digitalWrite(PC2, HIGH);   //LED灭
    delay(500);   // 延时
    digitalWrite(PC2, LOW);   //LED亮
    delay(500);   //延时
  }
  stopSendData();
}

void stopSendData()
{
    
    
  String stopflag="stop!";//停止发送数据信号
  String receivedata="";//存放接受到的数据
  while(Serial.available()>0)//判断串口是否还有数据
  {
    
    
    receivedata=Serial.readString();//读取接收到的数据
  }
  if(receivedata==stopflag)//判断接收到的数据是否为停止发送数据的信号
  {
    
    
    flag=0;
  }
}

Connect the computer to the development board, set the chip model, upload method and port,
then compile, upload, and run
Insert picture description here
Part of the renderings:
Insert picture description here
reference: https://blog.csdn.net/qq_43279579/article/details/111086766
https://www.cnblogs. com/chdfelix/p/9637648.html

Guess you like

Origin blog.csdn.net/xianyudewo/article/details/111178702