Main Content

Transition Your Code totcpclientInterface

Thetcpipfunction, its object functions, and its properties will be removed. Use thetcpclientinterface instead.

tcpipInterface tcpclientInterface Example
tcpip tcpclient Create a TCP/IP Client
fwriteandfread writeandread Write and Read
fprintf writeline Read Terminated String
fscanf,fgetl, andfgets readline Read Terminated String
Read and Parse String Data
query writeread Write and Read Back Data
binblockwriteandbinblockread writebinblockandreadbinblock Write and Read Data with the Binary Block Protocol
flushinputandflushoutput flush Flush Data from Memory
Terminator configureTerminator Set Terminator
BytesAvailableFcnCount,BytesAvailableFcnMode,BytesAvailableFcn, andBytesAvailable configureCallback Set Up Callback Function
tcpipProperties tcpclientProperties

Create a TCP/IP Client

These examples show how to create and clear a TCP/IP client using the recommended functionality.

Functionality Use This Instead
t = tcpip(“localhost”,3030); fopen(t)
t = tcpclient(“localhost”,3030); t.ByteOrder ="big-endian";
t = tcpip("127.0.0.1",3030,"NetworkRole","client");fopen (t)
t = tcpclient("127.0.0.1",3030); t.ByteOrder ="big-endian";
fclose(t) delete(t) cleart
cleart

Note

Since the default value ofByteOrderisbigEndianfortcpipobjects andlittle-endianfortcpclientobjects, you must set it using dot notation to make the property values match.

For more information, seetcpclient.

Write and Read

These examples use an echo server to show how to perform a binary write and read, and how to write and read nonterminated string data, using the recommended functionality.

Functionality Use This Instead
echotcpip("on",3030)% t is a tcpip objectfwrite(t,1:5); data = fread(t,5)
data =1 2 3 4 5
echotcpip("on",3030)% t is a tcpclient objectwrite(t,1:5,"uint8") data = read(t,5)
data =1×5 uint8 row vector 1 2 3 4 5
data =double(data)
data =1 2 3 4 5
echotcpip("on",3030)% t is a tcpip objectfwrite(t,"hello","char")长度= 5;data =fread(t,length,"char")
data =104 101 108 108 111
data =char(data)'
data ='hello'
echotcpip("on",3030)% t is a tcpclient objectwrite(t,"hello","string");length = 5; data = read(t,length,"string")
data ="hello"

For more information, seewriteorread.

Read Terminated String

These examples show how to write and read terminated string data using the recommended functionality.

Functionality Use This Instead
echotcpip("on",3030)% t is a tcpip objectt.Terminator ="CR"; fprintf(t,"hello") data = fscanf(t)
data ='hello '
echotcpip("on",3030)% t is a tcpclient objectconfigureTerminator (t)"CR");writeline(t,"hello");data =readline(t)
a = "hello"
echotcpip("on",3030)% t is a tcpip objectt.Terminator ="CR"; fprintf(t,"hello") data = fgetl(t)
data ='hello'

fgetlreads until the specified terminator is reached and then discards the terminator.

echotcpip("on",3030)% t is a tcpip objectt.Terminator ="CR"; fprintf(t,"hello") data = fgets(t)
data ='hello '

fgetsreads until the specified terminator is reached and then returns the terminator.

For more information, seewritelineorreadline.

Read and Parse String Data

This example shows how to read and parse string data using the recommended functionality.

Functionality Use This Instead
% t is a tcpip objectdata =scanstr(t,';')
data =3×1 cell array {'a'} {'b'} {'c'}
% t is a tcpclient objectdata =readline(t)
data ="a;b;c"
data =strsplit(data,";")
data =1×3 string array "a" "b" "c"

For more information, seereadline.

Write and Read Back Data

This example shows how to write ASCII terminated data and read ASCII terminated data back using the recommended functionality.

Functionality Use This Instead
% t is a tcpip objectdata =query(t,'ctrlcmd')
data ='success'
% t is a tcpclient objectdata =writeread(t,"ctrlcmd")
data ="success"

For more information, seewriteread.

Write and Read Data with the Binary Block Protocol

This example shows how to write data with the IEEE standard binary block protocol using the recommended functionality.

Functionality Use This Instead
% t is a tcpip objectbinblockwrite(t,1:5); data = binblockread(t)
data =1 2 3 4 5
% t is a tcpclient objectwritebinblock(t,1:5,"uint8");data =readbinblock(t)
data =1 2 3 4 5

For more information, seewritebinblockorreadbinblock.

Flush Data from Memory

These examples show how to flush data from the buffer using the recommended functionality.

Functionality Use This Instead
% t is a tcpip objectflushinput(t)
% t is a tcpclient objectflush(t,"input")
% t is a tcpip objectflushoutput(t)
% t is a tcpclient objectflush(t,"output")
% t is a tcpip objectflushinput(t) flushoutput(t)
% t is a tcpclient objectflush(t)

For more information, seeflush.

Set Terminator

These examples show how to set the terminator using the recommended functionality.

Functionality Use This Instead
% t is a tcpip objectt.Terminator ="CR/LF";
% t is a tcpclient objectconfigureTerminator (t)"CR/LF")
% t is a tcpip objectt.Terminator = {"CR/LF"[10]};
% t is a tcpclient objectconfigureTerminator (t)"CR/LF",10)

For more information, seeconfigureTerminator.

Set Up Callback Function

These examples show how to set up a callback function using the recommended functionality.

Functionality Use This Instead
% t is a tcpip objectt.BytesAvailableFcnCount = 5 t.BytesAvailableFcnMode ="byte"t.BytesAvailableFcn = @mycallbackfunctionmycallback(src,evt) data = fread(src,src.BytesAvailableFcnCount); disp(evt) disp(evt.Data)end
Type: 'BytesAvailable' Data: [1×1 struct] AbsTime: [2019 12 21 16 35 9.7032]
% t is a tcpclient objectconfigureCallback(t,"byte",5,@mycallback);functionmycallback(src,evt) data = read(src,src.BytesAvailableFcnCount); disp(evt)end
ByteAvailableInfo with properties: BytesAvailableFcnCount: 5 AbsTime: 21-Dec-2019 12:23:01
% t is a tcpip objectt.Terminator ="CR"t.BytesAvailableFcnMode ="terminator"t.BytesAvailableFcn = @mycallbackfunctionmycallback(src,evt) data = fscanf(src); disp(evt) disp(evt.Data)end
Type: 'BytesAvailable' Data: [1×1 struct] AbsTime: [2019 12 21 16 35 9.7032]
% t is a tcpclient objectconfigureTerminator (t)"CR") configureCallback(t,"terminator",@mycallback);functionmycallback(src,evt) data = readline(src); disp(evt)end
TerminatorAvailableInfo with properties: AbsTime: 21-Dec-2019 12:23:01

For more information, seeconfigureCallback.

See Also

Related Topics