How can I read the whole input buffer of a serial port object?

28 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
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)

更多回答(1 个)

Valentino Tomasic
Valentino Tomasic 2015-6-22
Hi Walter! Firstly, I want to thank you for your fast response! Your advice worked perfectly, I could read out the whole buffer at once. My main problem is that the device sends data faster than my processing loop is (it's a real time plot). This is why the input buffer is getting filled progressively. So I thought the problem could be solved if the buffer is being read out completely.
  1 个评论
Walter Roberson
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 CenterFile 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!

Translated by