why fgetl read '\n'?

1 Ansicht (letzte 30 Tage)
MINKYUNG KIM
MINKYUNG KIM am 17 Okt. 2019
Hi, all. I'd like to ask for advice.
What I'm laying is to show up the string data transmitted from AVR using USART(Serial communication). My AVR is sending three ascii data generated from sensors and '\n'. I call this total four data as 'data group'. Let me give you an example as below.
0\n
-5\n
10\n
\n
20\n
10\n
-1\n
\n
... and so on
So, three data would be transmitted and fourth data '\n' will sort of report the end of the current data group. I use 'fgetl'(fgetl - Read line of text from instrument and discard terminator) and expected as below, repeating N_mat times. (Please refer to the code attached!)
roll = 0 -> roll = 20 ->... and so on
pitch = -5 -> pitch = 10 ->... and so on
pressure = 10 -> pressure = -1 ->... and so on
N_mat = 50;
t_pause = 0.001;
bt = Bluetooth('JCNET-BT-7827',1);
fopen(bt);
for i = 1:1:N_mat
% remove data from input buffer
flushinput(bt);
% find the point for starting data group
LF = fgetl(bt);
tic(); pause(t_pause); toc();
while ~isempty(LF)
flushinput(bt);
LF = fgetl(bt);
tic(); pause(t_pause); toc();
end
% get the data in order if the starting point is found
roll = fgetl(bt);
tic(); pause(t_pause); toc();
pitch = fgetl(bt);
tic(); pause(t_pause); toc();
pressure = fgetl(bt);
tic(); pause(t_pause); toc();
end
Yes, it works as I expected. But roll, or pitch or pressure occasionally get ' ', which means nothing(0x0 char). I have no idea why this happen. Anyone can give me a help?

Antworten (0)

Kategorien

Mehr zu Instrument Control Toolbox Supported Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by