Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Receive negative number from HC-05

1 回表示 (過去 30 日間)
Nikola Cabrilo
Nikola Cabrilo 2019 年 5 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I want to send negative number from microcontroller (PIC) to Matlab. When i receive 32bit negative number, for example 0xffffff36, and try to convert it to int32 i dont get my number. Code is:
Enc2 = [Enc2,int32(bitor(bitor(Enc21,bitsll(Enc22,8)),bitor(bitsll(Enc23,16),bitsll(Enc24,24))))];
Enc21, Enc22, Enc23 and Enc24 are four bytes of 32bit number. The problem is when i try this part of code:
bitsll(Enc24,24)
I got as result 0xffffffff when i look it as binary (with function dec2bin). If i change int32 to int64, and than try to do the same, and represent it as binary it is correct, but of course it doesn't see it as correct number becouse its 64bits. What is the correct way to get correct representation of int32 number when one is received as 4 separate bytes?

回答 (1 件)

Guillaume
Guillaume 2019 年 5 月 19 日
I don't have the fixed-point toolbox but I would suspect that bitsll returns an 8 bit integer if the input is 8 bit.
You say you receive a 32-bit integer, in which case there's nothing to do, but later say that you get 4 bytes, so what do you get?
If you have 4 bytes, e.g.
bytes = uint8([56, 255, 255, 255]); %FFFFFF36 as hex, split into LITTLE-ENDIAN bytes
then
numberasint32 = typecast(bytes, 'int32')
If you get the bytes in Big-Endian order
bytes = uint8([255, 255, 255, 56]); %same number in Big Endian
numberasint32 = swapbytes(typecast(bytes, 'int32'))

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by