为什么我的 Arduino 上没有可用的串行数据?

发布于 2024-12-07 03:42:08 字数 1667 浏览 1 评论 0原文

我已经在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱你是孤单的心事 2024-12-14 03:42:08

Arduino 的原始粘合代码如下所示:

#include <WProgram.h>
int main(void)
{
    init();
    setup();
    for (;;)
        loop();
    return 0;
}

您的代码中缺少 init() 内容。 init() 定义在 $ARDUINO_HOME/hardware/arduino/cores/arduino/wiring.c 中,您可以直接链接它,也可以复制 的代码>init() 到您的代码中。

Arduino's original glue code looks like this:

#include <WProgram.h>
int main(void)
{
    init();
    setup();
    for (;;)
        loop();
    return 0;
}

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 of init() into your code.

慢慢从新开始 2024-12-14 03:42:08

您可能没有正确初始化芯片上的 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.

ら栖息 2024-12-14 03:42:08

找到了我自己问题的答案:

事实证明 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 call sei(); to turn on interrupts before you try to use the Serial Library.

cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文