How can I format this data from the serial port in MATLAB?

I am trying to connect a digital sensor to a computer through MATLAB.

First, I send a request for data to the sensor, then the sensor responds with a six-byte stream.

MATLAB reads the sensor as follows:

data1 = fscanf(obj1, '%c', 6);

I know exactly what the contents of the data should be, but I do not know how to read the data in the package that MATLAB released.

A packet ( data1) is only 6 bytes, but how can I access each individual element as a whole?

I never touched MATLAB programming before I lost.

Side question: what do the data formats the MATLAB %c, %s, %c\nand %s\n? I tried to find it, but found nothing.

+3
source share
2 answers

The format %cspecifier indicates that FSCANF reads six characters. You should be able to convert these characters to integer values ​​using the DOUBLE function :

data1 = double(data1);

Now it data1should be a six-element array containing integer values. You can access each of the indexes in an array :

a = data1(1);  %# Gets the first value and puts it in a

If you want to combine pairs of values ​​in data1so that one value represents the highest 8 bits of a number and one value represents the least 8 bits, the following should work:

a = int16(data1(1)*2^8+data1(2));

data1(1) data1(2) , INT16 . INT16, DOUBLE (, , ).

%s , . FSCANF, .

+4

cstruct AJ Johnson MATLAB File Exchange. C, . () MATLAB. , - .

0

Source: https://habr.com/ru/post/1723804/


All Articles