Timed excel loop for serial port

4 次查看(过去 30 天)
Avichal
Avichal 2013-4-11
I am trying to create a MATLAB program using an excel sheet with 500 values below 10, and 500 values above 10, a total of 1000 values. I need the program to read the values in a 10 second loop. Every time it reads values below ten it should send a signal of "1" to the serial port, and every time it reads the values above 10 it should send a signal of "0" to the serial port. How should I write this?

回答(2 个)

Walter Roberson
Walter Roberson 2013-4-12
num = xlsread('YourFile.xls');
s = serial('COM1');
fopen(s);
for K = 1 : length(num)
thisval = num(K);
signaltosend = 1;
if thisval > 10; signaltosend = 0; end
fwrite(s, signaltosend, 'uint8');
pause(10);
end
fclose(s);
If you need '1' and '0' instead of 1 and 0, then the change is straight-forward.

Avichal
Avichal 2013-4-17
Thanks! It worked!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by