No data sent over serial port in linux c++ TL;DR - I am attempting serial communication with Arduino with code that I found here and nothing gets sent over (Arduino programmed to respond, and I checked that it does with its serial monitor)

Hi there,
I was looking for a way to send information over to an Arduino Mega (2560) unit over linux serial port by C++.

I came across the following solution: Solution
I'm using this guy's code for write (I'm able to read data from the arduino) and use the same parameters (they work, as I'm able to receive data from the Ardunio). 
I programmed my Arduino to send "Hi" over serial whenever it sees at least 1 bit of information, and checked it worked through the Arduino IDE Serial Monitor.

Yet when running the C++ code, the arduino doesn't respond. Do anyone might have idea why?

Full disclosure - I inserted @Lunatic999's code to a class so I can make an instance of it for my needs of the code.

fd = open(portNameC, O_RDWR | O_NOCTTY | O_SYNC); //open port ("opens file")


Serial Parameters:

struct termios tty;
struct termios tty_old;
memset (&amp;tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( fd, &amp;tty ) != 0 ) {
   std::cout &lt;&lt; "Error " &lt;&lt; errno &lt;&lt; " from tcgetattr: " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
}

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&amp;tty, (speed_t)B9600);
cfsetispeed (&amp;tty, (speed_t)B9600);

/* Setting other Port Stuff */
tty.c_cflag     &amp;=  ~PARENB;            // Make 8n1
tty.c_cflag     &amp;=  ~CSTOPB;
tty.c_cflag     &amp;=  ~CSIZE;
tty.c_cflag     |=  CS8;

tty.c_cflag     &amp;=  ~CRTSCTS;           // no flow control
tty.c_cc[VMIN]   =  1;                  // read doesn't block
tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ &amp; ignore ctrl lines

/* Make raw */
cfmakeraw(&amp;tty);

/* Flush Port, then applies attributes */
tcflush( fd, TCIFLUSH );
if ( tcsetattr ( fd, TCSANOW, &amp;tty ) != 0) {
   std::cout &lt;&lt; "Error " &lt;&lt; errno &lt;&lt; " from tcsetattr" &lt;&lt; std::endl;
}


Write (this code I put inside a function which I call)

unsigned char cmd[] = "INIT \r";
int n_written = 0,
    spot = 0;

do {
    n_written = write( fd, &amp;cmd[spot], 1 );
    spot += n_written;
} while (cmd[spot-1] != '\r' &amp;&amp; n_written &gt; 0);


Arduino code:

bool dataRecieved = false;
int ledpin = 13;

void setup() {
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(19200);
}

void loop() {  
  while(!dataRecieved)
  {
   digitalWrite(ledpin,HIGH);
   if (Serial.available() &gt; 0) 
   {
     dataRecieved = true;
   }
  }
  digitalWrite(ledpin,LOW);
  delay(1000);
  digitalWrite(ledpin,HIGH);
  delay(1000);
  Serial.println("hi");
}
The Arduino resets the moment you open the serial port.  Once reset the bootloader executes, which consumes (and discards since it's invalid) any serial data for a second or so before your sketch runs.

Because of this anything you send immediately after opening the serial port is ignored.

There are two commonly accepted "fixes" for this:


Delay for a pre-defined period after opening the serial port, and
Have the Arduino send a "READY" signal before doing anything else which your program waits for before sending.