%% Matlab script for testing TCP/IP communication with matlab
%% This script is the sender part
%% Written by Philippe Lucidarme
%% https://lucidar.me/en/networks/matlab-tcp-ip-sender-and-receiver/





%% TCP/IP Sender

% Clear console and workspace
clc;
clear all;
close all;

% Configuration and connection
t = tcpip('192.168.0.44',4013);

% Open socket and wait before sending data
fopen(t);
pause(0.2);


% Send data every 500ms
for i=0:10    
    DataToSend=[i;i]
    fwrite(t,DataToSend);
    pause (0.5);
end

% Close and delete connection
fclose(t);
delete(t);





%% TCP/IP Receiver


% Clear console and workspace
% close all;
% clear all;
% clc;
% 
% Configuration and connection
% disp ('Receiver started');
% t=tcpip('192.168.0.10', 4013,'NetworkRole','server');
% 
% Wait for connection
% disp('Waiting for connection');
% fopen(t);
% disp('Connection OK');
% 
% Read data from the socket
% for i=0:10
%     DataReceived=fread(t,2)
% end