I’m trying to send data from a C++ program to Simulink for further processing. For testing purposes, I’m doing it on the same computer to expand this further to send the data to another computer. I tried doing this over a TCP connection. To receive the data in Simulink, I'm using a TCP/IP Client Receive block Unfortunately, I can’t get the block working to receive any data.
To double-check the connection, I used the program Wireshark and the data is being sent from C++ successfully. First, I tried to send a string with 3 comma-separated coordinates. After this, I sent only a double but this also is not working. I tried to check the status of The Simulink block and only got False, so the block does not receive any data. For the string, maybe the parsing is the problem but I also can’t get the double working. Does anyone have a clue how to proceed?
This is a snippet of C++ code Im using to send the information.
// 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;
A print of the string seperated with a comma:
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
This is the Simulink setup:
Simulink Block and Simulink Data types
Update
This is the code that I tried to use:
// Pack the timestamp into a double
double data = static_cast<double>(ts);
// Print the timestamp
std::cout << "Timestamp: " << data << std::endl;
// Send the data over TCP
boost::system::error_code error;
boost::endian::big_to_native_inplace(data);
boost::asio::write(tcp_socket, boost::asio::buffer(&data, sizeof(data)), error);
// Error handling
if(!error) {
std::cout << "Data sent successfully!" << std::endl;
} else {
std::cout << "Send failed: " << error.message() << std::endl;
}
I suspect that the datasize attribute in the Simulink block is misconfigured and I don’t exactly know what it represents. I initially thought it is the byte amount received but I found the Simulink documentation quite vague.
You never show the code that actually sends. So, we don't know what you're sending nor how.
My best guess is you need to keep in mind the fact that Simulink expects big-endian data. Compare:
When testing against netcat e.g.
May print (assuming little-endian architecture, like on my linux box):
Clearly, only after the endianness conversion the data are correct
BONUS!
Consider automatic conversions and domain type. The tuples in your string representation suggest a point type, like:
Live On Coliru
With similar output