STM32G070RBT6基于Arduino框架下串口数据接收使用示例
相关篇《STM32G070RBT6基于Arduino串口的使用》🌻串口接收数据转发
📑通过使能串口1,并将串口引脚指定到PA10,PA9上。将从串口接收到的数据,转发出去。
#define led1 PC2#define led2 PC3#define button1 PC0#define button2 PC1String comdata = "";bool mark = false;// RX TXHardwareSerial Serial1(PA10, PA9);// the setup function runs once when you press reset or power the boardvoid setup() {Serial.begin(115200);// initialize digital pin LED_BUILTIN as an output.pinMode(led1, OUTPUT);pinMode(led2, OUTPUT);digitalWrite(led1, LOW);digitalWrite(led2, HIGH);pinMode(button1, INPUT_PULLUP);pinMode(button2, INPUT_PULLUP);}void loop() {while (Serial.available() > 0){comdata += char(Serial.read());delay(2);mark = 1;//标志位}if (mark == true){//标志位判断Serial.println(comdata);mark = 0;//使用完数据,记得一定要清空标志位,等待下一次数据接收comdata = "";//使用完数据,清空变量的数据,等待下一次接收digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ; digitalToggle(led2);}}
🌼对接收内容中指定字符串段内容进行判断
⚡需要注意的是,字符串与字符串进行比较时,使用
pareTo()
,的返回值如果是0则相同,如果是1则不相同。
#define led1 PC2#define led2 PC3#define button1 PC0#define button2 PC1String comdata = "";bool mark = false;// RX TXHardwareSerial Serial1(PA10, PA9);// the setup function runs once when you press reset or power the boardvoid setup() {Serial.begin(115200);// initialize digital pin LED_BUILTIN as an output.pinMode(led1, OUTPUT);pinMode(led2, OUTPUT);digitalWrite(led1, LOW);digitalWrite(led2, HIGH);pinMode(button1, INPUT_PULLUP);pinMode(button2, INPUT_PULLUP);}// the loop function runs over and over again forevervoid loop() {while (Serial.available() > 0){comdata += char(Serial.read());delay(2);mark = 1;//标志位}if (mark == true){//标志位判断Serial.println(comdata);String str = comdata.substring(0,5);Serial.println(str);// if(str == "hello")Serial.println("Yes,is hello");if(pareTo("hello")==0)Serial.println("Yes,is hello");//同上mark = 0;//使用完数据,记得一定要清空标志位,等待下一次数据接收comdata = "";//使用完数据,清空变量的数据,等待下一次接收digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ; digitalToggle(led2);}}
串口打印
通过serialEvent()
事件来接收数据
serialEvent()
事件函数可以不用放在主循环loop中。该函数会运行在loop与loop之间响应。如果主循环loop函数中有有较多的处理任务运行时,不推荐使用这个函数来处理接收数据,否则加得不到及时响应。
#define led1 PC2#define led2 PC3#define button1 PC0#define button2 PC1String comdata = "";bool mark = false;// RX TXHardwareSerial Serial1(PA10, PA9);// the setup function runs once when you press reset or power the boardvoid setup() {Serial.begin(115200);// initialize digital pin LED_BUILTIN as an output.pinMode(led1, OUTPUT);pinMode(led2, OUTPUT);digitalWrite(led1, LOW);digitalWrite(led2, HIGH);pinMode(button1, INPUT_PULLUP);pinMode(button2, INPUT_PULLUP);}// the loop function runs over and over again forevervoid loop() {if (mark == true){//标志位判断Serial.println(comdata);String str = comdata.substring(0,5);Serial.println(str);// if(str == "hello")Serial.println("Yes,is hello");if(pareTo("hello")==0)Serial.println("Yes,is hello");mark = 0;//使用完数据,记得一定要清空标志位,等待下一次数据接收comdata = "";//使用完数据,清空变量的数据,等待下一次接收digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ; digitalToggle(led2);}}void serialEvent() {while (Serial.available() > 0){comdata += char(Serial.read());delay(2);mark = 1;//标志位}}