int16 to uint8 array conversion for Serial Transmit
13 次查看(过去 30 天)
显示 更早的评论
Hello there, I would like to take a int16 and split it into a uint8 array to transmit via serial connection (arduino).
Then i would like to reassemble the the two bytes to give me back my original int16. Any help would be appreciated :)
0 个评论
回答(3 个)
Guillaume
2017-5-26
编辑:Guillaume
2017-5-26
exampleint16 = int16(12345);
sameasuint8 = typecast(exampleint16, 'uint8')
restoredint16 = typecast(sameasuint8, 'int16')
However, if all you want to do is transmit that int16. You could just fwrite it as is, and read it as an uint8:
%on the sender
fwrite(serialobj, yourint16, 'int16')
%on the reader
fread(serialobj, 2, 'uint8')
1 个评论
Walter Roberson
2017-5-26
If you split into uint8 using typecast() then you need to take care of the endian problems yourself. For this purpose you need to know that if you typecast a 16 bit quantity into uint8 then on all current implementations of MATLAB, the first byte of the result is the least significant byte.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!