How to send 16bits value from Arduino serial to matlab simulink?

Hello,
I am trying to send 16bits read analog value of a servo motor to Simulink/Matlab software through serial communication protocol. Actually, I already sent an 8bit value & I read it on matlab, but I couldn't do the same with 16bits value read (0-1023 analog read). here is the arduino code I am using :

#include <SCServo.h>

SCSCL sc;
union Data{  uint16_t now; } data;

void setup() {   // put your setup code here, to run once:
    Serial.begin(9600);
    
Serial.begin(115200);
  Serial1.begin(1000000);
  sc.pSerial = &Serial1;

}

void loop() {   // put your main code here, to run repeatedly:
    uint16_t sensorVal = sc.ReadPos(2);
     union Data data; 

    data.now = sensorVal;

    Serial.write(0xCC); // Send the start byte
Serial.write((sensorVal >> 8) & 0xFF); // Send the upper byte first
Serial.write(sensorVal & 0xFF); // Send the lower byte
    delay(10); }

and here is the simulink model:

As you can see in the arduino code, I am trying to separate the 16bits data to 2 of 8bits data but in vain...
the display shows wrong data values

Can you please give me some ideas about where how could this work?

image_2021-03-26_153624.png

image_2021-03-26_153624.png

Perhaps it's an endian issue. Try sending the bytes the other way round.

wildbill:
Perhaps it's an endian issue. Try sending the bytes the other way round.

I agree with Bill.

wildbill:
Perhaps it's an endian issue. Try sending the bytes the other way round.

I tried both LittleEndian & BigEndian methods!! but in vain..

image_2021-03-26_155353.png

image_2021-03-26_155353.png

ToddL1962:
I agree with Bill.

I tried both LittleEndian & BigEndian methods as shown in the pic above!!

I would try sending a fixed value so you know what it is and see what matlab translates it to. Maybe that'll give you a clue as to what is going on.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.