First fscanf(s) returns an empty array.

조회 수: 4 (최근 30일)
Reuben
Reuben 2013년 10월 22일
답변: David Sanchez 2013년 10월 22일
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

답변 (1개)

David Sanchez
David Sanchez 2013년 10월 22일
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.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by