arduino學習筆記001.串口接收


一、簡介

  Arduino單片機處理串口數據接收具有自己的專用事件,當串口接收到數據,會調用serialEvent()事件函數.

二、典例

  2.1、串口接收字符串數據

 1 /********************************************
 2  *  歡迎關注我的博客及某寶店鋪——深藍創客  3 ********************************************/
 4 
 5 String read_val = "";       //用於儲存串口接收的字符串
 6 
 7 void setup()
 8 {
 9   Serial.begin(115200);     
10 }
11 
12 void loop()
13 {}
14 
15 /* 串口接收數據處理事件 */
16 void serialEvent()
17 {
18   while(Serial.available())
19   {
20     read_val=read_val+char(Serial.read());      //接收串口數據
21     delayMicroseconds(100);                     //延時100us
22   }
23   if(read_val.length()>0)
24   {
25     Serial.println(read_val);     //打印數據
26     read_val="";                  //清空接收的數據
27   }
28 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM