fscanf(serial)

2 Ansichten (letzte 30 Tage)
Jason
Jason am 17 Feb. 2011
I'm trying to read data from a serial port. Right now, I use fprintf to write a bunch of command to a serial port and using fscanf to read from that port. The problem is fscanf read from the first command, but I just want it to read whatever the last command I send. example:
s = serial('COM1');
fopen(s);
fprintf(s, 'command1');
fprintf(s, 'command2');
fprintf(s, 'command3');
data = fscanf(s);
The problem is fscanf read command1 first so I would have to scan three times to get to the last command. Is there any way to just read whatever the last command you input? I believe this device store commands in a txt file format so when you scan it reads from the first line until it see the end of line. I just want it to read the very last line. Thanks.

Antworten (1)

Walter Roberson
Walter Roberson am 17 Feb. 2011
No, there is no way to do that. You could, however, use fread() to read everything that is pending, and then go backwards through the data to find the beginning of the last line and extract from there on.
  2 Kommentare
KONGPHET Vorachack
KONGPHET Vorachack am 29 Sep. 2020
I have some problem like Jason, I write fprintf(s, 'command'); data = fscanf(s);..........in the first times, it works well, I can get the data but when I sent the command second times, serial can't get the data .....there shown
Warning: Unsuccessful read: A timeout occurred before the
Terminator was reached.
'serial' unable to read any data. For more information on
possible reasons, see Serial Read Warnings.
Then I tried to used the paus (1) till (60) before the fscanf(s); but it still doesn't work.
if you have the experience about this, please sharing with me. Thank you in advance.
Vor
KONGPHET Vorachack
KONGPHET Vorachack am 29 Sep. 2020
Morover, When I reset my microcontroller....it works. But it is not that I prefer to do....

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O 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