Reversing typecast in C

7 ビュー (過去 30 日間)
Luca Maria Castiglione
Luca Maria Castiglione 2018 年 5 月 14 日
編集済み: Luca Maria Castiglione 2018 年 5 月 14 日
Hey everyone, I would really appreciate your help as I am struggling on a (seems to be silly) question. I am working on communication between two heterogenous systems. Specifically one of them is high-level programmed through simulink while the second is completely implemented in C. Communication happens over UDP packers through the Realtime UDP that only accepts bytestreams as its input. In order to transmit double values from matlab to C, simulinks converts the double value to an array of an uint8 using typecast with something like
v = typecast(5.57, 'uint8').
My question is, how I should go back to the original double value, in native C code, once the uint8 array has been received from the second system?
I already tried to generated a kindofcode that reverts typecast applying
typecast(received, 'double')
using codegen. It didn't work, obviously :)
Thank you so much, Best Luca
  1 件のコメント
James Tursa
James Tursa 2018 年 5 月 14 日
Could you be more specific about what is not working? E.g., this seems to work just fine
v = typecast(5.57, 'uint8')
v =
72 225 122 20 174 71 22 64
>> typecast(v, 'double')
ans =
5.5700

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

採用された回答

Guillaume
Guillaume 2018 年 5 月 14 日
As James says, in matlab, and so for codegen I guess, it is indeed
typecast(uint8values, 'double');
In plain C, it's also just a basic cast
char* buffer = yourudpreadfunction;
double* values = (double*) buffer;
One thing to watch out for though, and this may be the cause for your didn't work is that I believe that by default matlab transmits values in udp packets in big endian ordering, so you may nee to reverse the order of the bytes. In matlab:
swapbytes(typecast(uint8values, 'double');
In C, your library may provide a <swapbytes.h> header.

その他の回答 (1 件)

Luca Maria Castiglione
Luca Maria Castiglione 2018 年 5 月 14 日
編集済み: Luca Maria Castiglione 2018 年 5 月 14 日
Thank you guys. I solved the issue! I leave a reference for the solution in case someone needs Extract data from uint8 double
best
Luca

カテゴリ

Help Center および File ExchangeSimulink Coder についてさらに検索

製品


リリース

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by