Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Time elapsed using serial fwrite and fread functions

Subject: Time elapsed using serial fwrite and fread functions

From: Benjamin

Date: 11 Nov, 2008 22:10:18

Message: 1 of 3

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,

Ben

Subject: Time elapsed using serial fwrite and fread functions

From: Trent Jarvi

Date: 12 Nov, 2008 01:10:58

Message: 2 of 3


"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.

Subject: Time elapsed using serial fwrite and fread functions

From: Benjamin

Date: 12 Nov, 2008 08:58:03

Message: 3 of 3

"Trent Jarvi" <tjarvi@mathworks.com> wrote in message <gfdaf2$el3$1@fred.mathworks.com>...
>
> "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.
>


Hi Trent!

Thank you very much for your feedback!!

I will try to configure output and input buffer size as big as possible and minimize fwrite/fread function calls to avoid overhead.

Also I will configure the timeout because I suspect that maybe it could expires while Nios is reading from the output buffer and Matlab is blocked at fread waiting until input buffer is full.

I will inform you as soon I can try it!

Regards
Ben

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us