# Receive binary data into a variable

**URL:** <https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202>\
**Category:** Programming\
**Created:** [December 21, 2012, 8:22am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202 "2012-12-21T08:22:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjwillia](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mjwillia](https://forum.arduino.cc/u/mjwillia)\
**Post date:** [December 21, 2012, 8:22am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/1 "2012-12-21T08:22:00Z")

</div>

Hey all, I'm very new to Arduino and need some help with a project.

I want to receive 8 bits of binary data on the hardware serial port (9600n2), look and the first 4 bits (it's an ID) to see if it's addressed to us, and if it is then compare the last 4 bits to some known's to decide what the message was telling us to do.

I can handle the latter, but not sure how to wait for the serial port to get 8 bits in the buffer before doing something, then how to store the first 4 in a ID variable and the next 4 in another variable.

Please any help to get me on the right track would be appreciated.

Thanks 🙂  
-Matt

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [December 21, 2012, 8:36am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/2 "2012-12-21T08:36:58Z")

</div>

Hello and welcome 🙂

You could wait for 8 separate bits, or you could send a single byte that you then parse in your sketch 🙂

Example:

```cpp
byte binary_data = 0b11000101;

byte ID_variable = (binary_data & 0xF0) >> 4; //will be equal to 0b1100
byte another_var =  binary_data & 0x0F;      //will be equal to 0b0101

```

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [December 21, 2012, 8:47am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/3 "2012-12-21T08:47:54Z")

</div>

The arduino handles serial communications for you - it delivers the data a byte at a time, so there is no need to worry about waiting for individual bits. You use serial.available to find out if any data has arrived, and if so, serial.read to get it.

---

<div class="post-metadata">

**Author:** ![mjwillia](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mjwillia](https://forum.arduino.cc/u/mjwillia)\
**Post date:** [December 21, 2012, 9:10am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/4 "2012-12-21T09:10:17Z")

</div>

Thanks for the reply's

Still a bit lost (no pun intended).

Perhaps it's better if I tell you what is happening.  
In total there are 10 ID's:  
0000  
0001  
0010  
0011  
0100  
1000  
1001  
1010  
1011  
1100

Then the next 4 bits are a binary number 0 through 9 (9=1001 and so forth)

I need the arduino to listen for it's own ID and then store the ascii number into a variable.  
After that I'm driving a 7segment display directly from the arduino.

It's kinda like mimicking a 7seg latch driver.

I just don't understand how to get from the binary serial to the point that I have a ascii value stored in a variable.

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [December 21, 2012, 9:29am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/5 "2012-12-21T09:29:42Z")

</div>

What is sending 8 bits to the Arduino? You with the Serial monitor, or a sensor/device?

> mjwillia:  
> I need the arduino to listen for it's own ID

So you have 10 Arduinos, each with it's own ID?

---

<div class="post-metadata">

**Author:** ![mjwillia](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mjwillia](https://forum.arduino.cc/u/mjwillia)\
**Post date:** [December 21, 2012, 9:38am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/6 "2012-12-21T09:38:26Z")

</div>

Yes, Correct

They are scattered all over a building connected by RS485 (I convert back to rs232 before giving it to the arduino).  
The binary data stream originates from a propriety system I can't change.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 21, 2012, 9:54am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/7 "2012-12-21T09:54:52Z")

</div>

```cpp
if (Serial.available ()) {
  int candidate = Serial.read ();
  if ((candidate & 0xF0) == myAddress) {
    char ascii = '0' + (candidate & 0x0f);    // edit: sorry, forgot the masking here.
  }
}

```

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [December 21, 2012, 10:11am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/8 "2012-12-21T10:11:24Z")

</div>

Ok then you have to wait receiving 8 bytes, that you then convert in a single byte 🙂

Something like this (maybe):

```cpp
void loop()
{
  if ( Serial.available() == 8 )
  {
    byte bytes[8];
    for ( uint8_t i = 0; i < 8; i++ )
      bytes[i] = Serial.read();

    byte result = 0;
    for ( uint8_t i = 0; i < 8; i++ )
      result += bytes[7-i] << i;

    byte ID = (result & 0xF0) >> 4;
    if ( ID == ARDUINO_ID )
    {
      byte ASCII =  result & 0x0F;
      DisplayTo7Seg( ASCII );
    }
  }
}

```

So if the bytes sent by your device are for example: 1,1,0,0,0,1,0,1 , the ID will be 1100, and the ASCII value will be 101.

Note, I'm beginner too so there may be a better way, or maybe my code won't even work 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 21, 2012, 10:15am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/9 "2012-12-21T10:15:08Z")

</div>

> I want to receive 8 bits of binary data on the hardware serial port

Bits, not bytes.

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [December 21, 2012, 10:19am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/10 "2012-12-21T10:19:36Z")

</div>

But a bit will be read as a byte by Serial.read()..Correct?

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [December 21, 2012, 10:23am UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/11 "2012-12-21T10:23:17Z")

</div>

> guix:  
> But a bit will be read as a byte by Serial.read()..Correct?

No.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:01pm UTC](https://forum.arduino.cc/t/receive-binary-data-into-a-variable/135202/12 "2021-05-05T20:01:04Z")

</div>

Closed on May 5, 2021, 8:01pm UTC


