필터 지우기
필터 지우기

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

조회 수: 13 (최근 30일)
Johannes Rebling
Johannes Rebling 2016년 6월 10일
댓글: Walter Roberson 2016년 6월 10일
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