How Serial read work any explanation

Hi,
am trying to send Data betwwen 2 Arduino via UART
i send it as 2 bytes the first one is 100 1100 and the second one is 1 1110
i send byte 2 first and i try to read byte 2 first but arduino read byte 1 first and then byte 2
see Photos

thanks.

Please post your sketches here in code tags rather than pictures of the code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi,
am trying to send Data between 2 Arduino via UART
i send it as 2 bytes the first one is 100 1100 and the second one is 1 1110
i send byte 2 first and i try to read byte 2 first but arduino read byte 1 first and then byte 2

thanks.
here where i send data

#include <Arduino_LSM9DS1.h>
#include <Arduino.h>




// Function
void ReadAndSendData(); // Liest die Sensor daten. schickt die an Due
void Get_Command();
void Analyze_Command();


// Variable byteread()
unsigned long previousMillis = 0; // Für Timer
long interval = 2000;              // Defulte Dataen Rate 
int zaehler = 0;
float x, y, z, omega, beta, gama, mx, my, mz;
// Variable zu GetRate(
 unsigned char byte1, byte2 ;
int wertex = 17950;
   
int16_t coming_Rate; // Date Rate kommt als Bytes und werden zusammengefügt mit Comming Rate
char r;
String reads;

String str0, strLeer;
bool SendData = false;



void setup()
{
    Serial.begin(115200);  // Serial beging mit 115,200
    Serial1.begin(115200); // Serial beging mit 115,200 configuration in platform.ini nötig    monitor_speed = 115200

    Serial.println("Read the serial");
    if (!IMU.begin())
    {
        Serial.println("Failed to initialize IMU!");
        while (1)
            ;
    }
}

void loop()
{
ReadAndSendData();
  
}

void ReadAndSendData()
{
   
  
 
    if (IMU.accelerationAvailable())
    {
        IMU.readAcceleration(x, y, z);
    
    }

    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) // Timer
    {

        previousMillis = currentMillis;
     
        // werteGama = (gama * 16);   // GAMA ROW
        // X,Y,Z in Bytes teilen
        byte1 = (wertex & 255);        // First byte of x
        byte2 = ((wertex >> 8) & 255); // Second byte of x
        // Serial.println("Acculeration");
        Serial1.write((char)byte2); // X value
        Serial1.write((char)byte1);
        Serial.println();
        Serial.print("Byte2: ");
         Serial.print((char)byte2,BIN); // X value
         Serial.println();
        Serial.print("Byte1:  ");

        Serial.print((char)byte1,BIN);
        
    }
}

here where i get data (byte)

#include <Arduino.h>

char read;
uint32_t startByte;
uint8_t data;
char save[30];
int i =0;
void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);

  Serial.println("Read the serial");
}

void loop()
{
  if(Serial1.available()>0){
    read = Serial1.read();
    save[i]=read;
    Serial.println();
    Serial.print("First: ");
    Serial.print((int16_t)save[0],BIN);
    Serial.println();
    Serial.print("seconde: ");
    Serial.print((int16_t)save[1],BIN);
    i++;
    if(i>1){
      
      i=0;
    }
  }
  
}

I have merged your cross-posts @mousahasan.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please take some time to pick the forum category that best suits the subject of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide you will find at the top of every forum category. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1. When you say byte; make it byte with 8-bit
Sender Codes:

byte data1 = 0b01001100;  //0x4C in hex-base 
byte data2 = 0b00011110;   //0x1E in hex-base

void setup()
{
    Serial.begin(9600);
    Serial1.begin(9600);
}

void loop()
{
     Serial.write(data2);   
     Serial.write(data1);
     delay(1000);   //test interval
}

Receiver Codes:

void setup()
{
    Serial,begin(9600);
    Serial1.begin(9600);
}

void loop()
{
      byte n = Serial1.available();
     if(n == 2)
    {
          Serial.println(Serial1.read(), BIN);  //should show: 11110; //1E 
          Serial.println(Serial1.read(), BIN): //should show:  1001100
    }
}

Thansks you. but i wanna to collect the date as Int from a sensor that why am not working with byte.
do you now how the buffer will be reading ?
i think the buffer is like that
0 1 0 0 1 1 0 0 0 0 0 1 1 1 1 0 Buffer
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

is he reading 15 to 0 first or 0 to 15 first ?

each serial read returns an 8 bit byte. a serial write transmits an 8 bit byte

the serial interface is often used to transmit ASCII characters.

there's no reason it won't transmit raw binary values, but then how do you determine the first byte in the sequence of bytes being transmitted ?

Tell about your sensor type, your Arduino Sender, and Arduino Receiver.

i should make an 16 bit( 2 byte) value at the end.
there is the problem. when i have 16 bit it read it like that:
for example
if i send
1111 1111 0000 0000
will read it like that
0000 0000 1111 1111
so it read position 15 -> 9 first then position 8->0
if i send a Dec number like 65280
i get on the other side the number of 255 after Collection the 2 byte in int16_t
that mean it read 0000 00000 then 1111 1111.
i guess it read the buffer from the last to the first.
and when i make byte1 which should be 1111 1111 + secondes byte what should be 0000 0000
i should receive 1111 1111 0000 0000 but i receive 0000 0000 1111 1111.

I just change the position of byte and i got the right number but i have to explan it for my Bachelor Thesis.
so i should explan why i send first byte at first then the second byte but i collect the second first and then the first.

am using Nano 33 ble and Due.

my Programm work fine i change the Position of the bytes

but i need explantion for my Bachelor Thesis.

Can you read the comment before may you could help me

https://www.programmingelectronics.com/serial-read/
i just found that Video i guess it explan it

OK, but how will you know which byte is which when you receive them ? Do not rely on serial comms being perfect.
What will happen if for some reason you miss receiving one of the bytes ?

Please post the code that you posted pictures of as it is a much simpler example to test and comment on that your full code

To explain what observation/comment?

i have in my Orginal code for my (Bachelor) a check Function and set time out if i miss one it will complet with no problem
so i will have just one byte wrong but it will not make a big problem.

To explan why i send first byte then second byte

Here i cut my 16 bit 2 byte into 2 8 bit
i store the first 8 bit in byte1
then i sheft 8 position and i store the 8 bin in byte 2
then i write it in Serial1
but as u see i write first byte2 then byte1
otherweis if i add them i will recive 255

wertex = 65280; 



 byte1 = (wertex & 255);        // First byte of x 8 bit 
        byte2 = ((wertex >> 8) & 255); // Second byte of x 8 bit  
        // Serial.println("Acculeration");
        Serial1.write((char)byte2); // X value
        Serial1.write((char)byte1);

hier i read

if(Serial1.available()>0){
    read = Serial1.read();
    save[i]=read;
    Serial.println();
    Serial.print("First: ");
    Serial.print((int16_t)save[0],BIN);
    Serial.println();
    Serial.print("seconde: ");
    Serial.print((int16_t)save[1],BIN);

i wrote byte2 first and it has to be save in save[0]
and byte1 has to be save[1]

but save[0] is byte1
and save[1] is byte2

I WRITE BYTE 2 FIRST

Then you guess wrong

What if you sent text, say HELLO, then it would be nonsense if the receiver printed OLLEH and how would it know that O was the last character anyway ?

Serial.read() uses a FIFO (First In, First Out) buffer, ie the bytes are removed from the buffer in the order in which they were received

1. I hope your data item is:
unsigned int y = 65280; //0xFF00
Remember: Though you have declared number in decimal, it is saved as binary. I have written in hex-base just for simplicity as writing so many bits is error prone.

2. You have stored 1st 8-bit into variable byte1.

byte byte1 = lowByte(y);  //byte1 = 0x00

3. You shifted y = 0xFF00 to get byte2.

byte byte2 = (byte) y>>8;  //byte2 = 0xFF

4. You wrote byte2 onto Serial1 Port.

Serial1.write(byte2);

that make since
thanks that what i want to know if FIFO or FILO
in my orignal code it is abit complecated, i send a start byte 255 255 255 255 when the Reciver read that start to collect each 2 byte i save 20
i dont send just 2 byte. so every telegramm has 24 byte 4 start 255 flags and then the right bytes.

so maybe that coz of cuting my 16 bit value into 2 byte each 8 bit.
may when am cuting it store the last 8 bit in byte 1
and the first 8 bit in byte 2 ?

**wertex = 65280; **
//1111 1111 0000 0000 **
** byte1 = (wertex & 255); **
** byte2 = ((wertex >> 8) & 255);

what should be byte1 ?
and what should be byte 2 ?

okey thanks you man you save my life.
now it is much better and clear i was thinkig the whole time wrong.
i thougt my low byte is 0xFF and high byte 0x00
i will try it now and see my result.

Upload the following sketch and observe.

void setup()
{
  Serial.begin(9600);
  unsigned int wertex = 65280; //0xFF00
  //1111 1111 0000 0000 **
  byte byte1 = wertex & 255;
  byte byte2 = (wertex >> 8)&255;
  //--------------------------
  Serial.println(byte1, HEX);
  Serial.println(byte2, HEX);
  //--------------------------
}

void loop()
{

}