First fscanf(s) returns an empty array.

4 visualizaciones (últimos 30 días)
Reuben
Reuben el 22 de Oct. de 2013
Respondida: David Sanchez el 22 de Oct. de 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

Respuestas (1)

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

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by