a little help with building a command in hex string...

Hi guys…

I need to figure out the method how to prepare the command string for the devise…

Device reads the 6 bytes in HEX string as a command so…

like that:

Str2[0]=0x01;
Str2[1]=0x0F;
Str2[2]=0x00;
Str2[3]=0x00;
Str2[4]=0x11;
Str2[5]=(Str2[0]^Str2[1]^Str2[2]^Str2[3]^Str2[4]);

mySerial.write(Str2, sizeof(Str2));

Where is Str[2] , [3] and [4] = the data
each Str2 contains 255 max or FF

Here is a simple scetch I sending a digit from serial monitor the scetch converts the the digit to HEX but it works no problem untill 255 with Str2[4]; which is int E1 in the cketch.

So I need to use Str2[3] byte to exceed 255…

So how to include Str2[3] byte in the code where digit I sending higher then 255…

So actually I got to get 2 INTs from one INT E1 when it’s higher the 255… I pretty sure that there should a mathematical formula but I can’t find it…

I broke my head around this… I know should be simple but…

here is the code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int E2;
int E1;

String Str;

byte Str2[6];

void setup()  
{
  Serial.begin(57600);

  Serial.println("Goodnight moon!");
  Serial.setTimeout(10);
   delay(100);
  mySerial.begin(9600);

}

void loop() // run over and over
{
    if(Serial.available()>0)
    {
      
        Str  = Serial.readString();
        delay(10);
        int ch1 = Str.length();  
        
        if(ch1 == 1)
        {
          E1 = Str[0]-48;
          E2=0;
        }
         
          if(ch1 == 2)
        {
          E1 = ((Str[0]-48)*10)+Str[1]-48;
          E2=0;
        } 
          
          if(ch1 ==3)
        {
         int  E1 = (((Str[0]-48)*10+(Str[1]-48))*10)+(Str[2]-48);
            
            if(E1 > 255)
            {
              
            }
               
       } 

         
Str2[0]=0x01;
Str2[1]=0x0F;
Str2[2]=0x00;
Str2[3]=E2;
Str2[4]=E1;
Str2[5]=(Str2[0]^Str2[1]^Str2[2]^Str2[3]^Str2[4]);

mySerial.write(Str2, sizeof(Str2)); //20


    }


}

A byte can only store values up to 255. If you have an int, that is a two byte variable this can be split into two bytes by:-

int number = 12345;
lowerByte = number & 0xff;
upperByte = number >> 8;

Where & is a bit wise AND

is shift right

Grumpy_Mike

Thanks man for kicking me in right direction

E2=E1>>8;

That's it loke always