 # Error in shifting data bytes

Hello everyone,

I need little assistance in shifting four bytes in a variable. I am reading four bytes from device that is connected to Arduino. The sequence of four bytes is BC 0D 05 15. I am putting them in a variable so that it becomes 0515BC0D. I am doing it as shown in the sketch below. I have in total 31 bytes where byte 05 is at 25, 15 at 26, BC at 23, and 0D at 24.

``````  unsigned long int combined = 0;
long x0 = incomingBytes << 24;
long x1 = incomingBytes << 16;
long x2 = incomingBytes << 8;
long x3 = incomingBytes;
combined = x0 | x1 | x2 | x3;
Serial.println(x0);
Serial.println(x1);
Serial.println(x2);
Serial.println(x3);
Serial.println(combined);
``````

The above sketch works fine on MKR 1400 board and I get the value in variable combined = 0515BC0D, but on Arduino UNO board when I print the values this is what I get:

x0 = 0
x1 = 0
x2 = FFFFBC00
x3 = 0D
combined = FFFFBC0D

May I know why do I get the above values and not the values as on MKR board? Thank you

A quick guess is that you need to cast incomingBytes to a long before doing the shift for each of x0, x1 x2 & x3. But that's just a guess since you've not posted all your code and we can't see your declaration of incomingBytes.

MKR 1400 is 32 bits, Uno is 8 bits. Variables are treated differently.

guix:
MKR 1400 is 32 bits, Uno is 8 bits. Variables are treated differently.

Oh okay. Yeah, my data is 32 bits and that's why UNO isn't able to handle it. Thanks guix for the input.

You can do it something like this which works also for an 8 bit MCU

``````void setup() {
Serial.begin(115200) ;

uint32_t combined = 0 ;

combined = 0x05 ;
combined = (combined << 8) | 0x15 ;
combined = (combined << 8) | 0x26 ;
combined = (combined << 8) | 0x0D ;
Serial.println(combined,HEX);
}

void loop() {

}
``````
``````#include <SoftwareSerial.h>
SoftwareSerial mySerial (8, 9);

byte incomingBytes ;                                                           // array for 31 bytes incoming serial data
byte message [] = {0xAA, 0x03, 0x00, 0x24, 0x00, 0x0D, 0xDD, 0xDF};                // message to read 13 registers

void setup() {

Serial.begin(115200);                                                            // opens serial port, sets data rate to 115200 bps
mySerial.begin(115200);                                                          // opens serial port, sets data rate to 115200 bps
}

void loop () {
requestMessage();                                                               // request message function call
receivedBytes();                                                                // received bytes function call

batterySOC();
}
void requestMessage() {

mySerial.write(message, sizeof(message));                                          // send request message to wake BMS up if in deep sleep mode
delay(500);
while (mySerial.read() >= 0) {                                                     // clear the buffer
}
mySerial.write(message, sizeof(message));                                          // send request message
delay(500);
}

void receivedBytes() {

if (mySerial.available())
{
mySerial.readBytes(incomingBytes, 31);
}
}

void batterySOC() {

unsigned long int combined = 0;                                                      // clear it out
unsigned long int x0 = incomingBytes << 24;
unsigned long int x1 = incomingBytes << 16;
unsigned long int x2 = incomingBytes << 8;
unsigned long int x3 = incomingBytes;
combined = x0 | x1 | x2 | x3;                                                    // combining required 4 data bytes of received 8 bytes

Serial.println(x0, HEX);
Serial.println(x1, HEX);
Serial.println(x2, HEX);
Serial.println(x3, HEX);
Serial.print("Data value in HEX = ");
Serial.println(combined, HEX);                                                // print the 4 combined HEX bytes in HEX
Serial.println("");
Serial.print("Data value in DEC = ");
Serial.println(combined, DEC);                                                // print them in decimal
Serial.println("");
Serial.print("State of Charge: ");
battery_SOC = (float)combined / 1000000.0;                                       // dividing the dec values by 1000000 to get the values of SOC in BMS register
Serial.print(battery_SOC);                                                       // print the SOC value
Serial.println(" %");
Serial.println("");
}
``````

@markd833 that’s the sketch. That works fine on MKR board but not UNO. But I think what guix commented is the reason.

It will work if you follow given advices. I prefer how 6v6gt did it, but mark’s advice will work as well

``````long x0 = ((long)incomingBytes) << 24;
...
``````

I checked that and it worked. Thank you very much everyone for the help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.