user28063: After reading the following 2 articles:
https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/
https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/
I have get rid of the complete String class from my main program and went back to using character arrays.
This seems to work reliably until some extent but once I start stress testing it, quickly turns out that it is not reliable at all!
Here is the complete code, you can load it into the mega and it will produce the same results (I have removed all other unnecessary parts from the code):
#define DEBUG

char Command_PC[64], Command_Xbee[64], Command[64];
int count=0;
bool Complete_PC=false;
bool Complete_Xbee=false;

void setup() 
{
  Serial.begin(9600);
  Serial1.begin(9600);

} // END void setup


void comm() {

/* COMM FROM PC */    
if (Serial.available()){

  #ifdef DEBUG
    Serial.println("PC: New command, collecting...");
  #endif
    count = 0;
    memset(Command_PC, 0, sizeof(Command_PC));
    while (Serial.available()){
      char character = Serial.read();
      if (character=='\n')
         {
          Complete_PC =true;
          break;
         }     
      Command_PC[count] = character;
      count++;
    }
  }

/* COMM FROM Xbee */  
if (Serial1.available()){
  #ifdef DEBUG
    Serial.println("Xbee: New command, collecting...");
  #endif
    count = 0;
    memset(Command_Xbee, 0, sizeof(Command_Xbee));
    while (Serial1.available()){
      char character = Serial1.read();
      if (character=='\n')
         {
          Complete_Xbee =true;
          break;
         }     
      Command_Xbee[count] = character;
      count++;
    }
  }

   delay(500);

 if (Complete_Xbee || Complete_PC) {

  if(Complete_Xbee)
    strcpy(Command, Command_Xbee);
  else
    strcpy(Command, Command_PC);

  #ifdef DEBUG
    Serial.print("Command received: >");
    Serial.print(Command);
    Serial.println("<");
  #endif      
           Complete_Xbee=false;
           Complete_PC=false;
 }

}

void loop()
{    
    comm();

} // END void loop

The code is simple, all it is intended to do that whether there is a command coming from the PC (USB) or Xbee (attached to hardware uart) process it and do further things. As long as you typing in commands manually it goes reliably for a while but even then after 10-15 commands it might screws up. The automated testing with:
for ((;;)); do echo HELLOWORLD > /dev/ttyACM0; done
Command received: >HELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORL<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORLDELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOORLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWOD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWOLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWOLD<
ERR_INVALIDCMD
PC: New command, collecting...
Command received: >HELLOWLD<
ERR_INVALIDCMD
PC: New command, collecting...

Brings out the bug right away. Is there any problem with this code? Any ideas how to make this 100% reliable even if it takes slowing the main program down?
I have noticed that by adding: delay(500); it makes it more stable. If this is not there it will even fail on the first manually entered command like:
PC: New command, collecting...
PC: New command, collecting...
PC: New command, collecting...
Command received: >lloworld<

By adding a delay to my test script:
for ((;;)); do echo HELLOWORLD > /dev/ttyACM0 && sleep 1; done
It runs stable for a while but after 15 mins testing it start misbehaving again:
Command received: >HELLOWORLD<
PC: New command, collecting...
Command received: >HELLOWORLD<
PC: New command, collecting...
PC: New command, collecting...
Command received: ><
PC: New command, collecting...
PC: New command, collecting...
Command received: ><
PC: New command, collecting...
PC: New command, collecting...
Command received: ><
PC: New command, collecting...
PC: New command, collecting...
Command received: ><


user4143: As @Edesign has mentioned your reception methodology is slightly flawed.  A better arrangement would be:
static int Count_PC = 0;
static bool Complete_PC = false;

if (Serial.available()) {
  char character = Serial.read();
  if (character=='\n') {
    Complete_PC =true;
    Count_PC = 0;
  } else {
    if (Count_PC < 62) { // Don't overflow the array...!
      Command_PC[Count_PC++] = character;
      Command_PC[Count_PC] = 0;
    }
  }
}

The count and complete flags are now static - that is, they retain their values between iterations of the loop. You only look at one character per iteration of the loop (you could enhance it by changing the first if to a while if you like, but this way plays better with handling multiple data sources), and add that to the string. Only when the \n is received do you flag it as complete. There is no clearing of the whole array, it's not needed - just ensure that you always add a \0 to the end of the string each time you add a character.
The current character position (Count_PC) is reset when you receive the \n so that it is ready to receive the next command - be sure that you process the command that same loop (which you do anyway).

user28063: @Edesign, @Majenko: thank you for your help, it turns out that the hardware DOES matter as well. It was partially a hardware issue. Regarding my initial code, you can get that code remove the delay(500); from it and load it into an Arduino Leonardo and it will pass my first initial stress test without any delays and it works stable even after an hour testing.
I believe that both the Mega and the Leonardo have USB2 connections:
Bus 001 Device 003: ID 2341:0042 Arduino SA Mega 2560 R3 (CDC ACM)
Bus 003 Device 013: ID 2341:8036 Arduino SA 
Although this is was a Keyestudio MEGA, a cheap chinese counterfeit which would explain why did it have this issue. My initial code was not wrong but as you have pointed out it could have been improved, what I did.
Code1:
static int Count_PC = 0;
static bool Complete_PC = false;

if (Serial.available()) {
  char character = Serial.read();
  if (character=='\n') {
    Complete_PC =true;
    Count_PC = 0;
  } else {
    if (Count_PC < 62) { // Don't overflow the array...!
      Command_PC[Count_PC++] = character;
      Command_PC[Count_PC] = 0;
    }
  }
}

Code2:
/* COMM FROM PC */   
if (Serial.available()){

  #ifdef DEBUG
    Serial.println("PC: New command, collecting...");
  #endif
    memset(Command_PC, 0, sizeof(Command_PC));
    while (Serial.available()){
      char character = Serial.read();
      if (character=='\n')
         {
          Complete_PC =true;
          Count_PC = 0;
          //break;
         } else {
             if (Count_PC < 62) { // Don't overflow the array...!
                 Command_PC[Count_PC++] = character;
                 Command_PC[Count_PC] = 0;
             }
         }
  }
}   

The difference between his code and my last code is that I do the while looping inside the function while his relies on the main loop cycles to complete the string.
Both are working but if the main program have a lot to do, his version will noticeably make the responses more sluggish. Which can be easily tested with:
 for ((;;)); do date > /dev/ttyACM0; done


user-1: To me, your problem is because the program is reading the characters faster than they are sent by the computer. Then, some situations occur where all the characters of the buffer have been read (Serial.available() = 0) but the '\n' character has not been sent by the computer yet. The program will then exit the loop without setting the Complete_PC variable to true.
In that case, the program will write several times "PC: New command, collecting..." without writing any result (Complete_PC not set to true). It can also drop the first letters, as they were read during the previous loop and the Command_PC array and count were reset in between.
The delay(500) command improve the stability because it increase the chances that the whole line is received when the program is not reading it at the same time.
Try something like this:
while ((!Complete_PC)&&(count<60))
{
    int character = Serial.read();
    if (character=='\n')
    {
        Complete_PC =true;
    }
    else if (character!=-1)
    {
        Command_PC[count] = (char) character;
        count++;
    }
}


28063: <p>After reading the following 2 articles:</p>

<p><a href="https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/" rel="nofollow noreferrer">https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/</a></p>

<p><a href="https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/" rel="nofollow noreferrer">https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/</a></p>

<p>I have get rid of the complete String class from my main program and went back to using character arrays.
This seems to work reliably until some extent but once I start stress testing it, quickly turns out that it is not reliable at all!</p>

<p>Here is the complete code, you can load it into the mega and it will produce the same results (I have removed all other unnecessary parts from the code):</p>

<pre><code>#define DEBUG

char Command_PC[64], Command_Xbee[64], Command[64];
int count=0;
bool Complete_PC=false;
bool Complete_Xbee=false;

void setup() 
{
  Serial.begin(9600);
  Serial1.begin(9600);

} // END void setup


void comm() {

/* COMM FROM PC */    
if (Serial.available()){

  #ifdef DEBUG
    Serial.println("PC: New command, collecting...");
  #endif
    count = 0;
    memset(Command_PC, 0, sizeof(Command_PC));
    while (Serial.available()){
      char character = Serial.read();
      if (character=='\n')
         {
          Complete_PC =true;
          break;
         }     
      Command_PC[count] = character;
      count++;
    }
  }

/* COMM FROM Xbee */  
if (Serial1.available()){
  #ifdef DEBUG
    Serial.println("Xbee: New command, collecting...");
  #endif
    count = 0;
    memset(Command_Xbee, 0, sizeof(Command_Xbee));
    while (Serial1.available()){
      char character = Serial1.read();
      if (character=='\n')
         {
          Complete_Xbee =true;
          break;
         }     
      Command_Xbee[count] = character;
      count++;
    }
  }

   delay(500);

 if (Complete_Xbee || Complete_PC) {

  if(Complete_Xbee)
    strcpy(Command, Command_Xbee);
  else
    strcpy(Command, Command_PC);

  #ifdef DEBUG
    Serial.print("Command received: &gt;");
    Serial.print(Command);
    Serial.println("&lt;");
  #endif      
           Complete_Xbee=false;
           Complete_PC=false;
 }

}

void loop()
{    
    comm();

} // END void loop
</code></pre>

<p>The code is simple, all it is intended to do that whether there is a command coming from the PC (USB) or Xbee (attached to hardware uart) process it and do further things. As long as you typing in commands manually it goes reliably for a while but even then after 10-15 commands it might screws up. The automated testing with:</p>

<p><strong><code>for ((;;)); do echo HELLOWORLD &gt; /dev/ttyACM0; done</code></strong></p>

<pre><code>Command received: &gt;HELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORL&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORLDELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOORLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWOD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWOLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWOLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
Command received: &gt;HELLOWLD&lt;
ERR_INVALIDCMD
PC: New command, collecting...
</code></pre>

<p>Brings out the bug right away. Is there any problem with this code? Any ideas how to make this 100% reliable even if it takes slowing the main program down?</p>

<p>I have noticed that by adding: <code>delay(500);</code> it makes it more stable. If this is not there it will even fail on the first manually entered command like:</p>

<pre><code>PC: New command, collecting...
PC: New command, collecting...
PC: New command, collecting...
Command received: &gt;lloworld&lt;
</code></pre>

<p>By adding a delay to my test script:</p>

<p><strong><code>for ((;;)); do echo HELLOWORLD &gt; /dev/ttyACM0 &amp;&amp; sleep 1; done</code></strong></p>

<p>It runs stable for a while but after 15 mins testing it start misbehaving again:</p>

<pre><code>Command received: &gt;HELLOWORLD&lt;
PC: New command, collecting...
Command received: &gt;HELLOWORLD&lt;
PC: New command, collecting...
PC: New command, collecting...
Command received: &gt;&lt;
PC: New command, collecting...
PC: New command, collecting...
Command received: &gt;&lt;
PC: New command, collecting...
PC: New command, collecting...
Command received: &gt;&lt;
PC: New command, collecting...
PC: New command, collecting...
Command received: &gt;&lt;
</code></pre>
 4143: <p>As @Edesign has mentioned your reception methodology is slightly flawed.  A better arrangement would be:</p>

<pre><code>static int Count_PC = 0;
static bool Complete_PC = false;

if (Serial.available()) {
  char character = Serial.read();
  if (character=='\n') {
    Complete_PC =true;
    Count_PC = 0;
  } else {
    if (Count_PC &lt; 62) { // Don't overflow the array...!
      Command_PC[Count_PC++] = character;
      Command_PC[Count_PC] = 0;
    }
  }
}
</code></pre>

<p>The count and complete flags are now static - that is, they retain their values between iterations of the loop. You only look at one character per iteration of the loop (you could enhance it by changing the first <code>if</code> to a <code>while</code> if you like, but this way plays better with handling multiple data sources), and add that to the string. Only when the <code>\n</code> is received do you flag it as complete. There is no clearing of the whole array, it's not needed - just ensure that you always add a <code>\0</code> to the end of the string each time you add a character.</p>

<p>The current character position (<code>Count_PC</code>) is reset when you receive the <code>\n</code> so that it is ready to receive the next command - be sure that you process the command that same loop (which you do anyway).</p>
 28063: <p>@Edesign, @Majenko: thank you for your help, it turns out that the hardware DOES matter as well. It was partially a <strong>hardware issue</strong>. Regarding my initial code, you can get that code remove the delay(500); from it and load it into an Arduino Leonardo and it will pass my first initial stress test without any delays and it works stable even after an hour testing.</p>

<p>I believe that both the Mega and the Leonardo have USB2 connections:</p>

<p>Bus 001 Device 003: ID 2341:0042 Arduino SA Mega 2560 R3 (CDC ACM)
Bus 003 Device 013: ID 2341:8036 Arduino SA </p>

<p>Although this is was a <em>Keyestudio MEGA</em>, a cheap chinese counterfeit which would explain why did it have this issue. My initial code was not wrong but as you have pointed out it could have been improved, what I did.</p>

<p>Code1:</p>

<pre><code>static int Count_PC = 0;
static bool Complete_PC = false;

if (Serial.available()) {
  char character = Serial.read();
  if (character=='\n') {
    Complete_PC =true;
    Count_PC = 0;
  } else {
    if (Count_PC &lt; 62) { // Don't overflow the array...!
      Command_PC[Count_PC++] = character;
      Command_PC[Count_PC] = 0;
    }
  }
}
</code></pre>

<p>Code2:</p>

<pre><code>/* COMM FROM PC */   
if (Serial.available()){

  #ifdef DEBUG
    Serial.println("PC: New command, collecting...");
  #endif
    memset(Command_PC, 0, sizeof(Command_PC));
    while (Serial.available()){
      char character = Serial.read();
      if (character=='\n')
         {
          Complete_PC =true;
          Count_PC = 0;
          //break;
         } else {
             if (Count_PC &lt; 62) { // Don't overflow the array...!
                 Command_PC[Count_PC++] = character;
                 Command_PC[Count_PC] = 0;
             }
         }
  }
}   
</code></pre>

<p>The difference between his code and my last code is that I do the while looping inside the function while his relies on the main loop cycles to complete the string.</p>

<p>Both are working but if the main program have a lot to do, his version will noticeably make the responses more sluggish. Which can be easily tested with:</p>

<pre><code> for ((;;)); do date &gt; /dev/ttyACM0; done
</code></pre>
 -1: <p>To me, your problem is because the program is reading the characters faster than they are sent by the computer. Then, some situations occur where all the characters of the buffer have been read (<code>Serial.available() = 0</code>) but the <code>'\n'</code> character has not been sent by the computer yet. The program will then exit the loop without setting the <code>Complete_PC</code> variable to true.</p>

<p>In that case, the program will write several times "PC: New command, collecting..." without writing any result (Complete_PC not set to true). It can also drop the first letters, as they were read during the previous loop and the <code>Command_PC</code> array and count were reset in between.</p>

<p>The <code>delay(500)</code> command improve the stability because it increase the chances that the whole line is received when the program is not reading it at the same time.</p>

<p>Try something like this:</p>

<pre><code>while ((!Complete_PC)&amp;&amp;(count&lt;60))
{
    int character = Serial.read();
    if (character=='\n')
    {
        Complete_PC =true;
    }
    else if (character!=-1)
    {
        Command_PC[count] = (char) character;
        count++;
    }
}
</code></pre>
