<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168823</link>
    <title>MATLAB Central Newsreader - Serial object, accuracy of TimerFcn callback</title>
    <description>Feed for thread: Serial object, accuracy of TimerFcn callback</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, 06 May 2008 17:19:04 -0400</pubDate>
      <title>Serial object, accuracy of TimerFcn callback</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168823#430619</link>
      <author>Sebastian </author>
      <description>Hello!&lt;br&gt;
&lt;br&gt;
I'm trying to communicate with an external device over a&lt;br&gt;
serial link. Unfortunately the communications protocol of&lt;br&gt;
the external device requires me to send a data request every&lt;br&gt;
32ms over the serial port.&lt;br&gt;
So far I've tried to use the serial object and the TimerFcn&lt;br&gt;
callback with an TimerCnt set to 32e-3. But the accuracy of&lt;br&gt;
the callback jitters havily (around 20ms, sometimes even&lt;br&gt;
more) and highly depends the acutal system load. &lt;br&gt;
I'm using WinXp and Matlab R2007a and know that WinXp is by&lt;br&gt;
far not a real time system, but is there any better way to&lt;br&gt;
do this? I need a maximal jitter of 3ms. How could I&lt;br&gt;
possibly achive this?&lt;br&gt;
&lt;br&gt;
Is there any way to increase the priority of the timer?&lt;br&gt;
&lt;br&gt;
Thanks for your ideas!&lt;br&gt;
&lt;br&gt;
Here is the code I used:&lt;br&gt;
&lt;br&gt;
% set up serial port&lt;br&gt;
s = serial('COM1');&lt;br&gt;
s.Baudrate = 19200;&lt;br&gt;
s.Parity = 'none';&lt;br&gt;
s.StopBits = 1;&lt;br&gt;
s.InputBufferSize = 2048;&lt;br&gt;
s.OutputBufferSize = 1024;&lt;br&gt;
s.ByteOrder = 'bigEndian';&lt;br&gt;
% set up calbacks&lt;br&gt;
s.BytesAvailableFcnMode  = 'byte';&lt;br&gt;
s.BytesAvailableFcnCount = 524;&lt;br&gt;
s.BytesAvailableFcn      = @readBlock;&lt;br&gt;
s.TimerPeriod            = 32e-3;&lt;br&gt;
s.TimerFcn               = @sendDataRequest;&lt;br&gt;
&lt;br&gt;
% callbacks&lt;br&gt;
function sendDataRequest(obj,event)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;global rq_cnt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;type = 2; % data request&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;data = [rq_cnt 1];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;len = length(data);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;%send data request telegram&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;msg = addCRC([type len data]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;fwrite(obj,msg,'async');&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;rq_cnt = rq_cnt + 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if rq_cnt == 64&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rq_cnt = 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 13:17:54 -0400</pubDate>
      <title>Re: Serial object, accuracy of TimerFcn callback</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168823#431037</link>
      <author>Trent Jarvi</author>
      <description>&lt;br&gt;
&quot;Sebastian &quot; &amp;lt;feese@cs.tu-berlin.de&amp;gt; wrote in message &lt;br&gt;
news:fvq3u7$jr8$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hello!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I'm trying to communicate with an external device over a&lt;br&gt;
&amp;gt; serial link. Unfortunately the communications protocol of&lt;br&gt;
&amp;gt; the external device requires me to send a data request every&lt;br&gt;
&amp;gt; 32ms over the serial port.&lt;br&gt;
&amp;gt; So far I've tried to use the serial object and the TimerFcn&lt;br&gt;
&amp;gt; callback with an TimerCnt set to 32e-3. But the accuracy of&lt;br&gt;
&amp;gt; the callback jitters havily (around 20ms, sometimes even&lt;br&gt;
&amp;gt; more) and highly depends the acutal system load.&lt;br&gt;
&amp;gt; I'm using WinXp and Matlab R2007a and know that WinXp is by&lt;br&gt;
&amp;gt; far not a real time system, but is there any better way to&lt;br&gt;
&amp;gt; do this? I need a maximal jitter of 3ms. How could I&lt;br&gt;
&amp;gt; possibly achive this?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Is there any way to increase the priority of the timer?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks for your ideas!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Here is the code I used:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; % set up serial port&lt;br&gt;
&amp;gt; s = serial('COM1');&lt;br&gt;
&amp;gt; s.Baudrate = 19200;&lt;br&gt;
&amp;gt; s.Parity = 'none';&lt;br&gt;
&amp;gt; s.StopBits = 1;&lt;br&gt;
&amp;gt; s.InputBufferSize = 2048;&lt;br&gt;
&amp;gt; s.OutputBufferSize = 1024;&lt;br&gt;
&amp;gt; s.ByteOrder = 'bigEndian';&lt;br&gt;
&amp;gt; % set up calbacks&lt;br&gt;
&amp;gt; s.BytesAvailableFcnMode  = 'byte';&lt;br&gt;
&amp;gt; s.BytesAvailableFcnCount = 524;&lt;br&gt;
&amp;gt; s.BytesAvailableFcn      = @readBlock;&lt;br&gt;
&amp;gt; s.TimerPeriod            = 32e-3;&lt;br&gt;
&amp;gt; s.TimerFcn               = @sendDataRequest;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; % callbacks&lt;br&gt;
&amp;gt; function sendDataRequest(obj,event)&lt;br&gt;
&amp;gt;   global rq_cnt;&lt;br&gt;
&amp;gt;   type = 2; % data request&lt;br&gt;
&amp;gt;   data = [rq_cnt 1];&lt;br&gt;
&amp;gt;   len = length(data);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;   %send data request telegram&lt;br&gt;
&amp;gt;   msg = addCRC([type len data]);&lt;br&gt;
&amp;gt;   fwrite(obj,msg,'async');&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;   rq_cnt = rq_cnt + 1;&lt;br&gt;
&amp;gt;   if rq_cnt == 64&lt;br&gt;
&amp;gt;       rq_cnt = 0;&lt;br&gt;
&amp;gt;   end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
Without a realtime OS, anything less than 10 ms is not really possible with &lt;br&gt;
MATLAB Serial.  ~20 ms is fairly typical.  You end up waiting for your slice &lt;br&gt;
of tasks to get through the CPU.  Sometimes they will be very close, other &lt;br&gt;
times you get stuck in the queue when your time passes.  MATLAB Serial was &lt;br&gt;
not written considering your use case 10 years ago so it will be very &lt;br&gt;
difficult if not impossible to get 3 ms resolution.  You may want to &lt;br&gt;
investigate the realtime offerings that have these type of use cases in &lt;br&gt;
mind. </description>
    </item>
  </channel>
</rss>

