% AMLlistenOPEN -- Matlab version 1.1.0
% Will_Ferenc@hmc.edu
% 6/22/2011 UCLA Applied Math Lab
% 2011 Applied Math REU 

%=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-USAGE-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
% AMLlistenOPEN is a matlab script that opens connections to a specified
% IP address and port number. Before running the script make sure that the
% IP address and port numbers are correct. These will most likeley change
% every time you log in to your workstation. The script will enter an
% infinite loop at the end to read all data broadcast over the TCPIP
% connection, press Ctrl + c to terminate.
% Package instructions summary follows.
%
% 0. Obtain and load AMLCOM and a listening script (eg. AMLlistenOPEN)
% 1. Find IP address of both computers (google "find my IP address")
% 2. Input IP address of the remote computer for each script
% 3. Run scripts in tandem. (If not they will timeout and error)
% 4. Wait for prompt, then activate the AML car, then press any key
% 5. Check receiving computer for results. Perhaps use fread(t,#bytessent)
%-------------------------------------------------------------------------
'AMLlistenOPEN -- Matlab -- Verision 1.0.2 UCLA AML Robotics'

%=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=Update Vars=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
REMOTEIP = '128.97.12.32'
%-------------------------------------------------------------------------

t = tcpip(REMOTEIP,'RemotePort',4433,'LocalPort',4455)
set(t, 'Timeout', 99); %Set slow timeout for initial connection
fopen(t);
set(t, 'Timeout', 0.2); %Reset to fast timeout for usage
while(1)
pause(5)
    if(t.BytesAvailable ~= 0)
        IPINPUT = char(fread(t, t.BytesAvailable))'
    end
end
