Serial communication with timer
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi all
I want to control a external device over a serial connection. I have two 1000-element arrays in matlab with the positions to send. When I just use a for loop to send the data, then the whole array is sent in about 3 seconds. This is too fast, I just want to send one position every 10ms so that the whole process takes 10 seconds. When I add a pause(X) after sending one position, then the whole process takes about 16 seconds, and it doesn't matter if I enter 0.01 or 0.0001 as X. Why is that?
Then I tried to use a timer. But I couldn't figure out how to write the TimerFcn that it works.
Can someone help me? The beginning would be as follows.
PositionX = rand(1,1000);
PositionY = rand(1,1000);
T=timer('Period',0.01,'TasksToExecute', 1000)
T.ExecutionMode='fixedRate';
T.TimerFcn = %%insert function here %%
start(T);
What the function should do is the following:
fprintf(serial_object,PositionX(i));
fprintf(serial_object,PositionY(i));
as i goes from 1 to 1000.
Best regards and thanks for the help,
Reto
댓글 수: 3
답변 (1개)
Walter Roberson
2013년 9월 9일
T.TimerFcn = setup_timer();
function timerfcn = setup_timer
i = 0;
timefcn = @send_position;
function send_position(varargin)
i = i + 1;
if i <= length(PositionX)
fprintf(serial_object, '%9.3f %9.3f\n', PositionX(i), PositionY(i))
end
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!