fscanf over serial port only returns one line at a time...
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Walter Roberson
on 10 Jun 2016
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 Comments
Walter Roberson
on 10 Jun 2016
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.
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!