Serial read values

Hi there,
I am sending data across serial communication protocol from Simulink/Matlab to Arduino, I am trying to debugg what kind of data I am receiving, The code above reads a single sent data from matlab & displays it in an lcd screen. does anyone have an idea about what I am receiving ?
here are the sent & received data

sent value =2, received data :

0 0 0 64 0 0 0 64.....

sent value =2, received data :

0 0 128 63 0 0 128 63 00 128 63 0 0...

sent value =8, received data

0 0 0 65 0 0 0 65 0 0 0 65 0 0 0 65....

sent value =10 , received data

0 0 32 64....

Please find above arduino & Simulink code :

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display

int foundValue = 0;
int cursor = 0;

void setup() {
 lcd.init();
 lcd.clear();
 lcd.backlight();      // Make sure backlight is on
 Serial.begin(9600);
}

void loop() {
 if (Serial.available() > 0) { 
   byte rc;
   rc = Serial.read();
   lcd.setCursor(cursor, 0);  //Set cursor to character 2 on line 0
   cursor += lcd.print(rc);  //print serial value to LCD
   cursor += lcd.print(" ");
 }

 delay(1000);    //Wait for 1s

 if (cursor >= 16) {
   lcd.clear();
   cursor = 0;
 }
}```

what arduino are you using?
are you sure the LCD is displaying correctly? e.g. does a simple test writing text to the display work OK
make sure both Matlab and the arduino are using the same baudrate, data bits, etc

if Matlab is transmitting ASCII text Serial.read reads a byte try Serial.readString()

yes, it works well! I already tried it with Hello world code & it works well!
you can verify that baudrate is the same in the provided arduino code & in this simulink picture image
I'll give it a tray & I'll get back to you !

With Serial.readString(), it doesn't display anything, but when I stop simulation it display the above attached image (uknown characters).

I am using Arduino Mega. The LCD works pefectly with other codes.
The corrected code :


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display

int foundValue = 0;
int cursor = 0;

void setup() {
  lcd.init();
  lcd.clear();
  lcd.backlight();      // Make sure backlight is on
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) { 
    String rc;
    rc = Serial.readString();
    lcd.setCursor(cursor, 0);  //Set cursor to character 2 on line 0
    cursor += lcd.print(rc);  //print serial value to LCD
    cursor += lcd.print(" ");
  }

  delay(1000);    //Wait for 1s

  if (cursor >= 16) {
    lcd.clear();
    cursor = 0;
  }
}```

if you use the Arduino Serial Monitor and type characters on the keyboard do they appear OK on the LCD? if so this proves you can receive ASCII text and display it OK

is Simulink transmitting ASCII text or binary (I have used Matlab but not Simulink)
if text try adding a newline terminator ('\n' in C)

have a look at Simulink formatted text output

Yes when I use the arduino Serial Monitr it works fine & it displays the content clearely.
I 'll give a try & I'll get to you back asap.

looks like Simulink is transmitting binary
if I run the following program using gnu C on a PC

union Data {
   float f;
   char c[4];
} data;
int main(void)
{
    data.f=2.0;
    printf("%x %x %x %x\n", data.c[0], data.c[1], data.c[2], data.c[3]);
    printf("%d %d %d %d\n", data.c[0], data.c[1], data.c[2], data.c[3]);
    data.f=10.0;
    printf("%x %x %x %x\n", data.c[0], data.c[1], data.c[2], data.c[3]);
    printf("%d %d %d %d\n", data.c[0], data.c[1], data.c[2], data.c[3]);
}

I get

0 0 0 40
0 0 0 64
0 0 20 41
0 0 32 65

the decimal output is similar to your original result on the LCD

you could use a union similar to the above to convert the recevied binary to a float and print the result