MATLAB实现TCP/IP通信

1、使用R2021a版本的函数
参考链接:MATLAB TCP

t = tcpclient(address,port)
t = tcpclient(address,port,Name,Value)

使用 IP 地址连接到 TCP/IP 远程主机
使用所示的 IP 地址和端口 80 创建一个名为 t 的 TCP/IP 客户端连接。

t = tcpclient("192.168.1.0",80)

连接到 TCP/IP 远程主机并设置超时期限
创建一个名为 t 的 TCP/IP 客户端连接,并将超时期限设置为 20 秒。

t = tcpclient("144.212.130.17",80,"Timeout",20)

Address — 远程主机名或 IP 地址
Port — 远程主机端口
ConnectTimeout — 连接到远程主机的允许时间

t = tcpclient("144.212.130.17",80,"ConnectTimeout",30) 将连接超时期限设置为 30 秒。

EnableTransferDelay — 允许来自服务器的延迟确认

 t = tcpclient("144.212.130.17",80,"EnableTransferDelay",false) 将禁用传输延迟。

t = tcpserver(address,port)
t = tcpserver(port)
t = tcpserver(___,Name,Value)

listens for connections at port 4000 and IP address 144.212.100.10.

t = tcpserver("144.212.100.10",4000)

sets the read/write timeout period to 20 seconds.

t = tcpserver("144.212.100.10",4000,"Timeout",20)

sets the byte order to big-endian.

t = tcpserver("144.212.100.10",4000,"ByteOrder","big-endian") 

Create the TCP/IP server called server and set the ConnectionChangedFcn property to a handle to the connectionFcn callback function.

server = tcpserver("localhost",4000,"ConnectionChangedFcn",@connectionFcn)

2、服务器和客户端
服务器

t_server=tcpip('0.0.0.0',30000,'NetworkRole','server');%与第一个请求连接的客户机建立连接,端口号为30000,类型为服务器。
t_server.InputBuffersize=100000;
fopen(t_server);%打开服务器,直到建立一个TCP连接才返回;
%get(t_server);
while(1)
    if  t_server.BytesAvailable>0
         t_server.BytesAvailable
        break;
    end
end
pause(1);
data_recv=fread(t_server,t_server.BytesAvailable/8,'double');%从缓冲区读取数字数据
fclose(t_server);

客户端

t_client=tcpip('localhost',30000,'NetworkRole','client');%与本地主机建立连接,端口号为30000,作为客户机连接。
t_client.OutputBuffersize=100000;
fopen(t_client);%与一个服务器建立连接,直到建立完成返回,否则报错。
data_send=sin(1:64);%发送的数字数据。
%%%%%%%% txt_send='HELLO'; %发送的文本数据
pause(1);%等待连接稳定,随意设置。
fwrite(t_client,data_send,'double');%写入数字数据
pause(1);
%%%%%%%% fprintf(t_client,txt_send);%发送文本数据
fclose(t_client)

3、TCP通信
建立一个TCP并打开

ipA = '192.168.10.5';%本机ip地址
portA = 8080;%本机端口
handles.tcpip_client = tcpip('192.168.10.10',8000,'NetworkRole','client');%%与本地主机建立连接,端口号为8000,作为客户机连接
handles.tcpip_client.OutputBuffersize=100000;%这个是设置的缓冲区大小,matlab默认的缓冲区大小仅仅512bit,而一个double类型的数据就要占据8字节大小的空间,所以超出范围后就写不进去缓冲区,发出去的只有那一部分,所以有必要扩大缓冲区。所以弄大一点。
handles.tcpip_client.TimeOut = 1;
handles.tcpip_client.Terminator = 'LF';%不同设备的停止位会有所不同
fopen(handles.tcpip_client);%与一个服务器建立连接,直到建立完成返回,否则报错。
fprintf("连接成功");

用TCP传输数据
因为每个协议的里面格式都是不一样的,但是只要保证传输的数据转化为二进制是相同的即可

%用于控制设备的data
data = [12,22,33];
    display(data);
    %fprintf(handles.tcpip_client,data);
    fwrite(handles.tcpip_client,data);%写入(发送)数据
    pause(0.1);

接收TCP数据
fwrite 与 fread搭配,数值型数据
fprintf 与 fscanf搭配,字符型数据

if handles.tcpip_client.BytesAvailable > 0 %判断是否有数据可用,这个判断的目的是为了避免matlab Warning: A timeout occurred before the Terminator was reached. 错误
    fprintf('NOT Blank');
    a1 =  fread(handles.tcpip_client,handles.tcpip_client.BytesAvailable);
    %a1 =  fscanf(handles.tcpip_client,handles.tcpip_client.BytesAvailable);
    display(class(a1));
    display((a1)');
    display(size(a1));
    set(handles.TCPRece,'string',a1);
end
%a1 = fscanf(handles.tcpip_client,'%c');
%a1 =  fread(handles.tcpip_client,handles.tcpip_client.BytesAvailable);
%a1 =  fgets(handles.tcpip_client);
%display(abs(a1));
pause(0.1);

关闭TCP

fclose(handles.tcpip_client);
delete(handles.tcpip_client);
clear ipA portA 
fprintf("断开成功");

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_27353621/article/details/127048951