6

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Finer Recliner
  • 1,579
  • 1
  • 13
  • 21

3 Answers3

3

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.

A.H.
  • 63,967
  • 15
  • 92
  • 126
2

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.

Chriszuma
  • 4,464
  • 22
  • 19
2

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!

Finer Recliner
  • 1,579
  • 1
  • 13
  • 21