Multiple Serial Transfer Data

Hi,
Finally I can send from Simulink/Matlab to arduino, the problem I am getting is that Sent data is not updated, I mean if I start simulation It starts displaying the sent value & If I try to update it (change the sent data value) it doesn't update.
My second problem is that I want to send multiple data (a vector of data), how would I receive them in arduino?

Here is MAtlab code image
Here is arduino 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;
int Save[3];
void setup() {
  lcd.init();
  lcd.clear();
  lcd.backlight();      // Make sure backlight is on
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) { 
    uint32_t rc;
    for(int i=0; i<4;i++)
  {
    rc = Serial.read();
    Save[i]=rc;
  }
    int Sum = Save[0]+Save[1]*256;
    lcd.setCursor(0,0);
    lcd.print(Sum);
    delay(1000);  
  }
  delay(1000);    //Wait for 1s
}```

The problem here is you receive one byte on the serial, and then assume you have received 4. Why not wait until you have received 4 bytes and then do your loop?
Paul

how would this " int Sum = Save[0]+Save[1]*256;" be changed then?

won't I get a problem of synchronization?

How? You already have a big problem with synchronization. Fix that first.
Paul

1 Like

There are three values in Save.
Save[0] , Save[1] , Save[2]

With this code, you are writing data to Save[3] which is memory you do not "own", and anything can happen when you do this.

 for(int i=0; i<4;i++)
  {
    rc = Serial.read();
    Save[i]=rc;
  }

Sorry but I did no get what do you mean by you do not own & anything can happen with when this is done...
Actually, I created save to convert from 8 bits that serial can handle to 16 bits so I can get my >255 values... I don't know if you got what I say...

If you have fixed the reading of an empty buffer, please show us the new code.
Paul

yes I think I did, it displays the right value, but still cannot be updated in real time, here is the 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;
byte Save[4];
void setup() {
  lcd.init();
  lcd.clear();
  lcd.backlight();      // Make sure backlight is on
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) { 
    uint32_t rc;
    for(int i=0; i<4;i++)
  {
    Save[i]=Serial.read();
  }
    int Sum = Save[0]+Save[1]*256;
    lcd.setCursor(0,0);
    lcd.print(Sum);
    delay(1000);  
  }
  delay(1000);    //Wait for 1s
}```

Nope! Still are processing 4 bytes in the buffer when only 1 has arrived from your PC.
While you are at it, consider if you are REVERSING the order of the bytes sent from the PC.
Paul

int Save[3]; //three element array Save
Save[3]=rc; //

Your code will corrupt memory. If you have 3 elements in an array, they have index numbers: 0, 1, 2. You are trying to write data to index number 3. The compiler may have placed something else in the memory location you are writing, and the consequences are undefined, i.e. anything can happen.

EDIT: I see you have redefined the size of Save correctly to hold 4 values.

I am sending an uint_32t value (with 4 bytes), Actually the sent data value is 400 which is more than 255 it means that it occupies at least 2 bytes, I think that 2 bytes has arrived from my pc, am I wrong? Correct me if I am wrong please!

What does the documentation say about Serial.available? What does it return?
Paul

the number of available bits in the receive buffer...

Bytes. Unread bytes.

sorry bytes yes !

Ok, you are getting close. When your code discovers there is one or more unread byte in the buffer, what does it do? It reads 4 bytes. One is real, the other three are imaginary.
Paul

so no need for the for loop at all....

If you keep the code you have, try waiting for greater than 3 bytes, not greater than 0.
Paul

it displays the sent data but it doesn't update it...