Borrar filtros
Borrar filtros

fscanf(serial)

5 visualizaciones (últimos 30 días)
Jason
Jason el 17 de Feb. de 2011
Comentada: KONGPHET Vorachack el 29 de Sept. de 2020
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.

Respuestas (1)

Walter Roberson
Walter Roberson el 17 de Feb. de 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 comentarios
KONGPHET Vorachack
KONGPHET Vorachack el 29 de Sept. de 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 el 29 de Sept. de 2020
Morover, When I reset my microcontroller....it works. But it is not that I prefer to do....

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by