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