Recording information to disk provides a permanent record of your serial port session, and is an easy way to debug your application. While the serial port object is connected to the device, you can record the following information to a disk file:
The number of values written to the device, the number of values read from the device, and the data type of the values
Data written to the device, and data read from the device
You record information to a disk file with the
record function. The following
table shows the properties associated with recording information to disk.
Amount of information saved to a record file
Specify whether data and event information is saved to one record file or to multiple record files
Name of the record file
Indicate if data and event information are saved to a record file
This example records the number of values written to and read from the device, and
stores the information to the file
s = serial('COM1'); fopen(s) s.RecordName = 'myfile.txt'; record(s) fprintf(s,'*IDN?') idn = fscanf(s); fprintf(s,'RS232?') rs232 = fscanf(s);
End the serial port session.
fclose(s) delete(s) clear s
myfile.txt at the command line, use the
When you initiate recording with the
record function, the
RecordMode property determines if a new record file is
created or if new information is appended to an existing record file.
You can configure
overwrite, the record file
is overwritten each time recording is initiated. If
append, the new information is appended to the file specified
index, a different disk file is created each time recording
is initiated. The rules for specifying a record filename are discussed in the next
You specify the name of the record file with the
property. You can specify any value for
including a directory path — provided the filename is supported by your
operating system. Additionally, if
index, the filename follows these rules:
Indexed filenames are identified by a number. This number precedes the filename extension and is increased by 1 for successive record files.
If no number is specified as part of the initial filename, the first
record file does not have a number associated with it. For example, if
myfile.txt is the name of the first record file,
myfile01.txt is the name of the second record file,
and so on.
RecordName is updated after the record file is
If the specified filename already exists, the existing file is overwritten.
The record file is an ASCII file that contains a record of one or more serial port
sessions. You specify the amount of information saved to a record file with the
RecordDetail can be
verbose. A compact record file contains the number of values
written to the device, the number of values read from the device, the data type of
the values, and event information. A verbose record file contains the preceding
information as well as the data transferred to and from the device.
Binary data with precision given by
int32 is recorded using hexadecimal
format. For example, if the integer value 255 is read from the instrument as a
16-bit integer, the hexadecimal value 00FF is saved in the record file. Single- and
double-precision floating-point numbers are recorded as decimal values using the
%g format, and as hexadecimal values using the format
specified by the IEEE® Standard 754-1985 for Binary Floating-Point Arithmetic.
The IEEE floating-point format includes three components: the sign bit, the exponent field, and the significant field. Single-precision floating-point values consist of 32 bits. The value is given by
Double-precision floating-point values consist of 64 bits. The value is given by
The floating-point format component, and the associated single-precision and double-precision bits are shown in the following table.
|Component||Single-Precision Bits||Double-Precision Bits|
Bit 1 is the left-most bit as stored in the record file.
This example illustrates how to record information transferred between a serial port object and a Tektronix® TDS 210 oscilloscope. Additionally, the structure of the resulting record file is presented.
Create the serial port object — Create the serial
s associated with the serial port
s = serial('COM1');
Connect to the device — Connect
s to the oscilloscope. Because the default value for
ReadAsyncMode property is
continuous, data is asynchronously returned the input
buffer as soon as it is available from the instrument.
Configure property values — Configure
s to record information to multiple disk files using
the verbose format. Recording is then initiated with the first disk file
s.RecordMode = 'index'; s.RecordDetail = 'verbose'; s.RecordName = 'WaveForm1.txt'; record(s)
Write and read data — The commands written to the instrument, and the data read from the instrument are recorded in the record file. For an explanation of the oscilloscope commands, see Example — Writing and Reading Text Data.
fprintf(s,'*IDN?') idn = fscanf(s); fprintf(s,'MEASUREMENT:IMMED:SOURCE CH2') fprintf(s,'MEASUREMENT:IMMED:SOURCE?') source = fscanf(s);
Read the peak-to-peak voltage with the
function. Note that the data returned by
recorded using hex format.
fprintf(s,'MEASUREMENT:MEAS1:TYPE PK2PK') fprintf(s,'MEASUREMENT:MEAS1:VALUE?') ptop = fread(s,s.BytesAvailable);
Convert the peak-to-peak voltage to a character array.
char(ptop)' ans = 2.0199999809E0
The recording state is toggled from
off. Because the
index, the record filename is automatically
record(s) s.RecordStatus ans = off s.RecordName ans = WaveForm2.txt
Disconnect and clean up — When you no longer need
s, disconnect it from the instrument, and remove it
from memory and from the MATLAB® workspace.
fclose(s) delete(s) clear s
The contents of the
WaveForm1.txt record file are shown
below. Because the
RecordDetail property was
verbose, the number of values, commands, and data were
recorded. Note that data returned by the
fread function is
in hex format.
type WaveForm1.txt Legend: * - An event occurred. > - A write operation occurred. < - A read operation occurred. 1 Recording on 22-Jan-2000 at 11:21:21.575. Binary data in... 2 > 6 ascii values. *IDN? 3 < 56 ascii values. TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04 4 > 29 ascii values. MEASUREMENT:IMMED:SOURCE CH2 5 > 26 ascii values. MEASUREMENT:IMMED:SOURCE? 6 < 4 ascii values. CH2 7 > 27 ascii values. MEASUREMENT:MEAS1:TYPE PK2PK 8 > 25 ascii values. MEASUREMENT:MEAS1:VALUE? 9 < 15 uchar values. 32 2e 30 31 39 39 39 39 39 38 30 39 45 30 0a 10 Recording off.