Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Serial object, accuracy of TimerFcn callback
Date: Tue, 6 May 2008 17:19:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 55
Message-ID: <fvq3u7$jr8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210094344 20328 172.30.248.38 (6 May 2008 17:19:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 6 May 2008 17:19:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1377509
Xref: news.mathworks.com comp.soft-sys.matlab:466965



Hello!

I'm trying to communicate with an external device over a
serial link. Unfortunately the communications protocol of
the external device requires me to send a data request every
32ms over the serial port.
So far I've tried to use the serial object and the TimerFcn
callback with an TimerCnt set to 32e-3. But the accuracy of
the callback jitters havily (around 20ms, sometimes even
more) and highly depends the acutal system load. 
I'm using WinXp and Matlab R2007a and know that WinXp is by
far not a real time system, but is there any better way to
do this? I need a maximal jitter of 3ms. How could I
possibly achive this?

Is there any way to increase the priority of the timer?

Thanks for your ideas!

Here is the code I used:

% set up serial port
s = serial('COM1');
s.Baudrate = 19200;
s.Parity = 'none';
s.StopBits = 1;
s.InputBufferSize = 2048;
s.OutputBufferSize = 1024;
s.ByteOrder = 'bigEndian';
% set up calbacks
s.BytesAvailableFcnMode  = 'byte';
s.BytesAvailableFcnCount = 524;
s.BytesAvailableFcn      = @readBlock;
s.TimerPeriod            = 32e-3;
s.TimerFcn               = @sendDataRequest;

% callbacks
function sendDataRequest(obj,event)
   global rq_cnt;
   type = 2; % data request
   data = [rq_cnt 1];
   len = length(data);
   
   %send data request telegram
   msg = addCRC([type len data]);
   fwrite(obj,msg,'async');

   rq_cnt = rq_cnt + 1;
   if rq_cnt == 64
       rq_cnt = 0;
   end
end