Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Serial object, accuracy of TimerFcn callback
Date: Thu, 8 May 2008 09:17:54 -0400
Organization: The MathWorks, Inc.
Lines: 69
Message-ID: <fvuui2$sla$1@fred.mathworks.com>
References: <fvq3u7$jr8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: jarvit.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1210252674 29354 172.31.56.68 (8 May 2008 13:17:54 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 8 May 2008 13:17:54 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
Xref: news.mathworks.com comp.soft-sys.matlab:467383




"Sebastian " <feese@cs.tu-berlin.de> wrote in message 
news:fvq3u7$jr8$1@fred.mathworks.com...
> 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
>
>
>

Without a realtime OS, anything less than 10 ms is not really possible with 
MATLAB Serial.  ~20 ms is fairly typical.  You end up waiting for your slice 
of tasks to get through the CPU.  Sometimes they will be very close, other 
times you get stuck in the queue when your time passes.  MATLAB Serial was 
not written considering your use case 10 years ago so it will be very 
difficult if not impossible to get 3 ms resolution.  You may want to 
investigate the realtime offerings that have these type of use cases in 
mind.