First fscanf(s) returns an empty array.

7 views (last 30 days)
Reuben
Reuben on 22 Oct 2013
Answered: David Sanchez on 22 Oct 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

Answers (1)

David Sanchez
David Sanchez on 22 Oct 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.

Products

Community Treasure Hunt

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

Start Hunting!