Thread Subject: Serial object, accuracy of TimerFcn callback

Subject: Serial object, accuracy of TimerFcn callback

From: Sebastian

Date: 6 May, 2008 17:19:04

Message: 1 of 2

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

Subject: Serial object, accuracy of TimerFcn callback

From: Trent Jarvi

Date: 8 May, 2008 13:17:54

Message: 2 of 2


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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
serial object Sebastian 6 May, 2008 13:20:22
timer accuracy Sebastian 6 May, 2008 13:20:22
callback Sebastian 6 May, 2008 13:20:22
rssFeed for this Thread

Contact us at files@mathworks.com