Using the serial port from C++ code.

I’m trying to use C++ code instead of Sketch on an Arduino Uno, reusing Arduino’s (C++) libraries, but I’m unable to read data from the serial port.

The following Sketch code works fine (it’s a simple echo program)

void blink()
{
    short state[2] = { LOW, HIGH };
    static int next = 0;
    
    digitalWrite(13,state[next]);
    next = 1-next;
}

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

int nextByte = 0;

void loop()
{
  delay(100);
  blink();
  while (Serial.available() > 0 )
  {
      nextByte = Serial.read();
      Serial.write(nextByte);
  }
}

The following code, which should be more or less the C++ equivalent, never enters the while loop (Serial.available() is never > 0 )
(The needed library file are copied to the project folder and compiled with the same flags as the main file)

#include <avr/io.h>
#include <util/delay.h>
#include "HardwareSerial.h"


int nextByte;


void blink()
{
    short state[2] = { 0x00, 0x20 };
    static int next = 0;
    
    PORTB = state[next];
    next = 1-next;
}

int main (void)
{
    Serial.begin(9600);
    Serial.flush();

    DDRB = 0xFF;

    while (1)
    {
        _delay_ms(100);
        blink();

        while (Serial.available() > 0 )
        {
            nextByte = Serial.read();
            Serial.write(nextByte);
        }
    }
    return 0;
}

The RX led blinks when I enter some text on the console, so it should not be a communication problem: I’m able to write data and see it on the host computer, it is only the read that doesn’t work.
Am I doing something wrong ? Or am I not supposed to reuse Arduino code this way ?
thanks.

What you're trying to do sounds pretty unreasonable and I'm surprised it works as well as it does. I guess that somewhere between the bootstrap and the setup()/loop() functions, there is some runtime library initialisation which your main() is not doing.

The IDE creates a main function that calls init(), setup(), and loop() in an endless loop.

Your main function is missing the stuff that init() does.

PaulS:
The IDE creates a main function that calls init(), setup(), and loop() in an endless loop.

Your main function is missing the stuff that init() does.

And, in addition, there might be other events being handled sequentially to loop(), as can be seen in Arduino 1.0’s now explicit main.cpp. I didn’t check if that’s true for Arduino 23 and earlier as well, but you get the idea:

// Arduino 1.0 rc2 main.cpp
#include <Arduino.h>

int main(void)
{
  init();

#if defined(USBCON)
  USB.attach();
#endif
	
  setup();
  for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
  }
  return 0;
}

I didn't check if that's true for Arduino 23 and earlier as well,

It isn't. Buffering of outgoing serial data (that's what that line of code after loop() is dealing with) is new for 1.0.

PaulS: The IDE creates a main function that calls init(), setup(), and loop() in an endless loop.

Your main function is missing the stuff that init() does.

Oh my, :blush: init() was exactly what I was missing, now it works fine :) Thanks a lot !

What you're trying to do sounds pretty unreasonable and I'm surprised it works as well as it does.

Is it really so unreasonable ? (beside missing initialization functions ? :D). While I prefer coding in C and/or C++ for a number of reasons, if could re-use some libraries (core or external), I'd be happy to. My understanding is that they are little more than C++ classes... or not ?