我在我的Arduino Uno上運行了簡單的串行程序,它只是迴應你輸入的任何內容。在Arduino Sketch IDE(v22)中運行時,此功能非常完美。爲什麼我的Arduino上沒有可用的串行數據?
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(115200); // opens serial port, sets data rate
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
(從http://arduino.cc/en/Serial/Available採取代號)
不過,我不喜歡使用Arduino的IDE,寧願編譯我的C++與AVR-G ++代碼,所以我寫了這個,它應有的功能正是與上面相同:
extern "C" {
#include <avr/io.h>
}
#include <HardwareSerial.h>
extern "C" void __cxa_pure_virtual() { while(1); }
int main (void)
{
int incomingByte = 0;
Serial.begin(115200);
while (1) {
if (Serial.available() > 0) {
incomingByte = Serial.read();
//print as an ASCII character
Serial.print("received: ");
Serial.println(incomingByte, DEC);
}
}
return 1;
}
我編譯並運行它,但它不起作用。我從來沒有看到我的文字回饋給我。我嘗試在while(1)
循環中輸出Serial.available()
的值,並且它始終爲零。每當我鍵入鍵盤時,我都會看到RX LED亮起,但之後沒有任何反應。只要條件在Serial.available()
以外,我就可以編輯我的代碼以成功呼叫Serial.println()
。
我已確認我的串行軟件中的我的baud rate也設置爲115200.是的,我的串行軟件指向正確的串行端口。
我錯過了什麼?