%Set up serial connection
mcu = serial('COM9','BaudRate',9600,'DataBits',8,'StopBits',1);
mcu.Timeout = 180;
mcu.Terminator = 122;
%set up tcpip connection
input_port = 8080
output_port = 8080
import java.net.ServerSocket
import java.net.Socket
import java.io.*
    
    server_socket = ServerSocket(input_port);
    input_socket = server_socket.accept;
    instream = input_socket.getInputStream
    b_instream = BufferedInputStream(instream)
    
    output_socket = Socket('128.84.82.175', output_port);
    output_stream = output_socket.getOutputStream;
    buffered_output_stream = BufferedOutputStream(output_stream); % optional
    d_output_stream = DataOutputStream(buffered_output_stream);

    k = 0
    2+3
    a = 0
    b = 0
    c = 0
    d = 0
    e = 0
    f = 0
    t = 0
    while(1)
        str=0
        if(t==0)
            a = instream.read
            b = instream.read
            c = instream.read
            d = instream.read
            e = instream.read
        else            
            a = instream.read
            while(a==122)
                a = instream.read
            end;
            b = instream.read
            b = instream.read
            c = instream.read
            c = instream.read
            d = instream.read
            d = instream.read
            e = instream.read
            e = instream.read
        end;
        t=1;
        k=0;
        fopen(mcu);
        %Send the first character to the mcu
        if(a~=0)
            fwrite(mcu,a,'char')
        end;
        %Send the second character to the mcu
        if(b~=0)
            fwrite(mcu,b,'char')
        end;
        %Send the third character to the mcu
        if(c~=0)
            fwrite(mcu,c,'char')
        end;
        %Send the fourth character to the mcu
        if(d~=0)
            fwrite(mcu,d,'char')
        end;
        %Send the fifth character to the mcu
        if(e~=0)
            fwrite(mcu,e,'char')
        end;
        %Close the serial port
        fclose(mcu);
        %Reopen the serial port, this time for receiving
        fopen(mcu);
        %print the system time to the Matlab window
        a = fscanf(mcu)
        %print the system time to the Matlab window
        b = fscanf(mcu)
        %print the system time to the Matlab window
        c = fscanf(mcu)
        %print the system time to the Matlab window
        d = fscanf(mcu)
        %print the system time to the Matlab window
        e = fscanf(mcu)
        %Close the serial port
        fclose(mcu);
        output_stream.write(int8(a));
        output_stream.write(int8(b));
        output_stream.write(int8(c));
        output_stream.write(int8(d));
        output_stream.write(int8(e));
    end;