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?
To display text and numbers from your sketch on a PC or Mac via a serial link, put the Serial. begin(9600) statement in setup() , and then use Serial. print() statements to print the text and values you want to see. The Arduino Serial Monitor function can display serial data sent from Arduino.
The most common reason for the board not being displayed on a port are a failing USB connection. The board needs to be connected to your computer with a data USB cable. Make sure the USB cable is not damaged. Test your cable with a different device, or try using a different cable.
In order to view Serial output (basically see whatever is printed by the device using Serial. print() or its variants), you can use the Serial Monitor built into the Arduino IDE. In case you see multiple Serial Ports, it will be a good idea to disconnect your board and see which port disappears.
Verify the communication ports Arduino and the Arduino IDE should be connected to the same communications port. Unplugging and plugging back the Arduino can switch the ports. For a simple check, re-upload the sketch from the same IDE that you are using to open the Serial Monitor.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With