为什么我的 Arduino 上没有可用的串行数据?
我已经在我的 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);
}
}
(代码取自http://arduino.cc/en/Serial/Available)
但是,我我不想使用 Arduino IDE,而是使用 avr-g++ 编译我的 C++ 代码,所以我写了这个,它的功能应该与上面完全一样:
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.println()
,只要它位于 Serial.available()
条件之外。
我已经确认我的串行软件中的波特率也设置为115200。是的,我的串行软件指向正确的串行端口。
我缺少什么?
I've run the simple serial program on my Arduino Uno, which just echos whatever you type to it. This works perfectly when run in the 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);
}
}
(Code taken from http://arduino.cc/en/Serial/Available)
However, I prefer not to use the Arduino IDE and would rather compile my C++ code with avr-g++, so I wrote this, which should function exactly the same as above:
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;
}
I compile and run it, but it doesn't work. I never see my text echoed back to me. I tried printing out the value of Serial.available()
in the while(1)
loop, and it's always zero. Whenever I type on the keyboard, I see the RX LED light up, but nothing happens after that. I can edit my code to successfully call Serial.println()
as long as it's outside the Serial.available()
conditional.
I've confirmed that my baud rate in my serial software is also set to 115200. And yes, my serial software is pointing to the right serial port.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Arduino 的原始粘合代码如下所示:
您的代码中缺少
init()
内容。init()
定义在$ARDUINO_HOME/hardware/arduino/cores/arduino/wiring.c
中,您可以直接链接它,也可以复制的代码>init()
到您的代码中。Arduino's original glue code looks like this:
The
init()
stuff is missing in your code.init()
is defined in$ARDUINO_HOME/hardware/arduino/cores/arduino/wiring.c
, you can either link against it directly or just copy the code ofinit()
into your code.您可能没有正确初始化芯片上的 UART 端口。对于微控制器来说,这必须手动完成,Arduino IDE 可能已经为您完成了。检查您的芯片的 AVR 数据表,特别是串行端口部分。
You probably have not properly initialized the UART port on the chip. This has to be done manually for microcontrollers, and the Arduino IDE was probably doing it for you. Check the AVR datasheet for your chip, specifically the serial port section.
找到了我自己问题的答案:
事实证明 HardwareSerial.h 库依赖于中断。当使用 Arduino IDE 进行构建时,系统会自动为您处理这些事情。如果您不使用 Arduino IDE(像我一样),那么您必须记住自行启用中断。
只需
#include
,并在尝试使用串行库之前调用sei();
打开中断。干杯!
Found the answer to my own question:
It turns out the HardwareSerial.h library relies on interrupts. This is something that is automagically taken care of for you when building with the Arduino IDE. If you aren't using the Arduino IDE (like me), then you must remember to enable interrupts on your own.
Just
#include <avr/interrupt.h>
, and callsei();
to turn on interrupts before you try to use the Serial Library.cheers!