Search code examples
matlabtcp

fwrite different following fread in a tcpip connection? [matlab]


In the process of writing a class to connect 2 instances of matlab together. the instances will be on separate computers but I'm currently testing on 1 computer.

Currently I'm able to establish a connection between both matlabs and I'm able to send/receive messages between them.

code:

classdef connectcompstogether<handle
    properties
        serverIP
        clientIP
        tcpipServer
        tcpipClient
        Port = 4000;
        bsize = 8;
        Message
    end

    methods
        function gh = connectcompstogether(~)
            % gh.serverIP = '127.0.0.1';
            gh.serverIP = 'localhost';
            gh.clientIP = '0.0.0.0';
        end

        function SetupServer(gh)
            gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
            set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
            fopen(gh.tcpipServer);
            display('Established Connection')
        end

        function SetupClient(gh)
            gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
            set(gh.tcpipClient,'InputBufferSize',gh.bsize);
            set(gh.tcpipClient,'Timeout',30);
            fopen(gh.tcpipClient);
            display('Established Connection')
        end
        function CloseClient(gh)
            fclose(gh.tcpipClient);
        end
    end
    methods
        function sendmessage(gh,message)
            fwrite(gh.tcpipServer,message,'double');
        end

        function recmessage(gh)
            gh.Message = fread(gh.tcpipClient,gh.bsize);
        end
    end
end

matlab 1

gh = connectcompstogether;
gh.SetupServer();
gh.sendmessage(555);

matlab 2

gh = connectcompstogether;
gh.SetupClient();
gh.recmessage();

the message sent is an 8 bit double 555. However when looking at the received message it turns out to be a matrix

64
129
88

don't understand what is going on as the examples I have been following don't have this problem.

and to add context. I'm trying to connect 2 matlabs through TCP-IP so I can control one instance with another. my plan is to have a second matlab waiting for command codes and execute specified functions when the 1st matlab requests.


Solution

  • tcpip/fread default precision is uchar, so by default fread will output a column array of 8-bit unsigned integers.

    You either need to specifiy that a double is expected:

    %size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
    gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');
    

    Or typecast the uint8 array to a double:

    rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
    % cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
    % then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
    % a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
    gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));