Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Time elapsed using serial fwrite and fread functions
Date: Tue, 11 Nov 2008 20:10:58 -0500
Organization: The MathWorks, Inc.
Lines: 73
Message-ID: <gfdaf2$el3$1@fred.mathworks.com>
References: <gfcvsa$dpv$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: jarvit.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1226452258 15011 172.31.56.68 (12 Nov 2008 01:10:58 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 12 Nov 2008 01:10:58 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:500325



"Benjamin " <benjaminst@gmail.com> wrote in message 
news:gfcvsa$dpv$1@fred.mathworks.com...
> Hi all!
>
> Currently I am working in my master thesis. It consists on implementing an 
> arbitrary waveform generator using Matlab and a FPGA. So, I need to 
> transfer signal samples (100Kbytes) from Matlab to a Altera Nios 2 
> processor(85MHz) using a serial protocol (UART 115Kbps). I have done the 
> following code which I have tested and it works ok:
>
> ....
> tic
> for i=1:length(data_to_send) %length(data_to_send)=100e3
>    tic
>    fwrite(s,data_to_send(i),'uchar');
>    time_write(i)=toc;
>    tic
>    if(fread(s,1,'uchar')~=0)
>        Error('Unexpected error while reading UART');
>    end
>    time_read(i)=toc;
> end
> time_elapsed=toc;
> fprintf (1,'Elapsed CPU time (min) = %f\n',time_elapsed/60);
> ...
>
> The main problem is that it is too slow (the elapsed cpu time is more than 
> 30minutes!). I can't understand it because the expected time a grosso modo 
> would be 100Kbytes/115.2Kbits/s=6,94seconds.
>
> I'm sure that the problem is not from Nios 2 processor because the main 
> function is just a infinite loop that reads from the uart and send 0 
> character.
>
> Does anybody know if there is a manner to increase the perfomance of the 
> serial transmision? Thanks in advance,
>

Hi Ben,

There is considerable overhead in the parameter checking for each function 
call made.  You may want to reduce the number of calls if possible to 
improve the throughput.  As an example, I performed the following on a 
loopback connection to see how close MATLAB Serial was to the 6.94 seconds 
(write) you mention:

chunk=5120;
data_to_send=mod(1:102401,255);
s=serial('/dev/ttyS0');
s.outputbuffersize=chunk;
s.inputbuffersize=chunk;
s.baudrate=115200;
fopen(s);
tic;
a=[];
for i=0:length(data_to_send)/chunk-1
    fwrite(s,data_to_send((1:chunk)+chunk*i),'uchar');
    a = [a fread(s,5120,'uchar')'];
end
toc;
fclose(s);
delete(s);
clear('s');

The result on R2008b was reasonable for a quick test:

Elapsed time is 9.361430 seconds.

Thats actually less time than one would expect for both read and write if 
they did not overlap underneath.