Serial communication with timer
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 9 Sep. 2013
I notice you are not using a format when you fprintf(). If I recall correctly, the default format is '%s\n' which is not going to work well with your numeric data. You should provide a specific format. Also for efficiency, use a single fprintf() instead of two in a row, something like
fprintf(serial_object, '%9.3f %9.3f\n', PositionX(i), PositionY(i))
Reto Hofmann
am 10 Sep. 2013
Bearbeitet: Reto Hofmann
am 10 Sep. 2013
Walter Roberson
am 10 Sep. 2013
fwrite(s, mynumber, '=>uint8')
Antworten (1)
Walter Roberson
am 9 Sep. 2013
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
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!