First fscanf(s) returns an empty array.

7 Ansichten (letzte 30 Tage)
Reuben
Reuben am 22 Okt. 2013
Beantwortet: David Sanchez am 22 Okt. 2013
Almost every first read of fscanf() returns a empty array. Am I not setting up fopen correctly? Other than the first read, everything works fine. (The empty array causes problems when trying to save the value. My intention is to make this as simple as possible because the code is for first year programming students) Thank you.
clc
delete(instrfindall)
socket = serial('COM12','BAUD',57600);
fopen(socket);
pause(3);
for i = 1:30
data = str2num(fscanf(socket,'%s'));
disp ([data, length(data),isempty(data)])
raw_data(i) = data;
end

Antworten (1)

David Sanchez
David Sanchez am 22 Okt. 2013
You say "almost" every first reading of the serial port returns an empty array. That could mean that there's nothing in the buffer "almost" every first reading and your code is fine. Make sure the devices are properly synchronized and something was sent beforehand when you try to read.
Use isempty to avoid saving the empty string.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by