fscanf(serial)

2 ビュー (過去 30 日間)
Jason
Jason 2011 年 2 月 17 日
コメント済み: KONGPHET Vorachack 2020 年 9 月 29 日
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.

回答 (1 件)

Walter Roberson
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
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
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 ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by