Write and Read Data over TCP/IP Interface
Write Data
The write
function synchronously writes data to the remote host
connected to the tcpclient
object. First specify the data, then write
the data. The function waits until the specified number of values is written to the remote
host.
In this example, a tcpclient
object t
already
exists.
% Create a variable called data data = 1:10; % Write the data to the object t write(t, data)
Note
For any read or write operation, the data type is converted to
uint8
for the data transfer. It is then converted back to the data
type you set if you specified another data type.
Read Data
The read
function synchronously reads data from the remote host
connected to the tcpclient
object and returns the data. There are three
read options:
Read all bytes available (no arguments).
Optionally specify the number of bytes to read.
Optionally specify the data type.
If you do not specify a size, the default read uses the
BytesAvailable
property value, which is equal to the number of bytes
available in the input buffer.
In these examples, a tcpclient
object t
already
exists.
% Read all bytes available. read(t) % Specify the number of bytes to read, 5 in this case. read(t,5) % Specify the number of bytes to read, 10, and the data type, double. read(t,10,"double")
Note
For any read or write operation, the data type is converted to
uint8
for the data transfer. It is then converted back to the data
type you set if you specified another data type.
Acquire Data from Weather Station Server
One of the primary uses of TCP/IP communication is to acquire data from a server. This example shows how to acquire and plot data from a remote weather station.
Note
The IP address in this example is not a working IP address. The example shows how to connect to a remote server. Substitute the address shown here with the IP address or host name of a server you want to communicate with.
Create the
tcpclient
object using the address shown here and port1045
.t = tcpclient("172.28.154.231",1045)
t = tcpclient with properties: Address: '172.28.154.231' Port: 1045 NumBytesAvailable: 0 Show all properties, functions
Acquire data using the
read
function. Specify the number of bytes to read as30
, for 10 samples from three sensors (temperature, pressure, and humidity). Specify the data type asdouble
.data = read(t,30,"double");
Reshape the 1-by-30 data into 10-by-3 data to show one column each for temperature, pressure, and humidity.
data = reshape(data,[3,10]);
Plot the temperature.
subplot(311); plot(data(:,1));
Plot the pressure.
subplot(312); plot(data(:,2));
Plot the humidity.
subplot(313); plot(data(:,3));
Close the connection between the TCP/IP client object and the remote host by clearing the object.
clear t
Read Page from Website
In this example, you read a page from the RFC Editor Web site using a TCP/IP object.
Create a TCP/IP object. Port
80
is the standard port for web servers.t = tcpclient("www.rfc-editor.org",80);
Set the
Terminator
property of the TCP/IP object.configureTerminator(t,"LF","CR/LF");
You can now communicate with the server using the
writeline
andreadline
functions.To ask a web server to send a web page, use the
GET
command. You can ask for a text file from the RFC Editor Web site using'GET
.(path/filename)
'writeline(t,"GET /rfc/rfc793.txt");
The server receives the command and sends back the web page. You can see if any data was sent back by looking at the
NumBytesAvailable
property of the object.t.NumBytesAvailable
Now you can start to read the web page data. By default,
readline
reads one line at a time. You can read lines of data until theNumBytesAvailable
value is 0. Note that you do not see a rendered web page; the HTML file data scrolls by on the screen.while (t.NumBytesAvailable > 0) A = readline(t) end
If you want to do more communication, you can continue to read and write data. If you are done with the object, clear it.
clear t