Difference in behavior of Serial.available () function between Mega and Yun

Hi,

So while working on a project I noticed a difference in behavior for YUN and MEGA when Serial.available() was executed inside a loop .

So , I'm running the following program on the micro controllers:

void loop()
{
    char d_data[10]={};

    
    if (Serial.available() > 0)
    {

            digitalWrite(10, HIGH);
            Serial.readBytes(d_data,10);
            Serial.flush();

    }
}

Where I have a LED connected to pin 10 that indicates when the instructions inside the if are being implemented.

On the other side, I'm using Matlab so send data to the micro-controllers continously. Matlab code:

s2 = serial('COM18','DataBits',8,'Parity','none','StopBits',1);
set(s2,'BaudRate',115200);
set(s2,'Timeout',0.005);
fopen(s2);

a=[0,1,0,1,0,1,10,25,32,56];
b= char(a);


for i=1:50 
    
    pause(0.001);
   
    fwrite(s2,b,'uchar');
    fprintf(s2,'\n');
    
    pause(0.019);


end

fclose(s2);
delete(instrfindall);
clear all

Now, the YUN behaves exactly like expected i.e. as long as data is being transmitted the LED on pin 10 keeps on blinking rapidly.

However, when I run the same code on MEGA, the LED blinks only once (a very very short blink) ,in the beginning ,even though the board is still receiving data. This indicates that when using MEGA the program goes inside the 'if' only once.

Can somebody explain why does this difference of behavior exist? and why MEGA behaves in an unexpected way?

P.S. I know that YUN uses a software serial but I think this issue has nothing to do with that . Any function call should behave similarly unless explicitly mentioned by the developer

Please use code tags in future.
** **[code]** **
your code here
** **[/code]** **
will result in

your code here
void loop()
{
    char d_data[10]={};
 
    if (Serial.available() > 0)
    {
            digitalWrite(10, HIGH);
            Serial.readBytes(d_data,10);
            Serial.flush();
    }
}

I only have experience with an Uno so take this for what it is. The above code will switch the LED on (or off depending on how it is wired) once and never will change it's state again. There is no code to change the LED to the opposite state. Also note that on the Uno (and Mega) the on-board LED is connected to pin 13.

What do you want to do with Serial.flush() ?
http://www.arduino.cc/en/Serial/Flush

As soon as 1 byte is available, you read 10 bytes. That means you could have 9 wrong bytes.

The Serial.flush() is not the same for the Arduino Uno and the Arduino Leonardo. There was recently a topic on this forum about that, and the Serial.flush() for the SerialUSB port is not implemented, or was wrong implemented.

Reading characters is also not the same. For an Arduino Uno, the data is being received with a fixed speed (the baudrate). For the Arduino Leonardo, the data is received in bursts at a very high rate (the baudrate is ignored, the USB speed is used).

For the Arduino Mega, it is the same as the Arduino Uno, they very much alike.

The Arduino Yun is actually a Arduino Leonardo with an added linux/wifi module.

Get rid of the Serial.flush(), and use a number of Serial.read() to remove the remaining received characters if you have to. But only read if something is available.
If you need 10 bytes, be sure that all 10 bytes are available.

Have a look at the examples in Serial Input Basics. I have never noticed a difference using them on my Leonardo or Uno or Mega or Pro-Micro. And I think I have used this technique on my Yun without problems.

...R

Robin2, you don't use Serial.flush() and you don't read characters without testing if they are available. If the Serial functions are used correct, it the same for the Uno, Mega and Leonardo and Yun.

Hi,

Thanks Robin2 and Koepel. Your guys suggestions helped me a lot in debugging.

So it seems like the data transfers on Mega works fine when i used serial monitor or Tera-term. But as soon as I would start sending data from Matlab ,the mega would crash during the first transfer and reset as soon as the data transfer stopped (Hence , the behavoir of LED i mentioned above). I tried running the code on the page that Robin2 suggested and it behaved similarly. When I was doing this experiment I had nothing else connected to board. I had 2 other mega based boards and they were behaving similarly.

So I tried sending data to serial1 using a (USB to UART) FTDI cable and then the data was received as expected . I tried that with multiple serial.read() commands and also with one serial.ReadBytes() and both worked as expected.Plus, this freed the other serial for debugging purposes :slight_smile: .

Thanks again guys for all your help.

If anyone knows the reason why transferring data through Matlab on default serial causes arduino to crash let us know?

A possible answer is that Matlab is not accepting the data fast enough - but I don't know Matlab so there is no point showing me the Matlab code.

...R

Hmmm.... That could be a possible reason... But don't you think that if Matlab is slow the same thing would occur with the yun?

PapaMidnite:
Hmmm… That could be a possible reason… But don’t you think that if Matlab is slow the same thing would occur with the yun?

The Arduino side of a Yun is like a Leonardo and its serial comms is entirely different from a Mega or Uno. That may (or may not) explain the different behaviour.

…R