fscanf over serial port only returns one line at a time...

10 次查看(过去 30 天)
I have been trying this for two hours now, so I obviously need help.
I am reading data from the serial port, where a device sends strings of the form:
2.494e-08,0,343
Each string is terminated by a CR/LF which is set as the terminator when setting up the serial protocol. When I read this string using
fscanf(serialObject)
it returns a 17 char string, with the 16th char being the CR char and the 17th being the LR char (13/10).
Now of I read it using a format specifier of the form
fscanf(serialObject,['%f,%i,%i'])
I get the correct results, namely an array containing the numbers 2.494e-08,0,343. So far so good. Unfortunately, fscanf seems to only ever read one line at a time when accessing the serial port, even though thousands of lines are available. So it should return an array with the size 3 x number of lines available.
Any clues on to what I am missing?

回答(1 个)

Walter Roberson
Walter Roberson 2016-6-10
A read operation with fscanf blocks access to the MATLAB® command line until: * The terminator specified by the Terminator property is read. * The time specified by the Timeout property passes. * The number of values specified by size is read. * The input buffer is filled (unless size is specified)
You have terminators turned on, so the first one encountered will end the fscanf(). If you did not want that you could tell it not to use line terminators -- but if you do that then it is still not going to read indefinitely because it will at most proceed until the input buffer is filled.
  2 个评论
Johannes Rebling
Johannes Rebling 2016-6-10
Thanks a lot for the prompt reply. So I would have to not specify a terminator when setting up the Serial interface? Or is there a way to ignore the terminator when using fscanf?
Walter Roberson
Walter Roberson 2016-6-10
I do not see a method to disable the terminator. You could set it to some character that you are not likely to transmit, such as 0.
Terminator is ignored for fread() so you could fread() a bunch of data and then sscanf() it.

请先登录,再进行评论。

类别

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