Is it possible to send different types of data via UDP?

2 ビュー (過去 30 日間)
Bas Holten
Bas Holten 2017 年 2 月 6 日
コメント済み: Walter Roberson 2018 年 5 月 10 日
I am trying to send an array of data composed of both double() and uint32() values via UDP as the receiving application requires such a configuration. The size of the data packet should become 40 bytes eventually (4 doubles and 2 ints) However when I inspect the data packets manually the length of the data in the UDP packet drops to 24 bytes when I try to send both types of data.
Does Matlab automatically convert all the data into uint32() data as soon as I try to send both data types?
Thank you all in advance, Bas

回答 (1 件)

Bas Holten
Bas Holten 2017 年 2 月 7 日
I found a (rather sloppy) workaround myself by creating a dummy double precision value to fool the receiving application.
I1 = uint32(x);
I2 = uint32(y);
% Create two 8 hexadecimal byte strings for each of them
I1_hex = dec2hex(I1,8);
I2_hex = dec2hex(I2,8);
% Combine them into a single 16 hexadecimal byte string
hex_string = strcat(I2_hex,I1_hex); % switched order of the ints to comply with the endianness of the readout
% Create a dummy value to send via UDP
dummy = hex2num(hex_string);
This seems to work for the application. As this interface only serves as a testing application it is not terrible that it uses a workaround rather than a solution.
  2 件のコメント
Dmitriy Makovkin
Dmitriy Makovkin 2018 年 5 月 10 日
Bas, thanks for answering your own question - it has helped me with my own application. Do you know whether it matters if I1 & I2 are declared as uint32? It looks like no matter the type, the dec2hex(I1,8) command will always produce a 16 byte variable. Also, in your example, what is the significance of using 2 variables of the same type? How would the code change if I were interested in sending a uint32, uint16, and a uint8 in one string instead of 2 uint32s?
Walter Roberson
Walter Roberson 2018 年 5 月 10 日
dec2hex requires that its input have positive integral value (but not necessarily positive integer data type), and it outputs the hex representation of that integer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInstrument Control Toolbox Supported Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by