Slow serial communication with Matlab

Hi,

Current, I'm trying to collect data from a GPS and an Arduino Uno using Matlab on my PC (windows 7).

When I checked the time used to read data (using fscanf) from both GPS and Arduino Uno (the baud rates were both set to 19200), it took Matlab 0.15 s to read one line (84 chars) and 1.6 s to read one line from Uno (8 chars).

Here's the codes for Matlab:

%% GPS
comPort1 = 'COM10';
s1 = serial(comPort1);
set(s1,'DataBits', 8);
set(s1,'StopBits', 1);
set(s1,'BaudRate', 19200);
set(s1, 'Parity','none');
fopen(s1)
t = tic;
fscanf(s1)
toc(t)
%% Arduino Uno
comPort2 = 'COM8';
s2 = serial(comPort2);
set(s2,'DataBits', 8);
set(s2,'StopBits', 1);
set(s2,'BaudRate', 19200);
set(s2, 'Parity','none');
fopen(s2)
t = tic;
fscanf(s2)
toc(t)
fclose(s1)
delete(s1)
clear s1
fclose(s2)
delete(s2)
clear s2

Here's code for Uno:

int a = 0;
int b = 1;
void setup() {
  Serial.begin(19200);
}

void loop() {
 Serial.print(a);
 Serial.print(' ');
 Serial.println(b);
 a++;
 b++;
 delay(10);
}

Here's the result from Matlab:

ans =

$GPGGA,000554.30,4615.14626332,N,11944.40191032,W,0,3,3.2,244.649,M,-17.580,M,,*63


Elapsed time is 0.157869 seconds.

ans =

0 1


Elapsed time is 1.623741 seconds.

Does it means that it took 1.6 s to read a line from Uno? Why is it much slower to read data from Uno comparing to GPS even if it has less data to read? Actually I tried to use Mega instead of Uno with the same code, the time used to read data reduced to 0.9 s. But it is still quite slow though.

If possible, I would like to read data through serial port from Uno at a frequency of 10 Hz using Matlab, is this possible?

Thanks in advance.

You can try use Serial.flush() to make sure Arduino send out the outgoing data.

void loop() {
 Serial.print(a);
 Serial.print(' ');
 Serial.println(b);
 Serial.flush();// Waits for the transmission of outgoing serial data to complete.
 a++;
 b++;
 delay(10);
}

Sorry for the late reply. I just added Serial.flush() in my code. But it seems that it has no effect. It still took 1.6 s to read a line sent from Uno.

Thank you for your reply though.

When you open the Arduino's serial port, the Arduino resets, and it takes a second or so to start.

That sounds reasonable. Thank you very much for your help.

I will try to continuously read data from Arduino and check whether fscanf is fast enough for me to pick up data sent at 10 Hz or not. And I will post my result here tomorrow.