How to read 64-bit integer data from serial port

2 views (last 30 days)
Hi,
I work with a piece of hardware that sends data to USB port in an signed 64-bit integer format. I do not know how to read the stream of data with such precision. When I try to read the stream of data using:
X = fread(s, 1, 'long');
The data does not make any sense. I understand that the 'long' precision may be 32 or 64 bit. So I guess in this case it functions as 32-bit. I have though of using 'double' or 'float64' precision but the read data does not make any sense either. Can anybody help me through this problem?
Thanks in advance

Answers (1)

Walter Roberson
Walter Roberson on 27 May 2015
X4 = fread(s, 8, 'uint8');
now re-arrange the byte order to match that used by the sending machine. For example,
X4n = X4([3 4 1 2 7 8 5 6]); %if that's what the source happens to use
then
X = typecast(X, 'int64');
In the most simple case where the bytes do not have to be rearranged, you can
X = typecast(fread(s, 8, 'uint8'), 'int64');
or for more efficiency,
X = typecast(fread(s, 2, 'uint32'), 'int64');

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!