<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239083</link>
    <title>MATLAB Central Newsreader - Time elapsed using serial fwrite and fread functions</title>
    <description>Feed for thread: Time elapsed using serial fwrite and fread functions</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 11 Nov 2008 22:10:18 -0500</pubDate>
      <title>Time elapsed using serial fwrite and fread functions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239083#610382</link>
      <author>Benjamin </author>
      <description>Hi all!&lt;br&gt;
&lt;br&gt;
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:&lt;br&gt;
&lt;br&gt;
....&lt;br&gt;
tic&lt;br&gt;
for i=1:length(data_to_send) %length(data_to_send)=100e3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tic&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fwrite(s,data_to_send(i),'uchar');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;time_write(i)=toc;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tic&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(fread(s,1,'uchar')~=0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Error('Unexpected error while reading UART');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;time_read(i)=toc;&lt;br&gt;
end&lt;br&gt;
time_elapsed=toc;&lt;br&gt;
fprintf (1,'Elapsed CPU time (min) = %f\n',time_elapsed/60);&lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Does anybody know if there is a manner to increase the perfomance of the serial transmision? Thanks in advance,&lt;br&gt;
&lt;br&gt;
Ben</description>
    </item>
    <item>
      <pubDate>Wed, 12 Nov 2008 01:10:58 -0500</pubDate>
      <title>Re: Time elapsed using serial fwrite and fread functions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239083#610401</link>
      <author>Trent Jarvi</author>
      <description>&lt;br&gt;
&quot;Benjamin &quot; &amp;lt;benjaminst@gmail.com&amp;gt; wrote in message &lt;br&gt;
news:gfcvsa$dpv$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hi all!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Currently I am working in my master thesis. It consists on implementing an &lt;br&gt;
&amp;gt; arbitrary waveform generator using Matlab and a FPGA. So, I need to &lt;br&gt;
&amp;gt; transfer signal samples (100Kbytes) from Matlab to a Altera Nios 2 &lt;br&gt;
&amp;gt; processor(85MHz) using a serial protocol (UART 115Kbps). I have done the &lt;br&gt;
&amp;gt; following code which I have tested and it works ok:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ....&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; for i=1:length(data_to_send) %length(data_to_send)=100e3&lt;br&gt;
&amp;gt;    tic&lt;br&gt;
&amp;gt;    fwrite(s,data_to_send(i),'uchar');&lt;br&gt;
&amp;gt;    time_write(i)=toc;&lt;br&gt;
&amp;gt;    tic&lt;br&gt;
&amp;gt;    if(fread(s,1,'uchar')~=0)&lt;br&gt;
&amp;gt;        Error('Unexpected error while reading UART');&lt;br&gt;
&amp;gt;    end&lt;br&gt;
&amp;gt;    time_read(i)=toc;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; time_elapsed=toc;&lt;br&gt;
&amp;gt; fprintf (1,'Elapsed CPU time (min) = %f\n',time_elapsed/60);&lt;br&gt;
&amp;gt; ...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The main problem is that it is too slow (the elapsed cpu time is more than &lt;br&gt;
&amp;gt; 30minutes!). I can't understand it because the expected time a grosso modo &lt;br&gt;
&amp;gt; would be 100Kbytes/115.2Kbits/s=6,94seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I'm sure that the problem is not from Nios 2 processor because the main &lt;br&gt;
&amp;gt; function is just a infinite loop that reads from the uart and send 0 &lt;br&gt;
&amp;gt; character.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Does anybody know if there is a manner to increase the perfomance of the &lt;br&gt;
&amp;gt; serial transmision? Thanks in advance,&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
Hi Ben,&lt;br&gt;
&lt;br&gt;
There is considerable overhead in the parameter checking for each function &lt;br&gt;
call made.  You may want to reduce the number of calls if possible to &lt;br&gt;
improve the throughput.  As an example, I performed the following on a &lt;br&gt;
loopback connection to see how close MATLAB Serial was to the 6.94 seconds &lt;br&gt;
(write) you mention:&lt;br&gt;
&lt;br&gt;
chunk=5120;&lt;br&gt;
data_to_send=mod(1:102401,255);&lt;br&gt;
s=serial('/dev/ttyS0');&lt;br&gt;
s.outputbuffersize=chunk;&lt;br&gt;
s.inputbuffersize=chunk;&lt;br&gt;
s.baudrate=115200;&lt;br&gt;
fopen(s);&lt;br&gt;
tic;&lt;br&gt;
a=[];&lt;br&gt;
for i=0:length(data_to_send)/chunk-1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fwrite(s,data_to_send((1:chunk)+chunk*i),'uchar');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = [a fread(s,5120,'uchar')'];&lt;br&gt;
end&lt;br&gt;
toc;&lt;br&gt;
fclose(s);&lt;br&gt;
delete(s);&lt;br&gt;
clear('s');&lt;br&gt;
&lt;br&gt;
The result on R2008b was reasonable for a quick test:&lt;br&gt;
&lt;br&gt;
Elapsed time is 9.361430 seconds.&lt;br&gt;
&lt;br&gt;
Thats actually less time than one would expect for both read and write if &lt;br&gt;
they did not overlap underneath. </description>
    </item>
    <item>
      <pubDate>Wed, 12 Nov 2008 08:58:03 -0500</pubDate>
      <title>Re: Time elapsed using serial fwrite and fread functions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239083#610433</link>
      <author>Benjamin </author>
      <description>&quot;Trent Jarvi&quot; &amp;lt;tjarvi@mathworks.com&amp;gt; wrote in message &amp;lt;gfdaf2$el3$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Benjamin &quot; &amp;lt;benjaminst@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; news:gfcvsa$dpv$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &amp;gt; Hi all!&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Currently I am working in my master thesis. It consists on implementing an &lt;br&gt;
&amp;gt; &amp;gt; arbitrary waveform generator using Matlab and a FPGA. So, I need to &lt;br&gt;
&amp;gt; &amp;gt; transfer signal samples (100Kbytes) from Matlab to a Altera Nios 2 &lt;br&gt;
&amp;gt; &amp;gt; processor(85MHz) using a serial protocol (UART 115Kbps). I have done the &lt;br&gt;
&amp;gt; &amp;gt; following code which I have tested and it works ok:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; ....&lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; for i=1:length(data_to_send) %length(data_to_send)=100e3&lt;br&gt;
&amp;gt; &amp;gt;    tic&lt;br&gt;
&amp;gt; &amp;gt;    fwrite(s,data_to_send(i),'uchar');&lt;br&gt;
&amp;gt; &amp;gt;    time_write(i)=toc;&lt;br&gt;
&amp;gt; &amp;gt;    tic&lt;br&gt;
&amp;gt; &amp;gt;    if(fread(s,1,'uchar')~=0)&lt;br&gt;
&amp;gt; &amp;gt;        Error('Unexpected error while reading UART');&lt;br&gt;
&amp;gt; &amp;gt;    end&lt;br&gt;
&amp;gt; &amp;gt;    time_read(i)=toc;&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; time_elapsed=toc;&lt;br&gt;
&amp;gt; &amp;gt; fprintf (1,'Elapsed CPU time (min) = %f\n',time_elapsed/60);&lt;br&gt;
&amp;gt; &amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; The main problem is that it is too slow (the elapsed cpu time is more than &lt;br&gt;
&amp;gt; &amp;gt; 30minutes!). I can't understand it because the expected time a grosso modo &lt;br&gt;
&amp;gt; &amp;gt; would be 100Kbytes/115.2Kbits/s=6,94seconds.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I'm sure that the problem is not from Nios 2 processor because the main &lt;br&gt;
&amp;gt; &amp;gt; function is just a infinite loop that reads from the uart and send 0 &lt;br&gt;
&amp;gt; &amp;gt; character.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Does anybody know if there is a manner to increase the perfomance of the &lt;br&gt;
&amp;gt; &amp;gt; serial transmision? Thanks in advance,&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hi Ben,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There is considerable overhead in the parameter checking for each function &lt;br&gt;
&amp;gt; call made.  You may want to reduce the number of calls if possible to &lt;br&gt;
&amp;gt; improve the throughput.  As an example, I performed the following on a &lt;br&gt;
&amp;gt; loopback connection to see how close MATLAB Serial was to the 6.94 seconds &lt;br&gt;
&amp;gt; (write) you mention:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; chunk=5120;&lt;br&gt;
&amp;gt; data_to_send=mod(1:102401,255);&lt;br&gt;
&amp;gt; s=serial('/dev/ttyS0');&lt;br&gt;
&amp;gt; s.outputbuffersize=chunk;&lt;br&gt;
&amp;gt; s.inputbuffersize=chunk;&lt;br&gt;
&amp;gt; s.baudrate=115200;&lt;br&gt;
&amp;gt; fopen(s);&lt;br&gt;
&amp;gt; tic;&lt;br&gt;
&amp;gt; a=[];&lt;br&gt;
&amp;gt; for i=0:length(data_to_send)/chunk-1&lt;br&gt;
&amp;gt;     fwrite(s,data_to_send((1:chunk)+chunk*i),'uchar');&lt;br&gt;
&amp;gt;     a = [a fread(s,5120,'uchar')'];&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; toc;&lt;br&gt;
&amp;gt; fclose(s);&lt;br&gt;
&amp;gt; delete(s);&lt;br&gt;
&amp;gt; clear('s');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The result on R2008b was reasonable for a quick test:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Elapsed time is 9.361430 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thats actually less time than one would expect for both read and write if &lt;br&gt;
&amp;gt; they did not overlap underneath. &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Hi Trent!&lt;br&gt;
&lt;br&gt;
Thank you very much for your feedback!!&lt;br&gt;
&lt;br&gt;
I will try to configure output and input buffer size as big as possible and minimize fwrite/fread function calls to avoid overhead. &lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
I will inform you as soon I can try it!&lt;br&gt;
&lt;br&gt;
Regards&lt;br&gt;
Ben</description>
    </item>
  </channel>
</rss>

