write and read serial port data
rules for completing write and read operations
completing write operations
a write operation using
write
, writeline
, or
writebinblock
completes when one of these conditions is
satisfied:
the specified data is written.
the time specified by the
timeout
property passes.
a text command is processed by the instrument only when it receives the required
terminator. for serial port objects, each occurrence of \n
in the
ascii command is replaced with the terminator
property value.
the default value of terminator
is the line feed character. refer
to the documentation for your instrument to determine the terminator required by
your instrument.
completing read operations
a read operation with
read
, readline
, or
readbinblock
completes when one of these conditions is
satisfied:
the specified number of values is read.
the time specified by the
timeout
property passes.the terminator specified by the
terminator
property is read.
writing and reading text data
this example illustrates how to communicate with a serial port instrument by writing and reading text data.
the instrument is a tektronix® tds 210 two-channel oscilloscope connected to the serial port com1. therefore, many of the commands in the example are specific to this instrument. a sine wave is input into channel 2 of the oscilloscope, and you want to measure the peak-to-peak voltage of the input signal.
these functions and properties are used when reading and writing text.
function | purpose |
---|---|
read text data from the instrument. | |
write text data to the instrument. | |
character used to terminate commands sent to the instrument. |
note
this example is windows® specific.
create a serial port object — create the serial port object
s
associated with the serial port com1.s = serialport("com1",9600);
write and read data — write the
*idn?
command to the instrument usingwriteline
, and then read back the result of the command usingreadline
.writeline(s,"*idn?") s.numbytesavailable
ans = 56
idn = readline(s)
idn = "tektronix,tds 210,0,cf:91.1ct fv:v1.16 tds2cm:cmv:v1.04"
you need to determine the measurement source. possible measurement sources include channel 1 and channel 2 of the oscilloscope.
writeline(s,"measurement:immed:source?") source = readline(s)
source = "ch1"
the scope is configured to return a measurement from channel 1. because the input signal is connected to channel 2, you must configure the instrument to return a measurement from this channel.
writeline(s,"measurement:immed:source ch2") writeline(s,"measurement:immed:source?") source = readline(s)
source = "ch2"
you can now configure the scope to return the peak-to-peak voltage, and then request the value of this measurement.
writeline(s,"measurement:meas1:type pk2pk") writeline(s,"measurement:meas1:value?")
read back the result using the
readline
function.ptop = readline(s)
ptop = "2.0199999809e0"
disconnect and clean up — clear the serial port object
s
from the matlab® workspace when you are done working with it.clear s
writing and reading binary data
this example explores binary read and write operations with a serial port object. the instrument used is a tektronix® tds 210 oscilloscope.
functions and properties
these functions are used when reading and writing binary data.
function | purpose |
---|---|
read binary data from the instrument. | |
write binary data to the instrument. |
configure and connect to the serial object
you need to create a serial object. in this example, create a serial port object associated with the com1 port.
s = serialport("com1",9600);
write binary data
you use the write
function to write binary data to the
instrument. a binary write operation completes when one of these conditions is
satisfied:
all the data is written.
a timeout occurs as specified by the
timeout
property.
note
when you perform a write operation, think of the transmitted data in terms of
values rather than bytes. a value consists of one or more bytes. for example,
one uint32
value consists of four bytes.
writing int16 binary data
write a waveform as an int16
array.
write(s,"data:destination refb","string"); write(s,"data:encdg srpbinary","string"); write(s,"data:width 2","string"); write(s,"data:start 1","string");
t = (0:499) .* 8 * pi / 500; data = round(sin(t) * 90 127); write(s,"curve #3500","string");
note that one int16
value consists of two bytes. therefore, the
following command writes 1000 bytes.
write(s,data,"int16")
reading binary data
you use the read
function to read binary data from the
instrument. a binary read operation completes when one of these conditions is
satisfied:
a timeout occurs as specified by the
timeout
property.the specified number of values is read.
note
when you perform a read operation, think of the received data in terms of
values rather than bytes. a value consists of one or more bytes. for example,
one uint32
value consists of four bytes.
reading int16 binary data
read the same waveform on channel 1 as an int16
array.
read(s,"data:source ch1","string"); read(s,"data:encdg srpbinary","string"); read(s,"data:width 2","string"); read(s,"data:start 1","string"); read(s,"data:stop 2500","string"); read(s,"curve?","string")
note that one int16
value consists of two bytes. therefore, the
following command reads 512 bytes.
data = read(s,256,"int16");
disconnect and clean up
if you are finished with the serial port object, clear the object from the workspace.
clear s