TCP/IP Simulink 未接收数据

问题描述 投票:0回答:1

我正在尝试将数据从 C++ 程序发送到 Simulink 进行进一步处理。出于测试目的,我在同一台计算机上进行此操作,以进一步扩展以将数据发送到另一台计算机。我尝试通过 TCP 连接执行此操作。为了在 Simulink 中接收数据,我使用 TCP/IP 客户端接收模块 不幸的是,我无法让该块接收任何数据。

为了仔细检查连接,我使用了 Wireshark 程序,数据已成功从 C++ 发送。首先,我尝试发送一个包含 3 个逗号分隔坐标的字符串。此后,我只发送了一个双倍,但这也不起作用。我尝试检查 Simulink 模块的状态,但只得到 False,因此该模块没有接收任何数据。对于字符串,也许解析是问题所在,但我也无法双重工作。有人知道如何继续吗?

这是我用来发送信息的 C++ 代码片段。

// Initialize the endpoint to the loopback IP address and a specific port number
remote_endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 9999);

std::cout << "Socket opened successfully" << std::endl;
std::cout << "Connecting to: " << remote_endpoint.address() << ":" << remote_endpoint.port() << std::endl;

try {
    tcp_socket.connect(remote_endpoint);
} catch (const boost::system::system_error& e) {
    std::cout << "Failed to connect: " << e.what() << std::endl;
}

//tcp_socket.connect(remote_endpoint);
std::cout << "Connected successfully" << std::endl;

打印以逗号分隔的字符串:

975000,518.000000,287.000000
980000,518.000000,287.000000
985000,518.000000,287.000000
990000,518.000000,287.000000
995000,518.000000,287.000000

双重打印:

7.922e+07
7.9225e+07
7.923e+07
7.9235e+07
7.924e+07

这是 Simulink 设置:

Simulink Block Simulink Data types

c++ matlab tcp boost-asio simulink
1个回答
0
投票

您永远不会显示实际发送的代码。因此,我们不知道您要发送什么,也不知道如何发送。

我最好的猜测是您需要记住 Simulink 需要大端数据这一事实。比较:

#include <boost/endian/conversion.hpp>
#include <boost/asio.hpp>
#include <iostream>

namespace asio = boost::asio;
using asio::ip::tcp;

int main() {
    tcp::socket tcp_socket(asio::system_executor{});
    tcp_socket.connect({{}, 9999});
    std::cout << "Connected: " << tcp_socket.remote_endpoint() << "\n";

    std::vector<double> dd{
        975000, 518.000000, 287.000000, //
        980000, 518.000000, 287.000000, //
        985000, 518.000000, 287.000000, //
        990000, 518.000000, 287.000000, //
        995000, 518.000000, 287.000000,
    };

    write(tcp_socket, asio::buffer(dd));

    for (auto& d : dd)
        boost::endian::big_to_native_inplace(d);

    write(tcp_socket, asio::buffer(dd));
}

例如针对 netcat 进行测试时

nc -lp 9999 | od --endian=big =tf8

可以打印(假设是小端架构,就像在我的 Linux 机器上一样):

显然,只有经过字节序转换后数据才是正确的

奖金!

考虑自动转换和域类型。字符串表示形式中的元组建议使用点类型,例如:

住在Coliru

#include <boost/asio.hpp>
#include <boost/endian/arithmetic.hpp>
#include <iostream>

template <typename Real> struct Point {
    Real x, y, z;
    friend std::ostream& operator<<(std::ostream& os, Point p) {
        return os << "{ " << p.x << ", " << p.y << ", " << p.z << " }";
    }
};

using NetPoint = Point<boost::endian::big_float64_t>;
static_assert(std::is_trivially_copyable_v<NetPoint>);
static_assert(std::is_standard_layout_v<NetPoint>);
static_assert(sizeof(double) * 3 == sizeof(NetPoint));

namespace asio = boost::asio;
using asio::ip::tcp;

int main() {
    tcp::socket tcp_socket(asio::system_executor{});
    tcp_socket.connect({{}, 9999});
    std::cout << "Connected: " << tcp_socket.remote_endpoint() << "\n";

    std::vector<NetPoint> dd{
        {975000, 518.000000, 287.000000}, 
        {980000, 518.000000, 287.000000},
        {985000, 518.000000, 287.000000},
        {990000, 518.000000, 287.000000},
        {995000, 518.000000, 287.000000},
    };

    write(tcp_socket, asio::buffer(dd));
}

具有相似的输出

© www.soinside.com 2019 - 2024. All rights reserved.