fscanf(serial)
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Walter Roberson
on 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 Comments
KONGPHET Vorachack
on 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
on 29 Sep 2020
Morover, When I reset my microcontroller....it works. But it is not that I prefer to do....
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!