我在我的Arduino Uno上运行了简单的串行程序,它只是回显你输入的内容.在Arduino Sketch IDE(v22)中运行时,这非常有效.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
(代码来自http://arduino.cc/en/Serial/Available)
但是,我不想使用Arduino IDE而宁愿使用avr-g ++编译我的C++代码,所以我写了这个,它应该与上面完全相同:
extern "C" {
#include <avr/io.h>
}
#include <HardwareSerial.h>
extern …Run Code Online (Sandbox Code Playgroud)