Send struct over UDP

Hi all,
I need to transmit a structured data over a UDP connection, which will be read from a C/C++-based application and saved on a similar struct (actualy the same, just designed in C).
Is it possibile to transmit it directly on the UDP (from Matlab, or Simulink, or both), mantaining the byte-to-byte order? I want to avoid packing the data on Matlab, and then unpacking the incoming data (on the C/C++ interface) with nested for loop and if/switch case conditions. Just directly assigned byte by byte the inconig pack to a C/C++ struct.
Any help or reference to alternative solutions will be appreciate.
Thanks

Risposte (1)

Jan Houska
Jan Houska il 17 Ott 2021

2 voti

Hi Andrea,
if you are using Simulink Desktop Real-Time, you can use the Packet Output block. The block allows you to specify a sequence of data types which together form the packet that is sent. This is the same as sending the structure with the elements equal to the data types specified. The block also supports sending multi-byte elements such as integers in little or big endian.
Good Luck, Jan

5 Commenti

Andrea Ciullo
Andrea Ciullo il 28 Ott 2021
Hi Jan,
thank you vey much for your answer.
I just have one doubt more: where I can define the struct data type?
I mean, the mask property of the packet (input or output) block has the packet data type field (see pic).
Does this field accept only standard data types (e.g. int, double, ect), or I can define a new one somewhere?
Thank you very much,
Andrea.
Jan Houska
Jan Houska il 1 Nov 2021
Hi Andrea,
you do not use or define the data type directly. Instead, you fill in the elements of the structure into the data type field, sequentially in the order you want then to appear in the resulting structure. The blocks then packs the elements into a single packet and sends out the whole packet, which is equivalent to sending a structure.
Goold Luck, Jan
Andrea Ciullo
Andrea Ciullo il 2 Nov 2021
Hi Jan,
thanks again for the info.
I did the way you suggested, implementing a script to create the data type string for filling the block's field. I had to this since the struct I receive is quite complex (struct of structs). Unfortunatelly, this bring me out as many signals as the data types I decleared (e.g. uint8, double, double --> 3 different output signals, in my case extend it to 50 signals). Is ther a way to have them packed in a single output?
Thanks,
Andrea
Jan Houska
Jan Houska il 2 Nov 2021
Hi Andrea,
the block does not support buses as input signal, so your only choice is to use Bus Selector to split the input bus to individual signals.
To reduce the number of signals somewhat, you can use e.g. '3*double' instead of 'double', 'double', 'double'. This will cause the three separate signals to be replaced by one vector signal of width 3. However, this only works for consecutive signals (structure fields) that are of the same datatype.
Good Luck, Jan
Andrea Ciullo
Andrea Ciullo il 2 Nov 2021
Ok, I see.
I think that I'll just go to route programmatically with a mux the different signals composing a struct.
Thank you very much.
Andrea

Accedi per commentare.

Richiesto:

il 30 Set 2021

Commentato:

il 2 Nov 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by