ARDUINO 串列埠回顯資料
阿新 • • 發佈:2019-01-30
/*串列埠接收資料最大長度*/ #define _SER_RECBUFSIZE 512 char serial_recive[_SER_RECBUFSIZE] =""; /*串列埠資料接收字串*/ /* 讀取串列埠輸入(單個位元組延遲2ms) */ int Serial_Read(char* result) { int single = 0; while(Serial.available() > 0) { single = Serial.read(); delay(2); if(single == '\n') { *(result++) = '\0'; Serial.flush(); return 0; } if(single != '\r') *(result++) = single; } } /* 清空串列埠接收緩衝區 */ void Serial_BuffClear() { for(int i = 0 ; i < _SER_RECBUFSIZE ;i++) { serial_recive[i] = 0x00 ; } } void setup() { Serial.begin(9600); } void loop() { while(Serial_Read(serial_recive) == 0) { if(strlen(serial_recive) > 0) { Serial.println(serial_recive); Serial_BuffClear(); } } }