How To Use Fscanf To Read The Real Time Serial Data In Matlab
Reading Information
This section describes reading data from your series port device in three parts:
- The Input Buffer and Data Flow describes the menses of data from the device to MATLAB.
- Reading Text Data describes how to read from the device, and format the data as text.
- Reading Binary Data describes how to read binary (numerical) information from the device.
The functions associated with reading information are given below.
Office Proper name | Description |
fgetl | Read one line of text from the device and discard the terminator |
fgets | Read one line of text from the device and include the terminator |
fread | Read binary data from the device |
fscanf | Read data from the device, and format as text |
readasync | Read data asynchronously from the device |
stopasync | Stop asynchronous read and write operations |
The properties associated with reading data are given beneath.
Property Name | Description |
BytesAvailable | Signal the number of bytes available in the input buffer |
InputBufferSize | Specify the size of the input buffer in bytes |
ReadAsyncMode | Specify whether an asynchronous read operation is continuous or transmission |
Timeout | Specify the waiting time to complete a read or write operation |
TransferStatus | Betoken if an asynchronous read or write operation is in progress |
ValuesReceived | Point the total number of values read from the device |
The Input Buffer and Information Flow
The input buffer is computer memory allocated by the series port object to store data that is to exist read from the device. When reading data from your device, the data flow follows these two steps:
- The data read from the device is stored in the input buffer.
- The data in the input buffer is returned to the MATLAB variable specified by the read part.
The InputBufferSize
property specifies the maximum number of bytes that you tin can shop in the input buffer. The BytesAvailable
holding indicates the number of bytes currently bachelor to exist read from the input buffer. The default values for these backdrop are given below.
-
south = serial('COM1'); get(s,{'InputBufferSize','
BytesAvailable'})
ans = [512] [0]
If you effort to read more data than can fit in the input buffer, an mistake is returned and no data is read.
For case, suppose yous use the fscanf
office to read the text-based response of the *IDN?
control previously written to the TDS 210 oscilloscope. As shown below, the text information is first read into to the input buffer via the serial port.
Note that for a given read functioning, y'all might not know the number of bytes returned past the device. Therefore, you might demand to preset the InputBufferSize
property to a sufficiently large value before connecting the serial port object.
Equally shown below, later the data is stored in the input buffer, it is then transferred to the output variable specified by fscanf
.
Reading Text Data
You use the fgetl
, fgets
, and fscanf
functions to read data from the device, and format the information equally text.
For case, suppose you desire to return identification information for the oscilloscope. This requires writing the *IDN?
command to the instrument, so reading back the result of that command.
-
s = serial('COM1'); fopen(south) fprintf(south,'*IDN?') out = fscanf(s) out = TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.sixteen TDS2CM:CMV:v1.04
Past default, fscanf
reads data using the %c
format because the data returned by many serial port devices is text based. However, yous tin specify many other formats equally described in the fscanf
reference pages.
You tin verify the number of values read from the device - including the terminator - with the ValuesReceived
property.
-
southward.ValuesReceived ans = 56
Synchronous Versus Asynchronous Read Operations. You specify whether read operations are synchronous or asynchronous with the ReadAsyncMode
property. Y'all can configure ReadAsyncMode
to continuous
or manual
.
If ReadAsyncMode
is continuous
(the default value), the series port object continuously queries the device to make up one's mind if information is available to exist read. If data is available, it is asynchronously stored in the input buffer. To transfer the information from the input buffer to MATLAB, you use ane of the synchronous (blocking) read functions such equally fgetl
or fscanf
. If data is available in the input buffer, these functions will return rapidly.
-
s.ReadAsyncMode = 'continuous'; fprintf(south,'*IDN?') southward.BytesAvailable ans = 56 out = fscanf(s);
If ReadAsyncMode
is transmission
, the series port object does not continuously query the device to determine if information is available to be read. To read information asynchronously, you use the readasync
office.You and so use 1 of the synchronous read functions to transfer data from the input buffer to MATLAB.
-
s.ReadAsyncMode = 'manual'; fprintf(s,'*IDN?') s.BytesAvailable ans = 0 readasync(s) southward.BytesAvailable ans = 56 out = fscanf(s);
Asynchronous operations exercise non cake admission to the MATLAB command line. Additionally, while an asynchronous read performance is in progress, y'all tin:
- Execute an asynchronous write operation because series ports have separate pins for reading and writing
- Brand use of all supported callback properties
You lot can determine which asynchronous operations are in progress with the TransferStatus
property. If no asynchronous operations are in progress, and so TransferStatus
is idle
.
-
s.TransferStatus ans = idle
Rules for Completing a Read Operation with fscanf. A read operation with fscanf
blocks access to the MATLAB command line until:
- The terminator specified past the
Terminator
property is read. - The time specified by the
Timeout
property passes. - The specified number of values specified is read.
- The input buffer is filled.
Reading Binary Data
You use the fread
office to read binary data from the device. Reading binary information means that y'all return numerical values to MATLAB.
For example, suppose you want to return the cursor and display settings for the oscilloscope. This requires writing the CURSOR?
and Brandish?
commands to the instrument, and and so reading back the results of those commands.
-
south = serial('COM1'); fopen(s)
fprintf(s,'CURSOR?')
fprintf(s,'Display?')
Considering the default value for the ReadAsyncMode
belongings is continuous
, information is asynchronously returned to the input buffer as before long as it is available from the device. You can verify the number of values read with the BytesAvailable property.
-
due south.BytesAvailable ans = 69
You lot can render the data to MATLAB using any of the synchronous read functions. Nevertheless, if you use fgetl
, fgets
, or fscanf
, and so y'all must issue the role twice considering there are ii terminators stored in the input buffer. If you utilize fread
, then you lot can return all the data to MATLAB in ane function call.
-
out = fread(south,69);
Past default, fread
returns numerical values in double precision arrays. Nevertheless, you can specify many other precisions equally described in the fread
reference pages. You can convert the numerical data to text using the MATLAB char
function.
-
val = char(out)' val = HBARS;CH1;SECONDS;-1.0E-three;1.0E-three;VOLTS;-6.56E-one;6.24E-1 YT;DOTS;0;45
For more information about synchronous and asynchronous read operations, refer to Reading Text Data. For a clarification of the rules used past fread
to complete a read operation, refer to its reference pages.
Writing Information | Instance: Writing and Reading Text Data |
© 1994-2005 The MathWorks, Inc.
How To Use Fscanf To Read The Real Time Serial Data In Matlab,
Source: http://matlab.izmiran.ru/help/techdoc/matlab_external/ch_ser23.html
Posted by: jacobsinen1957.blogspot.com
0 Response to "How To Use Fscanf To Read The Real Time Serial Data In Matlab"
Post a Comment