1
votes

I am working with a F28335 from TI communicating serially (RS-232) with a PC.

In matlab, i need to calculate some double numbers and send them serially to the controller.

For that, for example, i have 1.00e-5, and i convert it to individual bytes using:

Time1On = typecast(Time1On, 'uint8');

The result is:

172  197   39   55

Then I convert this to hex, and i manage to turn it into a string to send it

ACC52737

I send this serially to the F28335, and i get the correct value into a Uint32 variable (a position in an array), but i can't seem to find a way to copy this same individual bytes into a double value, that would get me the same original fraction number.

I tried casting from a Uint32 array, where the received value is correctly stored:

dataDouble = (double) (*(RxPacket.RcvData + RxPacket.idxData));

But I get this:

11100000000000000000000000000000 or 2.898602e+09

And I tried storing it as a union:

union doubleConstruct{
    double Val;
    Uint16 rxArray[2];
};

and saving the 16 word separately (i encode every 16 bits with hamming parity, and decode the 16bits words) into the positions of the array, but the double result is the same...

Anyone has ever broken doubles into bytes and sent them serially? If yes, how did you later composed and recovered the respective double number? Is there a way to just stampo bits of an unsigned int into a double?

Thx!

2
Byte ordering issues? The two systems uses different encodings of float? - Some programmer dude
double type takes 64bits and not 32. You might need to use float. Why do you convert to hex string? sending four bytes is not enough? - Shai
OKEI I managed to copy what I want in a float variable (Val) using the union I declared. I get exactly the bits (ACC52737) I want in it, but the C program interprets this float as -5.60343e-12 while I'm supposed to be getting 1.00e-5. Different interpretations of the same float number??? - user2991509
THIS IS SOLVED. Turns out typecast() function in matlab gives you the individual bytes of the single representation, but you get the LSB in position 1, and the MSB in position 4... WHYY MATLAB WHYY?? If you don't want to deal with typecast you can also use num2hex() which will give the hex value of the single representation in the right order. - user2991509

2 Answers

0
votes

Is this what you want?

>> bytes = [0  0  1 0  102];
>> bytes*(2.^(8*(length(bytes)-1:-1:0))).'

ans =

       65638

I'm considering the most significative byte is to the left. Otherwise use bytes*(2.^(8*(0:length(bytes)-1))).'


If you have the hex string, use base2dec:

>> hex = 'ACC52737'
>> base2dec(hex,16)

ans =

  2.8986e+009

or, to get the bytes,

>> typecast(base2dec(hex,16), 'uint8')

ans =

    0    0  224  230  164  152  229   65
0
votes

Problem is you converted your bytes into string. What you need to do is to concatenate them back to 32bit number and cast it back:

num = 1e-5;
numBits = typecast(single(num ),'uint8');
numBitsConcat = (uint32(sum(uint32(numBits).*uint32(2.^(8*[0 1 2 3])))))
num_ =  typecast(numBitsConcat ,'single');