Working with serial input

First of all, hello to everyone.

I'm quite new to Arduino. startet with building a Wiicopter based on the Arduino Pro Mini. But now i want to get a bit deeper into Arduino usage. I played around a bit with the code examples and for now i think I understand the most of it.

So my new project should Display the Livedata of my RC-model Datalogger (in the future by wireless transmission).

So here is my question:

i get a Dataaset of 24 Byte on the serial input. Now I have to divide the dataset into the different information. to start I wanted to store the information in an array of 24 Bytes. So i should get all the information stored in 1 Byte each. But there are also some information sored together in 1 Byte

here an Example:

Byte 1 : bit 1-4 dataset type ; bit 5-8 datasetnumber

any help is welcome.

thanks Uwe

You can use bit-fields in a structure.

struct logData {
  unsigned int dataSetType : 4;
  unsigned int dataSetNumber: 4;

The conventional way to deal with this sort of thing in C is via shifting and masking bits. Maybe something like this:

// Byte 1 : bit 0-3 dataset type ; bit 4-7 datasetnumber
#define DATASET_TYPE_MASK 0x0f
#define DATASET_NUM_MASK 0xf0


  uint8_t buf[10];
  int type, num = 5;

  // Encode type=pontoon and num
  buf[6] = DATASET_TYPE_PONTOON | ((num << 4) & DATASET_NUM_MASK);

  // decode position 9
  type = buf[9] & DATASET_TYPE_MASK;
  num = (buf[9] & DATASET_NUM_MASK) >> 4;

Thanks for your fast replies,

the first example looks good for me. The second one, I'm sorry, but my Programming skills aren't that advanced at the moment :disappointed_relieved:. Furthermore I think the first method would be better as nearly the whole mentioned 24 byte are used bitwise to send some specific data. So I suppose i could define the whole datastructure in the structure.

as I've mentioned my programming skills are still developing. Is there a good reference I can use to learn more about the structure and bit-fields. I did'nt find much as I searched for it now.



Is there a good reference I can use to learn more about the structure and bit-fields.

Google "C bit-fields" claims 24 million hits. I don't expect they're all good, but a quick glance suggests the top five are helpful.

Bit fields may look simple to use, but there is a good deal of trickiness to hurdle over yet.

First, the operation of bit-fields is compiler- and platform-dependent, so you will want to look specifically at AVR-GCC bit fields. Here's an interesting thread:

AWOL did not mention how you will convert the int you read from the serial port into the bit-field struct so you can extract the data. That will look just as scary as what I posted.

AWOL did not mention how you will convert the int you read from the serial port into the bit-field struct so you can extract the data.

A simple cast is "scary"?

It sounds like what you want is a combination of bit fields and a union:

union convertor
      byte type: 4;
      byte number: 4;
    } pieces;
    byte whole;
} buff[24];

union convertor buff2[36]; // alternate way of declaring later in code

Once you have this, you ‘pour’ the bytes into buff, and get them out the other side as bit fields pieces:

for (i=0;i<24;++i)
  buff[i].whole= {get data in somehow} ;
  int y=buff[i].pieces.type; // the type of each entry
  int z=buff[i].pieces.number; // the number of each entry

Of course, if the bitfield repeats the same for all 24 bytes, then you don’t need the array - one temporary value will do:

union convertor data;
data.whole= {get data in somehow} ;
int y=data.pieces.type; // the type of each entry
int z=data.pieces.number; // the number of each entry

But, if the data layout changes throughout the 24 bits, then you’ll need to tweak the union for all the bytes.


thanks for the third optionj David.

As told yesterday, I'm not a pro in programming, and so startet some deeper investigation yesterday evening. I tried to understand the example with the masking and shifting, and basically i understood what should happen. But I still do not understand gardeners example. I think I'll have to get into all this a bit deeper.

So I'm shure the next questions will come, but for now I have a lot to read and to learn.

Thanks Uwe

Glad to help Uwe.

Because I had (obviously) too much time on my hands, I wrote it up for my blog, including some different examples - perhaps it will explain it more clearly for you:

And as already shown here, there are many ways to do what you want - unions just happen to be one way.