read
Description
reads the number of values specified by data
= read(t
,count
,datatype
)count
in the form specified by
datatype
and returns the data. The datatype
argument is a character vector of a standard MATLAB data type. For all numeric datatype
types,
data
is a row vector of double values. For the text type
datatype
values of "char"
or
"string"
, data
is of the specified type.
Examples
Read uint8
Data Sent to TCP/IP Server
Create a TCP/IP server that listens for a client connection request at the specified port and IP address. Then, read data sent to the server from the connected client.
Create a TCP/IP server that listens for connections at localhost
and port 4000.
server = tcpserver("localhost",4000)
server = TCPServer with properties: ServerAddress: "127.0.0.1" ServerPort: 4000 Connected: 0 ClientAddress: "" ClientPort: [] NumBytesAvailable: 0 Show all properties, functions
Create a TCP/IP client to connect to your server object using tcpclient
. You must specify the same IP address and port number you use to create server
.
client = tcpclient("localhost",4000)
client = tcpclient with properties: Address: 'localhost' Port: 4000 NumBytesAvailable: 0 Show all properties, functions
Display the values of the Connected
, ClientAddress
, and ClientPort
properties for server
.
server
server = TCPServer with properties: ServerAddress: "127.0.0.1" ServerPort: 4000 Connected: 1 ClientAddress: "127.0.0.1" ClientPort: 59357 NumBytesAvailable: 0 Show all properties, functions
The output shows that server
successfully accepts a request from client
and that client
establishes a connection to server
.
Write data to the TCP/IP client. Since the client is connected to the server, this data is available in the server. Read the data using the server
object.
write(client,[4,8,15,16,23,42],"uint8")
read(server,server.NumBytesAvailable)
ans = 1×6
4 8 15 16 23 42
Read String Data Sent to TCP/IP Server
Create a TCP/IP server that listens for a client connection request at the specified port and IP address. Then read data sent to the server from the connected client.
Create a TCP/IP server that listens for connections at localhost
and port 4000.
server = tcpserver("localhost",4000)
server = TCPServer with properties: ServerAddress: "127.0.0.1" ServerPort: 4000 Connected: 0 ClientAddress: "" ClientPort: [] NumBytesAvailable: 0 Show all properties, functions
Create a TCP/IP client to connect to your server object using tcpclient
. You must specify the same IP address and port number you use to create server
.
client = tcpclient("localhost",4000)
client = tcpclient with properties: Address: 'localhost' Port: 4000 NumBytesAvailable: 0 Show all properties, functions
Display the values of the Connected
, ClientAddress
, and ClientPort
properties for server
.
server
server = TCPServer with properties: ServerAddress: "127.0.0.1" ServerPort: 4000 Connected: 1 ClientAddress: "127.0.0.1" ClientPort: 65440 NumBytesAvailable: 0 Show all properties, functions
The output shows that server
successfully accepts a request from client
and that client
establishes a connection to server
.
Write data to the TCP/IP client. Since the client is connected to the server, this data is available in the server. Read the first five values of string data using the server
object.
write(client,"helloworld","string") read(server,5,"string")
ans = "hello"
If you read five more values, you receive the remaining string data.
read(server,5,"string")
ans = "world"
Input Arguments
t
— TCP/IP server
tcpserver
object
TCP/IP server, specified as a tcpserver
object.
Example: read(t,5)
reads data sent to the TCP/IP server
t
from the client connected to it.
count
— Number of values to read
numeric
Number of values to read, specified as a positive integer value. If
count
is greater than the NumBytesAvailable
property of t
, the function suspends MATLAB execution and waits until it reads the specified amount of data or a
timeout occurs.
Example: read(device,2)
reads two values of
uint8
data.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
datatype
— Size and format of each value
"uint8"
(default) | "int8"
| "uint16"
| "int16"
| "uint32"
| "int32"
| "uint64"
| "int64"
| "single"
| "double"
| "char"
| "string"
Size and format of each value, specified as a character vector or string.
datatype
determines the number of bytes to read for each value
and the interpretation of those bytes as a MATLAB data type.
Example: read(t,1,"uint16")
reads one value of
uint16
data. Each uint16 value is two bytes.
Data Types: char
| string
Version History
Introduced in R2021a
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)