How Serial read work any explanation

i recive them correct but i cant add them together again i got just 255 just just the high byte

Reciver code

how should i collect the code again ?
to recive 65280 ? or 0xFF00

#include <Arduino.h>

char read;
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("Low byte: ");
    Serial.print((uint16_t)save[0],HEX);
    Serial.println();
    Serial.print("High byte: ");
    Serial.print((uint16_t)save[1],HEX);
//Till hier every things is good

    save[1]=(save[1]>>8);
    save[3]=save[1]+save[0];
    Serial.println();
    Serial.print("Answer ");
    Serial.print((uint16_t)save[3]);
    Serial.println();
    i++;
    if(i>1){
      
      i=0;
    }
  }
  
}

No you multiply the the high byte by 256 and add it to the low byte to reconstruct your int.

What is your degree in?

This seems lightweight stuff as back in 1986 I gave one of my students the project of making a FORTH system in machine code. And he did it rather well. He was doing his degree in Physics.

Post your sender codes/sketch.

Here is the Reciver

#include <Arduino.h>

char read;
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)
  {
    int16_t answer;
    read = Serial1.read();
    save[i] = read;
 

    Serial.println();
    Serial.print("Low byte: ");
    Serial.print(save[0], BIN);
    Serial.println();

    Serial.print("High byte: ");
    Serial.print(save[1], BIN);

    answer = (int16_t)save[0];          // first bytes
    answer = (answer << 8);             
    answer =answer  + (int16_t)save[1] ; 

    Serial.println();
    Serial.print("Answer ");
    Serial.print(answer);
    Serial.println();
    i++;
    if (i > 1)
    {

      i = 0;
    }
  }
}

Here is the sender

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


// 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 ;
uint16_t wertex = 65534;
   
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();
        Serial.print("Byte1: ");
        Serial.print((char)byte1,HEX); // X value
        Serial.println();
        Serial.print("Byte2:  ");
        Serial.print((char)byte2,HEX);
        // Serial.println("Acculeration");
        Serial1.write((char)byte1); // X value
        Serial1.write((char)byte2);
    
      
        
    }
}

Instead of the above, do the following to send binary data (not ASCII characters).

unsigned char byte1, byte2 ;
uint16_t wertex = 65534; //FFFE

void setup()
{
  Serial.begin(9600);
  byte1 = (wertex & 255);        //0xFE
  byte2 = ((wertex >> 8) & 255); //0xFF
  Serial.println();
  Serial.print("Byte1: ");
  Serial.print(byte1, HEX);//((char)byte1, HEX); // X value
  Serial.println();
  Serial.print("Byte2:  ");
  Serial.print(byte2, HEX);//((char)byte2, HEX);
  Serial.println("Acculeration");
  Serial1.write(byte1);//((char)byte1); // X value
  Serial1.write(byte2);//((char)byte2);
}

void loop()
{

}

is not the same ?

char is also Bits

my problem is not in Sender it is in Reciver i recive the corect value also but when i try to Concatenate the bytes i have problem

i reecive them right now i want to add them together and have the value 1111 1111 1111 1110

There is a difference between the following two declarations:

char y1 = 'A';    //y1 = 01000001
byte y2 = 'A' ;    //y2 = 01000001

Observe the output of the following codes:

  Serial.println(y1); //shows: A
  Serial.println(y2); //shows: 65
  Serial.println((char)y2); //shows A

That right what you mean. but for me is importent at the end to have a 16 bits Value i dont know how to add 2 bytes together. like if i have 8 bits + 8 bits i should have a 16 bit value like
1111 1111 1111 1110

Send your werex data item (65324) as as 2 bytes in binary (not character); receive bytes in binary and then combine them to retrieve the original 65324.

Sending:
Serial1.write(highByte(werex));
Serial1.write(lowByte(werex)):

Receiving:
byte n = Serial1.available ();
if(n == 2)
(
unsigned int y = Serial1.read()<<8|Serial1.read ();
Serial.println(y, DEC); //shows: 65324
)

Finally work now i got the number right
I had a small problem i was trying with save dirctly but i save it in SenDate and it work correct.
i just needed a variable uint16_t
Thanks for your suppurt @GolamMostafa was really alot of help for me

#include <Arduino.h>

char read;
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;
 
    uint16_t SenDate;


    Serial.println();
    Serial.print("Low byte: ");
    Serial.print(save[0],BIN);
    Serial.println();


    Serial.print("High Byte: ");
    Serial.print(save[1],BIN);
    Serial.println();


//sheft and save it  and add the bits / bytes together  
    SenDate=(save[1]);
    SenDate=(SenDate<<8);
    SenDate=SenDate+save[0];
    Serial.print("Concat: ");
    Serial.print(SenDate);
   

    i++;
    if (i > 1)
    {

      i = 0;
    }
  }
}

Sender

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


// 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 ;
uint16_t wertex = 65534;
   
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();
        Serial.print("Byte1: ");
        Serial.print((char)byte1,HEX); // X value
        Serial.println();
        Serial.print("Byte2:  ");
        Serial.print((char)byte2,HEX);
        // Serial.println("Acculeration");
        Serial1.write((char)byte1); // X value
        Serial1.write((char)byte2);
    
      
        
    }
}

image

Is it your sketch or you have copied and mofified?

no that is mein i wrote it my self

@mousahasan

As previously requested, please stop posting pictures of code and output

but it is a small part from my orginal one i have another code with around 2000 line and i had the problem here i dont know how to explane it that why i did a new small code to ask

What are you doing with that 2000 lines program?

Posting a small but complete sketch that illustrates the problem is exactly the right thing to do, but post it here in code tags rather than as a picture. The same goes for output, paste it here in code tags

am sending data from the sensor to the Due then i have to work with that data and send it to matlab and MIDI to make musik and plots
and in the same time i have to send comand via serials to the sensors to stop sending data and start sending the data to the Due (Basis station).
Due (Basis station) is my big part code. in sensor i have just to collect data and send it
the most code and header are in Basis station

What does this Basis Station do? Does it produce any goods?

Through the sensors we are developing Gesture recognition system to controll light and musik.
in Theater for examples. Due get the data from the sensors and comntrols light and musik in Theater.