Receive binary data into a variable

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 :slight_smile:
-Matt

Hello and welcome :slight_smile:

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

Example:

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

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.

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.

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?

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.

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

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

Something like this (maybe):

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 :slight_smile:

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

Bits, not bytes.

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

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

No.