Abhishek Agarwal
Abhishek Agarwal

Reputation: 1248

Send data to serial port in little endian

I am trying to send some floating-point data to a serial port. The data by default is getting transmitted in big-endian. I am using the following code:

ser = serial('COM1');
set(ser,'BaudRate',115200);
fopen(ser);
fwrite(ser,1.7,'single'); %sends in big-endian format

Matlab documentation for fwrite does mention the format:

fwrite(fileID,A,precision,skip,machinefmt)

where machinefmt can change endianness but that does not seem to work while writing to serial port.

fwrite(ser,1.7,'single',0,'ieee-le');
fwrite(ser,1.7,'single',0,'l');

Both these commands throw the error:

Error using serial/fwrite (line 124)

Too many input arguments.

Is there a workaround for sending data in little-endian format?

Upvotes: 1

Views: 1343

Answers (1)

Marcos G.
Marcos G.

Reputation: 3506

I think you have read the documentation for writing to a file.

In recent versions of Matlab at least fwrite has its own help and if you read it you'll see that the endianness is a property.

First, create the port:

ser = serial('COM1');

And then check or change its endianness:

get(ser,'ByteOrder')
set(ser,'ByteOrder', 'littleEndian')

By the way, I think the default option is little-endian, so you might want to try bigEndian.

Source: Matlab help

Upvotes: 2

Related Questions