【问题标题】:fwrite different following fread in a tcpip connection? [matlab]在 tcpip 连接中 fwrite 跟 fread 不同? [matlab]
【发布时间】:2018-11-12 08:09:42
【问题描述】:

在编写一个类的过程中将两个matlab实例连接在一起。这些实例将位于不同的计算机上,但我目前正在一台计算机上进行测试。

目前,我能够在两个 matlab 之间建立连接,并且能够在它们之间发送/接收消息。

代码:

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();

发送的消息是一个 8 位双精度 555。 然而,当查看收到的消息时,它原来是一个矩阵

64
129
88

不明白发生了什么,因为我一直关注的examples 没有这个问题。

并添加上下文。我正在尝试通过 TCP-IP 连接 2 个 matlab,这样我就可以控制一个实例与另一个实例。我的计划是让第二个 matlab 等待命令代码并在第一个 matlab 请求时执行指定的函数。

【问题讨论】:

    标签: matlab tcp


    【解决方案1】:

    tcpip/fread默认精度为uchar,所以默认fread会输出一个8位无符号整数的列数组。 p>

    您要么需要指定预期的双精度:

    %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');
    

    typecast uint8 数组为双精度:

    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'));
    

    【讨论】:

    • 感谢您的解释。这就解释了为什么我收到一个数组。给出的第一个例子解决了这个问题。我尝试了第二个,但类型转换似乎对 rawMessage 没有影响。有什么理由吗?
    • 也许 Matlab 将输入读取为字节,然后将其转换为双精度(即输出具有 0-255 范围内整数值的双精度浮点数组)。所以,rawMessage 应该手动转换为 uint8。我将编辑答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2014-07-13
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多