Configuration of BGM113 through UART



I’m using BGM113 and I’m trying to understand how to parse the headers for proper processing using the BLE112 as reference (here).

The library class has a function dedicated to parsing the header, but I’m not understanding it completely. This is the section I need to understand from parse_packet(), starting in line 594:

event.msg_type <- (buffer[0] & 0x80);
event.tech_type <- (buffer[0] & 0x78) >> 3;
event.length <- ((buffer[0] & 0x07) << 8) + buffer[1];
event.cid <- buffer[2];
event.cmd <- buffer[3];

I want to choose the 4 bytes a header has, but the code is indexing (?) some other stuff I don’t truly know, because this is what I would’ve done:

event.msg_type <- buffer[0];
event.lenght <- buffer[1];
event.cid <- buffer[2];
event.cmd <- buffer[3];

Why are they doing it differently? This is a screenshot of the reported values in this order (the first one is the result for server.log(buffer)):

  1. event.msg_type <- (buffer[0] & 0x80);
  2. event.tech_type <- (buffer[0] & 0x78) >> 3;
  3. event.length <- ((buffer[0] & 0x07) << 8) + buffer[1];
  4. event.cid <- buffer[2];
  5. event.cmd <- buffer[3];
  6. event.msg_type <- buffer[0];
  7. event.lenght <- buffer[1];
  8. event.cid <- buffer[2];
  9. event.cmd <- buffer[3];

Any help would be appreciated!



The code in the library is pulling individual bits out.

eg msg_type is just the top bit of the first byte - it’ll be either 0x80 or 0x00. tech_type is the next 4 bits (0x78 is a bitmask of 0b01111000), which is then shifted right 3 places so if - as in this case - bit 5 is set, you’ll see it as 0x04 instead of 0x20 which is what you’d see without the shift).

Length is pulling out the length which has the 3 MSB in the bottom 3 bits of buffer[0], and the 8 LSB in buffer[1].

Your code would only work for packets up to 255 bytes long - a 256 byte packet would have bit 0 of buffer[0] set and zero in buffer[1], for example. You’re ignoring the top 3 MSB.

1 Like


Thanks for this useful description Hugo.

Ok, this will help me tons with my adaptation.