2

When I flash this simple code (using Arduino-Makefile):

#include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
void init_io(void) {
 // open serial port
 Serial.begin(9600);
 while (!Serial);
 Serial.println("Comm-link online");
}//END: init_io
int main(void) {
 init_io();
 while (1) {
 // send data only when you receive data:
 if (Serial.available() > 0) {
 // read the incoming byte:
 int incomingByte = Serial.read();
 // say what you got:
 Serial.print("I received: ");
 Serial.println(incomingByte, DEC);
 } else {
 // Serial.println("No serial ");
 }
 }//END: loop
 return 0;
}//END: main

I only see first two chars printed in the serial output:

~$> Co

The input also does not work as intended - tested on two independent microcontrolers, Mega and Uno. I'm not sure what's the cause of this behaviour. Here's the Makefile:

BOARD_TAG = uno # megaADK uno
MONITOR_PORT = /dev/ttyACM0
ARDUINO_LIBS = 
include $(ARDMK_DIR)/Arduino.mk

It's definitely not a hardware problem, I've used serial i/o before in other projects.

asked Mar 19, 2015 at 18:56
2
  • 1
    What program are you using to see the serial responses? How do you have the port configured on your end? Commented Mar 19, 2015 at 19:12
  • Usually screen, I tried also on Arduino IDEs built in Serial Monitor. Commented Mar 19, 2015 at 19:55

1 Answer 1

2

When using the Arduino core library, you normally do not write a main() function. Instead, you write setup() and loop() and rely on libcore's provided main() for calling them.

If you nevertheless want to provide your own main(), then you should call init(); (with no arguments) before you try to use the library. Adding this single call makes your program work on my Uno.

However, since it does not seem to be officially documented, this trick may not be safe against upgrades in the core library. Writing setup() and loop() should be more future-proof.

answered Mar 19, 2015 at 20:36
3
  • This solves the problem. An alternative is to use AVR libc implementations printf(), puts() and getchar(). Commented Mar 20, 2015 at 21:58
  • Yes, but if you go along this path, you will have to initialize the UART yourself, and provide the put and get functions, and the ISRs. And since your program will not depend on libcore, you will probably use a Makefile simpler than Arduino.mk. Commented Mar 21, 2015 at 9:22
  • 1
    Yes,off course - I'm simply stating it here as another possibility, for future reference. Commented Mar 22, 2015 at 10:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.