How can I read the whole input buffer of a serial port object?
22 ビュー (過去 30 日間)
古いコメントを表示
Valentino Tomasic
2015 年 6 月 22 日
コメント済み: Walter Roberson
2015 年 6 月 22 日
I am communicating with a device via rs-232 and I would like to read the whole iput buffer of the serial object at once.
What I already tried was:
s = serial('com1');
fopen(s);
fscanf(s,'%f',s.bytesavailable);
So when I run the code I only get the last value instead of the whole input buffer.
Do you have a clue what the issue could be?
0 件のコメント
採用された回答
Walter Roberson
2015 年 6 月 22 日
s = serial('com1');
fopen(s);
data = char(fread(s, s.bytesavailable));
sscanf(data,'%f');
Are you certain that you want to try to read floating point text numbers when the buffer might happen to end in the middle of a number?? For text you normally want to work line-by-line (fgetl() or fgets()) or else fscanf() without a count (allowing the scanning to pause for more characters to be received)
0 件のコメント
その他の回答 (1 件)
Valentino Tomasic
2015 年 6 月 22 日
1 件のコメント
Walter Roberson
2015 年 6 月 22 日
Is the input terminated somehow? linefeed? Or even just a comma between entries? If so then you should be programming a bytesavailablefcn callback. That callback can grab values from the serial port and store them away for a later run of the processing loop. For example you could keep a circular buffer, such as is shown blog or file exchange
参考
カテゴリ
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!