Go Down

Topic: Serial speed (Read 127 times) previous topic - next topic

harlock974

Hi,

I did this simple sketch to time serial communication with an Arduino Uno :

Code: [Select]

#define SPEED 57600

void setup()
{
unsigned long d,t0;
Serial.begin(SPEED);
t0=micros();
Serial.println("AZERTYUIOP");
d=micros()-t0;
Serial.print(d);
Serial.println(" ms");
}

void loop()
{}


The result is 84 microseconds, regardless of the speed. I tried 9600, 57600 and 115200 bps and I always have the same speed.
How to explain that ?

sherzaad

Hi,

I did this simple sketch to time serial communication with an Arduino Uno :

Code: [Select]

#define SPEED 57600

void setup()
{
unsigned long d,t0;
Serial.begin(SPEED);
t0=micros();
Serial.println("AZERTYUIOP");
d=micros()-t0;
Serial.print(d);
Serial.println(" ms");
}

void loop()
{}


The result is 84 microseconds, regardless of the speed. I tried 9600, 57600 and 115200 bps and I always have the same speed.
How to explain that ?
IMHO, the above code would give you the program excecution time NOT the communication speed... for the latter you would need to use a scope if you want to confirm the data transfer rate.

hope that helps....

harlock974

Thanks.
I did this test because I wanted to improve the execution speed of a program with many serial.print.
But it seems that the duration of this function is independant of communication speed...
I did another test where I sent various data types (string, integer, double), and execution time slightly increase with com speed !

Robin2

#3
Jun 30, 2020, 09:49 am Last Edit: Jun 30, 2020, 09:51 am by Robin2
What you are measuring is the time needed to move the characters into the Serial Output Buffer.

Try adding Serial.flush() to force the program to wait until the data is actually sent from the Buffer.
Code: [Select]
t0=micros();
Serial.println("AZERTYUIOP");
Serial.flush();
d=micros()-t0;


I have done measurements of transmission speed and it seems to be as expected - for example at 57600 baud it will send about 5760 bytes per second and at 500,000 baud it will send about 50,000 bytes per second.

Try measuring the time to send (say) 100 messages of 64 chars each. Just use Serial.flush() on the last message.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

harlock974

#4
Jun 30, 2020, 01:04 pm Last Edit: Jun 30, 2020, 01:06 pm by harlock974
Thanks Robin,
Your answer makes me understand, at last, what Serial.flush() does.
From Arduino reference, "Serial.flush() waits for the transmission of outgoing serial data to complete", which was not clear to me.
In fact, Serial.flush() suspend program execution until the transmission of outgoing serial data is completed.
I wasn't aware that serial transmission occurs in background, so after a call to Serial.print, the program keeps running even if the transmission is not completed.
So here are the results with your code :

Without Serial.flush : 84 µs for any com speed ; after that time, data sent is still in the pipe !

With Serial.flush :

  9600 bps : 11464 µs
 19200 bps :  5736 µs
 57600 bps :  1884 µs
115200 bps :   948 µs


So if you are only looking for a faster program, it is not necessary to use an high transmission speed.


Robin2

So if you are only looking for a faster program, it is not necessary to use an high transmission speed.
It's not as simple as that.

If you send less than 64 bytes (and don't use Serial.flush() ) then the sending program only needs the time to move the data into the buffer.

If your concern is the length of time for the data to get from one Arduino to another then, of course, the baud rate is the critical factor.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up