fscanf(serial)
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2011-2-17
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 个评论
KONGPHET Vorachack
2020-9-29
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
2020-9-29
Morover, When I reset my microcontroller....it works. But it is not that I prefer to do....
另请参阅
类别
在 Help Center 和 File 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!