I launched a simple sequential program on Arduino Uno , which simply echoes what you type. This works great 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 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 does not work. I never see my text coming back to me. I tried to print the value of Serial.available()
in a while(1)
and is always zero. Whenever I type on the keyboard, I see that the RX indicator lights up, but nothing happens after that. I can change my code to successfully call Serial.println()
if it is outside the Serial.available()
condition.
I confirmed that my baud rate in my serial software is also set to 115200. And yes, my serial software points to the right serial port.
What am I missing?
source share